PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C# | 364 lines | 340 code | 24 blank | 0 comment | 56 complexity | 6b95c22869e7a3b07c8de61d7cc8462c MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
  1. namespace Microsoft.VisualStudio.ServiceModel.DomainServices.Tools
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Globalization;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Runtime.CompilerServices;
  10. using System.Runtime.Remoting;
  11. using System.Threading;
  12. using System.Web.Compilation;
  13. internal class BusinessLogicViewModel : INotifyPropertyChanged, IDisposable
  14. {
  15. private string _assemblyName;
  16. private Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.BusinessLogicModel _businessLogicModel;
  17. private string _className;
  18. private System.Web.Compilation.ClientBuildManager _clientBuildManager;
  19. private List<Type> _contextTypes;
  20. private List<ContextViewModel> _contextViewModels;
  21. private ContextViewModel _currentContextViewModel;
  22. private bool _generateMetadataClasses;
  23. private IVsHelp _help;
  24. private string _language;
  25. private string _projectDirectory;
  26. private string _rootNamespace;
  27. public event PropertyChangedEventHandler PropertyChanged;
  28. public BusinessLogicViewModel(string projectDirectory, string className, string language, string rootNamespace, string assemblyName, IEnumerable<Type> contextTypes, IVsHelp help)
  29. {
  30. if (string.IsNullOrEmpty(projectDirectory))
  31. {
  32. throw new ArgumentNullException("projectDirectory");
  33. }
  34. if (string.IsNullOrEmpty(className))
  35. {
  36. throw new ArgumentNullException("className");
  37. }
  38. if (string.IsNullOrEmpty(language))
  39. {
  40. throw new ArgumentNullException("language");
  41. }
  42. if (contextTypes == null)
  43. {
  44. throw new ArgumentNullException("contextTypes");
  45. }
  46. if (string.IsNullOrEmpty(assemblyName))
  47. {
  48. throw new ArgumentNullException("assemblyName");
  49. }
  50. this._projectDirectory = projectDirectory;
  51. this._className = className;
  52. this._language = language;
  53. this._rootNamespace = rootNamespace;
  54. this._assemblyName = assemblyName;
  55. this._contextTypes = new List<Type>(contextTypes);
  56. this._help = help;
  57. }
  58. private void CurrentContextPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
  59. {
  60. string propertyName = propertyChangedEventArgs.PropertyName;
  61. if (string.Equals(propertyName, "IsMetadataClassGenerationAllowed", StringComparison.OrdinalIgnoreCase))
  62. {
  63. this.RaisePropertyChanged(propertyName);
  64. }
  65. }
  66. public void DisplayHelp()
  67. {
  68. if (this._help != null)
  69. {
  70. this._help.DisplayTopicFromF1Keyword("DomainServiceWizard.UI");
  71. }
  72. }
  73. public void Dispose()
  74. {
  75. GC.SuppressFinalize(this);
  76. if (this._businessLogicModel != null)
  77. {
  78. try
  79. {
  80. this._businessLogicModel.Dispose();
  81. }
  82. catch (RemotingException)
  83. {
  84. }
  85. this._businessLogicModel = null;
  86. }
  87. IDisposable disposable = this._clientBuildManager;
  88. if (disposable != null)
  89. {
  90. disposable.Dispose();
  91. }
  92. this._clientBuildManager = null;
  93. }
  94. public GeneratedCode GenerateBusinessLogicClass(string namespaceName)
  95. {
  96. ContextViewModel currentContextViewModel = this.CurrentContextViewModel;
  97. if (currentContextViewModel != null)
  98. {
  99. return this.BusinessLogicModel.GenerateBusinessLogicClass(currentContextViewModel.ContextData, this.ClassName, namespaceName, this.RootNamespace);
  100. }
  101. return new GeneratedCode();
  102. }
  103. public GeneratedCode GenerateMetadataClasses(string optionalSuffix)
  104. {
  105. ContextViewModel currentContextViewModel = this.CurrentContextViewModel;
  106. if (currentContextViewModel != null)
  107. {
  108. return this.BusinessLogicModel.GenerateMetadataClasses(currentContextViewModel.ContextData, this.RootNamespace, optionalSuffix);
  109. }
  110. return new GeneratedCode(string.Empty, new string[0]);
  111. }
  112. private void RaisePropertyChanged(string propertyName)
  113. {
  114. if (this.PropertyChanged != null)
  115. {
  116. this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  117. }
  118. }
  119. private void ReportException(Exception exception)
  120. {
  121. Action<Exception> exceptionHandler = this.ExceptionHandler;
  122. if (exceptionHandler == null)
  123. {
  124. throw exception;
  125. }
  126. exceptionHandler(exception);
  127. }
  128. private void ValidateClassName(string className)
  129. {
  130. using (CodeGenContext context = new CodeGenContext(this.Language, this.RootNamespace))
  131. {
  132. if (string.IsNullOrEmpty(className) || !context.IsValidIdentifier(className))
  133. {
  134. this.ReportException(new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.BusinessLogicClass_Error_Invalid_ClassName, new object[] { className })));
  135. }
  136. }
  137. }
  138. internal string AssemblyName
  139. {
  140. get
  141. {
  142. return this._assemblyName;
  143. }
  144. }
  145. private Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.BusinessLogicModel BusinessLogicModel
  146. {
  147. get
  148. {
  149. if (this._businessLogicModel == null)
  150. {
  151. this._businessLogicModel = (Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.BusinessLogicModel) this.ClientBuildManager.CreateObject(typeof(Microsoft.VisualStudio.ServiceModel.DomainServices.Tools.BusinessLogicModel), false);
  152. HashSet<string> source = new HashSet<string>();
  153. HashSet<string> set2 = new HashSet<string>();
  154. List<string> list = new List<string>();
  155. foreach (Type type in this._contextTypes)
  156. {
  157. if (CodeGenUtilities.IsValidGenericTypeParam(type))
  158. {
  159. source.Add(type.Assembly.Location);
  160. list.Add(type.AssemblyQualifiedName);
  161. Action<string> logger = delegate (string s) {
  162. };
  163. foreach (Assembly assembly in AssemblyUtilities.GetReferencedAssemblies(type.Assembly, logger))
  164. {
  165. if (!source.Contains(assembly.Location))
  166. {
  167. set2.Add(assembly.Location);
  168. }
  169. }
  170. }
  171. }
  172. BusinessLogicData data2 = new BusinessLogicData {
  173. Language = this.Language,
  174. AssemblyPaths = source.ToArray<string>(),
  175. ReferenceAssemblyPaths = set2.ToArray<string>(),
  176. ContextTypeNames = list.ToArray(),
  177. LinqToSqlPath = LinqToSqlContext.LinqToSqlDomainServiceAssemblyPath
  178. };
  179. BusinessLogicData businessLogicData = data2;
  180. this._businessLogicModel.Initialize(businessLogicData);
  181. }
  182. return this._businessLogicModel;
  183. }
  184. }
  185. public string ClassName
  186. {
  187. get
  188. {
  189. return this._className;
  190. }
  191. set
  192. {
  193. this.ValidateClassName(value);
  194. if (this._className != value)
  195. {
  196. this._className = value;
  197. this.RaisePropertyChanged("ClassName");
  198. }
  199. }
  200. }
  201. private System.Web.Compilation.ClientBuildManager ClientBuildManager
  202. {
  203. get
  204. {
  205. if (this._clientBuildManager == null)
  206. {
  207. this._clientBuildManager = new System.Web.Compilation.ClientBuildManager("/", this.ProjectDirectory, null, null);
  208. }
  209. return this._clientBuildManager;
  210. }
  211. }
  212. public IList<ContextViewModel> ContextViewModels
  213. {
  214. get
  215. {
  216. if (this._contextViewModels == null)
  217. {
  218. this._contextViewModels = new List<ContextViewModel>();
  219. foreach (ContextData data in from c in this.BusinessLogicModel.GetContextDataItems()
  220. orderby c.Name
  221. select c)
  222. {
  223. this._contextViewModels.Add(new ContextViewModel(this.BusinessLogicModel, data));
  224. }
  225. }
  226. return this._contextViewModels;
  227. }
  228. }
  229. public ContextViewModel CurrentContextViewModel
  230. {
  231. get
  232. {
  233. if (this._currentContextViewModel == null)
  234. {
  235. IList<ContextViewModel> contextViewModels = this.ContextViewModels;
  236. if (contextViewModels.Count == 1)
  237. {
  238. this.CurrentContextViewModel = contextViewModels[0];
  239. }
  240. else if (contextViewModels.Count > 1)
  241. {
  242. this.CurrentContextViewModel = contextViewModels[1];
  243. }
  244. }
  245. return this._currentContextViewModel;
  246. }
  247. set
  248. {
  249. if (value != this._currentContextViewModel)
  250. {
  251. if (this._currentContextViewModel != null)
  252. {
  253. this._currentContextViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.CurrentContextPropertyChanged);
  254. }
  255. this._currentContextViewModel = value;
  256. if (this._currentContextViewModel != null)
  257. {
  258. this._currentContextViewModel.PropertyChanged += new PropertyChangedEventHandler(this.CurrentContextPropertyChanged);
  259. }
  260. this.RaisePropertyChanged("CurrentContextViewModel");
  261. this.RaisePropertyChanged("IsMetadataClassGenerationRequested");
  262. this.RaisePropertyChanged("IsMetadataClassGenerationAllowed");
  263. }
  264. }
  265. }
  266. public Action<Exception> ExceptionHandler { get; set; }
  267. public bool IsMetadataClassGenerationAllowed
  268. {
  269. get
  270. {
  271. ContextViewModel currentContextViewModel = this.CurrentContextViewModel;
  272. if (currentContextViewModel == null)
  273. {
  274. return false;
  275. }
  276. if (!this.BusinessLogicModel.IsMetadataGenerationRequired(currentContextViewModel.ContextData))
  277. {
  278. return false;
  279. }
  280. IEnumerable<EntityViewModel> source = from e in currentContextViewModel.Entities
  281. where e.IsIncluded
  282. select e;
  283. if (!source.Any<EntityViewModel>())
  284. {
  285. return false;
  286. }
  287. foreach (EntityViewModel model2 in source)
  288. {
  289. string assemblyName = model2.EntityData.AssemblyName;
  290. if (!string.Equals(this.AssemblyName, assemblyName, StringComparison.OrdinalIgnoreCase))
  291. {
  292. return false;
  293. }
  294. }
  295. return true;
  296. }
  297. }
  298. public bool IsMetadataClassGenerationRequested
  299. {
  300. get
  301. {
  302. return (this._generateMetadataClasses && this.IsMetadataClassGenerationAllowed);
  303. }
  304. set
  305. {
  306. if (!((this.CurrentContextViewModel != null) && this.IsMetadataClassGenerationAllowed))
  307. {
  308. value = false;
  309. }
  310. if (this._generateMetadataClasses != value)
  311. {
  312. this._generateMetadataClasses = value;
  313. this.RaisePropertyChanged("IsMetadataClassGenerationRequested");
  314. this.RaisePropertyChanged("IsMetadataClassGenerationAllowed");
  315. }
  316. }
  317. }
  318. internal string Language
  319. {
  320. get
  321. {
  322. return this._language;
  323. }
  324. }
  325. public string ProjectDirectory
  326. {
  327. get
  328. {
  329. return this._projectDirectory;
  330. }
  331. }
  332. internal string RootNamespace
  333. {
  334. get
  335. {
  336. return this._rootNamespace;
  337. }
  338. }
  339. }
  340. }