■ 계약에 대한 별칭을 사용하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
using System; using System.Collections; using System.ServiceModel; /// <summary> /// 서점 인터페이스 /// </summary> [ServiceContract(Namespace = "http://noname.com/bookstore", Name = "IMyBookStore")] interface IBookStore { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 주문하기 - Order(userID, isbn, amount) /// <summary> /// 주문하기 /// </summary> /// <param name="userID">사용자 ID</param> /// <param name="isbn">ISBN</param> /// <param name="amount">수량</param> /// <returns>주문 ID</returns> [OperationContract(Name="PlaceOrder")] int Order(string userID, string isbn, int amount); #endregion #region 취소하기 - Cancel(orderID) /// <summary> /// 취소하기 /// </summary> /// <param name="orderID">주문 ID</param> /// <returns>처리 결과</returns> [OperationContract(Name="CancelOrder")] bool Cancel(int orderID); #endregion #region 검색하기 - Search(title) /// <summary> /// 검색하기 /// </summary> /// <param name="title">제목</param> /// <returns>제목 배열</returns> [OperationContract(Name="SearchByTitle")] string[] Search(string searchTitle); #endregion #region 검색하기 - Search(title, author) /// <summary> /// 검색하기 /// </summary> /// <param name="title">제목</param> /// <param name="author">저자</param> /// <returns>제목 배열</returns> [OperationContract(Name="SearchByTitleAndAuthor")] string[] Search(string title, string author); #endregion } /// <summary> /// 서점 서비스 /// </summary> [ServiceBehavior(IncludeExceptionDetailInFaults = true)] public class BookStoreService : IBookStore { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 주문 ID /// </summary> private static int _orderID = 0; /// <summary> /// 제목 배열 /// </summary> private static string[] _titleArray = new string[] { "도서 1", "도서 2", "도서 3" }; /// <summary> /// 저자 배열 /// </summary> private static string[] _authorArray = new string[] { "저자 1", "저자 2", "저자 3" }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 주문하기 - Order(userID, isbn, amount) /// <summary> /// 주문하기 /// </summary> /// <param name="userID">사용자 ID</param> /// <param name="isbn">ISBN</param> /// <param name="amount">수량</param> /// <returns>주문 ID</returns> public int Order(string userID, string isbn, int amount) { int price = GetUnitPrice(isbn) * amount; _orderID += 1; Console.WriteLine ( "도서 주문 : 주문 ID={0}\n 사용자 ID={1}, ISBN={2}, 수량={3}, 가격={4}", _orderID, userID, isbn, amount, price ); return _orderID; } #endregion #region 취소하기 - Cancel(orderID) /// <summary> /// 취소하기 /// </summary> /// <param name="orderID">주문 ID</param> /// <returns>처리 결과</returns> public bool Cancel(int orderID) { Console.WriteLine("취소 주문 : 주문 ID={0}", orderID); return true; } #endregion #region 검색하기 - Search(title) /// <summary> /// 검색하기 /// </summary> /// <param name="title">제목</param> /// <returns>제목 배열</returns> public string[] Search(string title) { ArrayList arrayList = new ArrayList(); for(int i = 0; i < _titleArray.Length; i++) { if(_titleArray[i].StartsWith(title) == true) { arrayList.Add(_titleArray[i]); } } return (string[])arrayList.ToArray(typeof(string)); } #endregion #region 검색하기 - Search(title, author) /// <summary> /// 검색하기 /// </summary> /// <param name="title">제목</param> /// <param name="author">저자</param> /// <returns>제목 배열</returns> public string[] Search(string title, string author) { ArrayList arrayList = new ArrayList(); for(int i = 0; i < _titleArray.Length; i++) { if(_titleArray[i].StartsWith(title) == true && _authorArray[i].StartsWith(author)) { arrayList.Add(_titleArray[i]); } } return (string[])arrayList.ToArray(typeof(string)); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 단가 구하기 - GetUnitPrice(isbn) /// <summary> /// 단가 구하기 /// </summary> /// <param name="isbn">ISBN</param> /// <returns>단가</returns> private int GetUnitPrice(string isbn) { return 35000; } #endregion } |