PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 186 lines | 171 code | 15 blank | 0 comment | 31 complexity | e35d71dafb355d54b14b63c0a09e2922 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
  1. namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
  2. {
  3. using Microsoft.ServiceModel.DomainServices.Tools;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data.Linq;
  7. using System.Data.Objects;
  8. using System.Globalization;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Web.Hosting;
  12. internal class BusinessLogicModel : MarshalByRefObject, IRegisteredObject, IDisposable
  13. {
  14. private Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.BusinessLogicData _businessLogicData;
  15. private List<BusinessLogicContext> _contexts;
  16. private Action<string> _logger;
  17. public BusinessLogicModel() : this(delegate (string s) {
  18. })
  19. {
  20. }
  21. internal BusinessLogicModel(Action<string> logger)
  22. {
  23. if (logger == null)
  24. {
  25. throw new ArgumentNullException("logger");
  26. }
  27. this._logger = logger;
  28. }
  29. public void Dispose()
  30. {
  31. this._contexts = null;
  32. }
  33. public GeneratedCode GenerateBusinessLogicClass(ContextData contextData, string className, string namespaceName, string rootNamespace)
  34. {
  35. BusinessLogicContext context = this._contexts.SingleOrDefault<BusinessLogicContext>(c => c.ContextData.ID == contextData.ID);
  36. if (context == null)
  37. {
  38. return new GeneratedCode();
  39. }
  40. return context.GenerateBusinessLogicClass(this.Language, className, namespaceName, rootNamespace);
  41. }
  42. public GeneratedCode GenerateMetadataClasses(ContextData contextData, string rootNamespace, string optionalSuffix)
  43. {
  44. BusinessLogicContext context = this._contexts.Single<BusinessLogicContext>(c => c.ContextData.ID == contextData.ID);
  45. if (context == null)
  46. {
  47. return new GeneratedCode(string.Empty, new string[0]);
  48. }
  49. return context.GenerateMetadataClasses(this.Language, rootNamespace, optionalSuffix);
  50. }
  51. internal ContextData[] GetContextDataItems()
  52. {
  53. if (this._contexts == null)
  54. {
  55. List<Assembly> loadedAssemblies = new List<Assembly>();
  56. this._contexts = new List<BusinessLogicContext>();
  57. this.LoadAssemblies(this.BusinessLogicData.AssemblyPaths, loadedAssemblies);
  58. this.LoadAssemblies(this.BusinessLogicData.ReferenceAssemblyPaths, loadedAssemblies);
  59. List<Type> list2 = new List<Type>();
  60. HashSet<string> set = new HashSet<string>(this.BusinessLogicData.ContextTypeNames);
  61. foreach (string str in this.BusinessLogicData.ContextTypeNames)
  62. {
  63. Type item = Type.GetType(str, false);
  64. if (item != null)
  65. {
  66. list2.Add(item);
  67. set.Remove(item.AssemblyQualifiedName);
  68. }
  69. }
  70. foreach (Assembly assembly in loadedAssemblies)
  71. {
  72. if (set.Count > 0)
  73. {
  74. foreach (Type type2 in AssemblyUtilities.GetExportedTypes(assembly, this._logger).ToArray<Type>())
  75. {
  76. if ((set.Count > 0) && set.Contains(type2.AssemblyQualifiedName))
  77. {
  78. list2.Add(type2);
  79. set.Remove(type2.AssemblyQualifiedName);
  80. }
  81. }
  82. }
  83. }
  84. this._contexts.Add(new BusinessLogicContext(null, Resources.BusinessLogic_Class_Empty_Class_Name));
  85. foreach (Type type3 in list2)
  86. {
  87. if (typeof(DataContext).IsAssignableFrom(type3))
  88. {
  89. this._contexts.Add(new LinqToSqlContext(type3));
  90. }
  91. else if (typeof(ObjectContext).IsAssignableFrom(type3))
  92. {
  93. this._contexts.Add(new LinqToEntitiesContext(type3));
  94. }
  95. else
  96. {
  97. Type dbContextTypeReference = DbContextUtilities.GetDbContextTypeReference(type3);
  98. if ((dbContextTypeReference != null) && dbContextTypeReference.IsAssignableFrom(type3))
  99. {
  100. this._contexts.Add(new LinqToEntitiesDbContext(type3));
  101. }
  102. else
  103. {
  104. this._logger(string.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_InvalidContextType, new object[] { type3.FullName }));
  105. }
  106. }
  107. }
  108. foreach (string str2 in set)
  109. {
  110. this._logger(string.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_Failed_Type_Load, new object[] { str2 }));
  111. }
  112. }
  113. return (from blc in this._contexts select blc.ContextData).ToArray<ContextData>();
  114. }
  115. public EntityData[] GetEntityDataItemsForContext(ContextData contextData)
  116. {
  117. BusinessLogicContext context = this._contexts[contextData.ID];
  118. List<BusinessLogicEntity> list = (context == null) ? new List<BusinessLogicEntity>() : context.Entities.ToList<BusinessLogicEntity>();
  119. return (from ble in list select ble.EntityData).ToArray<EntityData>();
  120. }
  121. internal void Initialize(Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.BusinessLogicData businessLogicData)
  122. {
  123. this._businessLogicData = businessLogicData;
  124. if (businessLogicData.LinqToSqlPath != null)
  125. {
  126. LinqToSqlContext.OverrideAssemblyPath(businessLogicData.LinqToSqlPath);
  127. }
  128. }
  129. public bool IsMetadataGenerationRequired(ContextData contextData)
  130. {
  131. BusinessLogicContext context = this._contexts.SingleOrDefault<BusinessLogicContext>(c => c.ContextData.ID == contextData.ID);
  132. if (context == null)
  133. {
  134. return false;
  135. }
  136. return context.NeedToGenerateMetadataClasses;
  137. }
  138. private void LoadAssemblies(string[] assembliesToLoad, IList<Assembly> loadedAssemblies)
  139. {
  140. foreach (string str in assembliesToLoad)
  141. {
  142. Assembly item = AssemblyUtilities.LoadAssembly(str, this._logger);
  143. if (item != null)
  144. {
  145. loadedAssemblies.Add(item);
  146. }
  147. }
  148. }
  149. void IRegisteredObject.Stop(bool immediate)
  150. {
  151. }
  152. private Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.BusinessLogicData BusinessLogicData
  153. {
  154. get
  155. {
  156. if (this._businessLogicData == null)
  157. {
  158. throw new InvalidOperationException(Resources.BusinessLogicClass_Not_Initialized);
  159. }
  160. return this._businessLogicData;
  161. }
  162. }
  163. internal string Language
  164. {
  165. get
  166. {
  167. return this.BusinessLogicData.Language;
  168. }
  169. }
  170. }
  171. }