PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSADONETDataService/Samples.svc.cs

#
C# | 361 lines | 191 code | 33 blank | 137 comment | 13 complexity | 440cc1017ef41102930a8f041b144f37 MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: Samples.svc.cs
  3. * Project: CSADONETDataService
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * Samples.svc demonstrates the ADO.NET Data Service for non-relational data
  7. * source. The non-relational data source is some in-memory objects which
  8. * hold the All-In-One Code Framework sample projects information. The
  9. * non-relational entity class also implements the IUpdatable interface to let
  10. * the client insert new data.
  11. *
  12. * This source is subject to the Microsoft Public License.
  13. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  14. * All other rights reserved.
  15. *
  16. * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  17. * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  19. \***************************************************************************/
  20. #region Using directive
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Data.Services;
  24. using System.Linq;
  25. using System.ServiceModel.Web;
  26. using System.Web;
  27. using System.Data.Services.Common;
  28. using System.Reflection;
  29. using System.Collections;
  30. #endregion
  31. namespace CSADONETDataService
  32. {
  33. public class Samples : DataService<SampleProjects>
  34. {
  35. // This method is called only once to initialize service-wide policies.
  36. public static void InitializeService(IDataServiceConfiguration config)
  37. {
  38. // Set rules to indicate which entity sets and service operations
  39. // are visible, updatable, etc.
  40. config.UseVerboseErrors = true;
  41. config.SetEntitySetAccessRule("*", EntitySetRights.All);
  42. }
  43. }
  44. #region Non-relational data entity classes
  45. // Sample project entity class with DataServiceKey 'ProjectName'
  46. [DataServiceKey("ProjectName")]
  47. public class Project
  48. {
  49. public string ProjectName { get; set; }
  50. public string Owner { get; set; }
  51. public Category ProjectCategory { get; set; }
  52. }
  53. // Sample project category entity class with DataServiceKey 'CategoryName'
  54. [DataServiceKey("CategoryName")]
  55. public class Category
  56. {
  57. public string CategoryName { get; set; }
  58. }
  59. // Sample data entity class
  60. public class SampleProjects : IUpdatable
  61. {
  62. static List<Category> categories;
  63. static List<Project> projects;
  64. // Static constructor
  65. static SampleProjects()
  66. {
  67. // Initialize the sample project category list
  68. categories = new List<Category>()
  69. {
  70. new Category { CategoryName = "COM"},
  71. new Category { CategoryName = "Data Access"},
  72. new Category { CategoryName = "Office"},
  73. new Category { CategoryName = "IPC and RPC"},
  74. new Category { CategoryName = "WinForm"},
  75. new Category { CategoryName = "Hook"}
  76. };
  77. // Initialize the sample project list
  78. projects = new List<Project>()
  79. {
  80. new Project { ProjectName = "CSDllCOMServer",
  81. Owner = "Jialiang Ge", ProjectCategory = categories[0] },
  82. new Project { ProjectName = "VBDllCOMServer",
  83. Owner = "Jialiang Ge", ProjectCategory = categories[0] },
  84. new Project { ProjectName = "ATLDllCOMServer",
  85. Owner = "Jialiang Ge", ProjectCategory = categories[0] },
  86. new Project { ProjectName = "CSUseADONET",
  87. Owner = "Lingzhi Sun", ProjectCategory = categories[1] },
  88. new Project { ProjectName = "CppUseADONET",
  89. Owner = "Jialiang Ge", ProjectCategory = categories[1] },
  90. new Project { ProjectName = "CSLinqToObject",
  91. Owner = "Colbert Zhou", ProjectCategory = categories[1] },
  92. new Project { ProjectName = "CSLinqToSQL",
  93. Owner = "Rongchun Zhang", ProjectCategory = categories[1] },
  94. new Project { ProjectName = "CSOutlookUIDesigner",
  95. Owner = "Jie Wang", ProjectCategory = categories[2] },
  96. new Project { ProjectName = "CSOutlookRibbonXml",
  97. Owner = "Jie Wang", ProjectCategory = categories[2] },
  98. new Project { ProjectName = "CSAutomateExcel",
  99. Owner = "Jialiang Ge", ProjectCategory = categories[2] },
  100. new Project { ProjectName = "VBAutomateExcel",
  101. Owner = "Jialiang Ge", ProjectCategory = categories[2] },
  102. new Project { ProjectName = "CppFileMappingServer",
  103. Owner = "Hongye Sun", ProjectCategory = categories[3] },
  104. new Project { ProjectName = "CppFileMappingClient",
  105. Owner = "Hongye Sun", ProjectCategory = categories[3] },
  106. new Project { ProjectName = "CSReceiveWM_COPYDATA",
  107. Owner = "Riquel Dong", ProjectCategory = categories[3] },
  108. new Project { ProjectName = "CSSendWM_COPYDATA",
  109. Owner = "Riquel Dong", ProjectCategory = categories[3] },
  110. new Project { ProjectName = "CSWinFormGeneral",
  111. Owner = "Zhixin Ye", ProjectCategory = categories[4] },
  112. new Project { ProjectName = "CSWinFormDataBinding",
  113. Owner = "Zhixin Ye", ProjectCategory = categories[4] },
  114. new Project { ProjectName = "CSWindowsHook",
  115. Owner = "Rongchun Zhang", ProjectCategory = categories[5] }
  116. };
  117. }
  118. // Public property to get the sample projects information from the
  119. // ADO.NET Data Service client side
  120. public IQueryable<Project> Projects
  121. {
  122. get { return projects.AsQueryable(); }
  123. }
  124. // Public property to get the sample projects categoryies information
  125. // from the ADO.NET Data Service client side
  126. public IQueryable<Category> Categories
  127. {
  128. get { return categories.AsQueryable(); }
  129. }
  130. // Implement the IUpdatable methods to enable the insert function
  131. #region IUpdatable Members
  132. // Save the added object temporarily
  133. object tempObj = null;
  134. /// <summary>
  135. /// Adds the given value to the collection
  136. /// </summary>
  137. /// <param name="targetResource">target object which defines the
  138. /// property</param>
  139. /// <param name="propertyName">name of the property whose value needs
  140. /// to be updated</param>
  141. /// <param name="resourceToBeAdded">value of the property which needs
  142. /// to be added</param>
  143. public void AddReferenceToCollection(object targetResource, string
  144. propertyName, object resourceToBeAdded)
  145. {
  146. // Get the target object type
  147. Type t = targetResource.GetType();
  148. // Get the property to be updated
  149. PropertyInfo pi = t.GetProperty(propertyName);
  150. if (pi != null)
  151. {
  152. // Retrieve the collection property value
  153. IList collection = (IList)pi.GetValue(targetResource, null);
  154. // Add the resource into the collection
  155. collection.Add(resourceToBeAdded);
  156. }
  157. }
  158. /// <summary>
  159. /// Revert all the pending changes.
  160. /// </summary>
  161. public void ClearChanges()
  162. {
  163. throw new NotImplementedException();
  164. }
  165. /// <summary>
  166. /// Creates the resource of the given type and belonging to the
  167. /// given container
  168. /// </summary>
  169. /// <param name="containerName">container name to which the resource
  170. /// needs to be added</param>
  171. /// <param name="fullTypeName">full type name i.e. Namespace
  172. /// qualified type name of the resource</param>
  173. /// <returns>object representing a resource of given type and
  174. /// belonging to the given container</returns>
  175. public object CreateResource(string containerName, string
  176. fullTypeName)
  177. {
  178. // Get the type of the resource
  179. Type t = Type.GetType(fullTypeName, true);
  180. // Create an instance of the resource type
  181. object resource = Activator.CreateInstance(t);
  182. // Return the resource object
  183. return resource;
  184. }
  185. /// <summary>
  186. /// Delete the given resource
  187. /// </summary>
  188. /// <param name="targetResource">resource that needs to be deleted
  189. /// </param>
  190. public void DeleteResource(object targetResource)
  191. {
  192. throw new NotImplementedException();
  193. }
  194. /// <summary>
  195. /// Gets the resource of the given type that the query points to
  196. /// </summary>
  197. /// <param name="query">query pointing to a particular resource
  198. /// </param>
  199. /// <param name="fullTypeName">full type name i.e. Namespace
  200. /// qualified type name of the resource</param>
  201. /// <returns>object representing a resource of given type and as
  202. /// referenced by the query</returns>
  203. public object GetResource(IQueryable query, string fullTypeName)
  204. {
  205. throw new NotImplementedException();
  206. }
  207. /// <summary>
  208. /// Gets the value of the given property on the target object
  209. /// </summary>
  210. /// <param name="targetResource">target object which defines the
  211. /// property</param>
  212. /// <param name="propertyName">name of the property whose value needs
  213. /// to be updated</param>
  214. /// <returns>the value of the property for the given target resource
  215. /// </returns>
  216. public object GetValue(object targetResource, string propertyName)
  217. {
  218. // Get the target object type
  219. Type t = targetResource.GetType();
  220. // Get the property
  221. PropertyInfo pi = t.GetProperty(propertyName);
  222. if (pi != null)
  223. {
  224. // Return property value
  225. return pi.GetValue(targetResource, null);
  226. }
  227. else
  228. {
  229. return null;
  230. }
  231. }
  232. /// <summary>
  233. /// Removes the given value from the collection
  234. /// </summary>
  235. /// <param name="targetResource">target object which defines the
  236. /// property</param>
  237. /// <param name="propertyName">name of the property whose value needs
  238. /// to be updated</param>
  239. /// <param name="resourceToBeRemoved">value of the property which
  240. /// needs to be removed</param>
  241. public void RemoveReferenceFromCollection(object targetResource,
  242. string propertyName, object resourceToBeRemoved)
  243. {
  244. throw new NotImplementedException();
  245. }
  246. /// <summary>
  247. /// Resets the value of the given resource to its default value
  248. /// </summary>
  249. /// <param name="resource">resource whose value needs to be reset
  250. /// </param>
  251. /// <returns>same resource with its value reset</returns>
  252. public object ResetResource(object resource)
  253. {
  254. throw new NotImplementedException();
  255. }
  256. /// <summary>
  257. /// Returns the actual instance of the resource represented by the
  258. /// given resource object
  259. /// </summary>
  260. /// <param name="resource">object representing the resource whose
  261. /// instance needs to be fetched</param>
  262. /// <returns>The actual instance of the resource represented by the
  263. /// given resource object</returns>
  264. public object ResolveResource(object resource)
  265. {
  266. return resource;
  267. }
  268. /// <summary>
  269. /// Saves all the pending changes made till now
  270. /// </summary>
  271. public void SaveChanges()
  272. {
  273. // Add the temp object into the local collection
  274. if (tempObj != null)
  275. {
  276. Type t = tempObj.GetType();
  277. if (t.Name == "Category")
  278. {
  279. SampleProjects.categories.Add((Category)tempObj);
  280. }
  281. else if (t.Name == "Project")
  282. {
  283. SampleProjects.projects.Add((Project)tempObj);
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// Sets the value of the given reference property on the target
  289. /// object
  290. /// </summary>
  291. /// <param name="targetResource">target object which defines the
  292. /// property</param>
  293. /// <param name="propertyName">name of the property whose value needs
  294. /// to be updated</param>
  295. /// <param name="propertyValue">value of the property</param>
  296. public void SetReference(object targetResource, string propertyName,
  297. object propertyValue)
  298. {
  299. ((IUpdatable)this).SetValue(targetResource, propertyName,
  300. propertyValue);
  301. }
  302. /// <summary>
  303. /// Sets the value of the given property on the target object
  304. /// </summary>
  305. /// <param name="targetResource">target object which defines the
  306. /// property</param>
  307. /// <param name="propertyName">name of the property whose value needs
  308. /// to be updated</param>
  309. /// <param name="propertyValue">value of the property</param>
  310. public void SetValue(object targetResource, string propertyName,
  311. object propertyValue)
  312. {
  313. // Get the resource object type
  314. Type t = targetResource.GetType();
  315. // Get the property to be updated
  316. PropertyInfo pi = t.GetProperty(propertyName);
  317. if (pi != null)
  318. {
  319. // Set the property value
  320. pi.SetValue(targetResource, propertyValue, null);
  321. }
  322. // Save the target object to temp added object
  323. tempObj = targetResource;
  324. }
  325. #endregion
  326. }
  327. #endregion
  328. }