PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/SolutionFramework/Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.10.0/Microsoft/VisualStudio/ServiceModel/DomainServices/Tools/LinqToSqlContext.cs

#
C# | 261 lines | 245 code | 16 blank | 0 comment | 26 complexity | 4398a35f62043f3bea49c3c4f3ea1548 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
  1. namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
  2. {
  3. using Microsoft.Win32;
  4. using System;
  5. using System.CodeDom;
  6. using System.Collections.Generic;
  7. using System.Data.Linq;
  8. using System.Data.Linq.Mapping;
  9. using System.IO;
  10. using System.Reflection;
  11. using System.Runtime.CompilerServices;
  12. using System.Linq;
  13. internal class LinqToSqlContext : BusinessLogicContext
  14. {
  15. private System.Data.Linq.DataContext _dataContext;
  16. private System.Data.Linq.Mapping.MetaModel _metaModel;
  17. private const string AssemblyPathKeyValueName = "AssemblyPath";
  18. private const string EnableDataContextKeyValueName = "EnableDataContext";
  19. private static string linqToSqlDomainServiceAssemblyPath;
  20. private const string ToolkitDomainServiceWizardRegKey = @"SOFTWARE\Microsoft\WCFRIAServices\v1.0\Toolkit\DomainServiceWizard";
  21. private static bool EnableDataContextTypes_TestOverride_k__BackingField;
  22. public LinqToSqlContext(Type contextType) : base(contextType, contextType.Name)
  23. {
  24. }
  25. protected override CodeTypeDeclaration CreateBusinessLogicClass(CodeGenContext codeGenContext, CodeNamespace codeNamespace, string className)
  26. {
  27. if (linqToSqlDomainServiceAssemblyPath == null)
  28. {
  29. return null;
  30. }
  31. foreach (string str in BusinessLogicClassConstants.LinqToSqlImports)
  32. {
  33. codeNamespace.Imports.Add(new CodeNamespaceImport(str));
  34. }
  35. if (base.ContextType.Namespace != codeNamespace.Name)
  36. {
  37. codeNamespace.Imports.Add(new CodeNamespaceImport(base.ContextType.Namespace));
  38. }
  39. codeGenContext.AddReference(typeof(System.Data.Linq.DataContext).Assembly.FullName);
  40. codeGenContext.AddReference(linqToSqlDomainServiceAssemblyPath);
  41. CodeTypeDeclaration declaration = CodeGenUtilities.CreateTypeDeclaration(className, codeNamespace.Name);
  42. CodeTypeReference reference = new CodeTypeReference(BusinessLogicClassConstants.LinqToSqlDomainServiceTypeName, new CodeTypeReference[] { new CodeTypeReference(base.ContextType.Name) });
  43. declaration.BaseTypes.Add(reference);
  44. return declaration;
  45. }
  46. protected override IEnumerable<BusinessLogicEntity> CreateEntities()
  47. {
  48. IEnumerable<MetaTable> enumerable = from t in this.MetaModel.GetTables()
  49. where t.RowType.IsEntity
  50. select t;
  51. List<BusinessLogicEntity> list = new List<BusinessLogicEntity>();
  52. foreach (MetaTable table in enumerable)
  53. {
  54. MetaType rowType = table.RowType;
  55. list.Add(new LinqToSqlEntity(this, rowType));
  56. }
  57. return list;
  58. }
  59. protected override void GenerateDeleteMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
  60. {
  61. string name = CodeGenUtilities.MakeLegalParameterName(entity.Name);
  62. CodeMemberMethod method = new CodeMemberMethod();
  63. businessLogicClass.Members.Add(method);
  64. LinqToSqlEntity entity2 = (LinqToSqlEntity) entity;
  65. method.Name = "Delete" + CodeGenUtilities.MakeLegalEntityName(entity.Name);
  66. method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  67. method.Parameters.Add(new CodeParameterDeclarationExpression(entity.ClrType.Name, name));
  68. CodeExpression targetObject = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
  69. CodeExpression expression2 = new CodePropertyReferenceExpression(targetObject, entity2.TablePropertyName);
  70. CodeMethodInvokeExpression expression3 = new CodeMethodInvokeExpression(expression2, "Attach", new CodeExpression[] { new CodeArgumentReferenceExpression(name) });
  71. method.Statements.Add(expression3);
  72. CodeMethodInvokeExpression expression4 = new CodeMethodInvokeExpression(expression2, "DeleteOnSubmit", new CodeExpression[] { new CodeArgumentReferenceExpression(name) });
  73. method.Statements.Add(expression4);
  74. }
  75. protected override void GenerateInsertMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
  76. {
  77. CodeMemberMethod method = new CodeMemberMethod();
  78. businessLogicClass.Members.Add(method);
  79. string name = CodeGenUtilities.MakeLegalParameterName(entity.Name);
  80. LinqToSqlEntity entity2 = (LinqToSqlEntity) entity;
  81. method.Name = "Insert" + CodeGenUtilities.MakeLegalEntityName(entity.Name);
  82. method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  83. method.Parameters.Add(new CodeParameterDeclarationExpression(entity.ClrType.Name, name));
  84. CodeExpression targetObject = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
  85. CodeExpression expression2 = new CodePropertyReferenceExpression(targetObject, entity2.TablePropertyName);
  86. CodeMethodInvokeExpression expression3 = new CodeMethodInvokeExpression(expression2, "InsertOnSubmit", new CodeExpression[] { new CodeArgumentReferenceExpression(name) });
  87. method.Statements.Add(expression3);
  88. }
  89. protected override CodeMemberMethod GenerateSelectMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
  90. {
  91. CodeMemberMethod method = null;
  92. LinqToSqlEntity entity2 = entity as LinqToSqlEntity;
  93. if ((entity2 != null) && (entity2.TablePropertyName != null))
  94. {
  95. method = new CodeMemberMethod();
  96. businessLogicClass.Members.Add(method);
  97. method.Comments.Add(new CodeCommentStatement(Resources.BusinessLogicClass_Query_Method_Remarks, false));
  98. method.Name = "Get" + CodeGenUtilities.MakeLegalEntityName(entity2.TablePropertyName);
  99. method.ReturnType = new CodeTypeReference("IQueryable", new CodeTypeReference[] { new CodeTypeReference(entity.Name) });
  100. method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  101. CodeExpression targetObject = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
  102. CodeExpression expression = new CodePropertyReferenceExpression(targetObject, entity2.TablePropertyName);
  103. CodeMethodReturnStatement statement = new CodeMethodReturnStatement(expression);
  104. method.Statements.Add(statement);
  105. }
  106. return method;
  107. }
  108. protected override void GenerateUpdateMethod(CodeGenContext codeGenContext, CodeTypeDeclaration businessLogicClass, BusinessLogicEntity entity)
  109. {
  110. string name = "current" + entity.ClrType.Name;
  111. CodeMemberMethod method = new CodeMemberMethod();
  112. businessLogicClass.Members.Add(method);
  113. LinqToSqlEntity entity2 = (LinqToSqlEntity) entity;
  114. method.Name = "Update" + CodeGenUtilities.MakeLegalEntityName(entity.Name);
  115. method.Attributes = MemberAttributes.Public | MemberAttributes.Final;
  116. method.Parameters.Add(new CodeParameterDeclarationExpression(entity.ClrType.Name, name));
  117. if (!entity2.HasTimestampMember)
  118. {
  119. CodeExpression targetObject = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "ChangeSet");
  120. CodeMethodReferenceExpression expression2 = new CodeMethodReferenceExpression(targetObject, "GetOriginal");
  121. CodeMethodInvokeExpression expression3 = new CodeMethodInvokeExpression(expression2, new CodeExpression[] { new CodeArgumentReferenceExpression(name) });
  122. CodeExpression expression4 = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
  123. CodeExpression expression5 = new CodePropertyReferenceExpression(expression4, entity2.TablePropertyName);
  124. CodeMethodInvokeExpression expression6 = new CodeMethodInvokeExpression(expression5, "Attach", new CodeExpression[] { new CodeArgumentReferenceExpression(name), expression3 });
  125. method.Statements.Add(expression6);
  126. }
  127. else
  128. {
  129. CodeExpression expression7 = new CodePropertyReferenceExpression(new CodeThisReferenceExpression(), "DataContext");
  130. CodeExpression expression8 = new CodePropertyReferenceExpression(expression7, entity2.TablePropertyName);
  131. CodeMethodInvokeExpression expression9 = new CodeMethodInvokeExpression(expression8, "Attach", new CodeExpression[] { new CodeArgumentReferenceExpression(name), new CodePrimitiveExpression(true) });
  132. method.Statements.Add(expression9);
  133. }
  134. }
  135. internal static void OverrideAssemblyPath(string assemblyPath)
  136. {
  137. EnableDataContextTypes_TestOverride = !string.IsNullOrEmpty(assemblyPath);
  138. linqToSqlDomainServiceAssemblyPath = assemblyPath;
  139. }
  140. public override string DataAccessLayerName
  141. {
  142. get
  143. {
  144. return Resources.BusinessLogicClass_LinqToSql;
  145. }
  146. }
  147. private System.Data.Linq.DataContext DataContext
  148. {
  149. get
  150. {
  151. if (this._dataContext == null)
  152. {
  153. try
  154. {
  155. this._dataContext = (System.Data.Linq.DataContext) Activator.CreateInstance(base.ContextType, new object[] { string.Empty });
  156. }
  157. catch (TargetInvocationException exception)
  158. {
  159. if (exception.InnerException != null)
  160. {
  161. throw exception.InnerException;
  162. }
  163. throw;
  164. }
  165. }
  166. return this._dataContext;
  167. }
  168. }
  169. public static bool EnableDataContextTypes
  170. {
  171. get
  172. {
  173. if (EnableDataContextTypes_TestOverride)
  174. {
  175. return true;
  176. }
  177. linqToSqlDomainServiceAssemblyPath = null;
  178. RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\WCFRIAServices\v1.0\Toolkit\DomainServiceWizard");
  179. if (key != null)
  180. {
  181. object obj2 = key.GetValue("EnableDataContext", string.Empty);
  182. int result = 0;
  183. if (((obj2 != null) && int.TryParse(obj2.ToString(), out result)) && (result == 1))
  184. {
  185. string path = key.GetValue("AssemblyPath", null) as string;
  186. if (File.Exists(path))
  187. {
  188. try
  189. {
  190. Assembly.LoadFrom(path);
  191. linqToSqlDomainServiceAssemblyPath = path;
  192. }
  193. catch (FileNotFoundException)
  194. {
  195. }
  196. catch (BadImageFormatException)
  197. {
  198. }
  199. catch (FileLoadException)
  200. {
  201. }
  202. }
  203. }
  204. key.Close();
  205. }
  206. return (linqToSqlDomainServiceAssemblyPath != null);
  207. }
  208. }
  209. private static bool EnableDataContextTypes_TestOverride
  210. {
  211. [CompilerGenerated]
  212. get
  213. {
  214. return EnableDataContextTypes_TestOverride_k__BackingField;
  215. }
  216. [CompilerGenerated]
  217. set
  218. {
  219. EnableDataContextTypes_TestOverride_k__BackingField = value;
  220. }
  221. }
  222. internal static string LinqToSqlDomainServiceAssemblyPath
  223. {
  224. get
  225. {
  226. if (!EnableDataContextTypes)
  227. {
  228. return null;
  229. }
  230. return linqToSqlDomainServiceAssemblyPath;
  231. }
  232. }
  233. private System.Data.Linq.Mapping.MetaModel MetaModel
  234. {
  235. get
  236. {
  237. if (this._metaModel == null)
  238. {
  239. this._metaModel = this.DataContext.Mapping;
  240. }
  241. return this._metaModel;
  242. }
  243. }
  244. }
  245. }