Advertisement
Guest User

Untitled

a guest
Feb 24th, 2016
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.67 KB | None | 0 0
  1. <#@ template debug="true" hostspecific="true" language="C#" #>
  2. <#@ include file="EF.Utility.CS.ttinclude"#>
  3. <#@ import namespace="System.IO" #>
  4. <#@ output extension=".cs" #>
  5. <#
  6. if(Errors.HasErrors)
  7. {
  8. return String.Empty;
  9. }
  10.  
  11. CodeGenerationTools code = new CodeGenerationTools(this)
  12. {FullyQualifySystemTypes = true, CamelCaseFields = false};
  13. MetadataLoader loader = new MetadataLoader(this);
  14.  
  15. string open = "<";
  16. string close = ">";
  17. string SourceCsdlPath = FindEDMXFileName();
  18. ReferenceCsdlPaths = new string[] {};
  19. string namespaceName = code.VsNamespaceSuggestion();
  20. ItemCollection = loader.CreateEdmItemCollection
  21. (SourceCsdlPath, ReferenceCsdlPaths.ToArray());
  22. EntityContainer container = ItemCollection.GetItems<EntityContainer>().FirstOrDefault();
  23. #>
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Linq;
  27. using System.Text;
  28. using System.Linq.Expressions;
  29. using System.Data.Entity;
  30. using System.Data.Entity.Validation;
  31. using System.Data.Entity.Infrastructure;
  32. using System.Data;
  33. using Microsoft.Practices.Unity;
  34. using!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! add your domain or entity model namespace here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  35. using Moq;
  36.  
  37. namespace <#=namespaceName#>
  38. {
  39. public interface IContext : IDisposable
  40. {
  41. DbSet<TEntity> Set<TEntity>() where TEntity : class;
  42. DbSet Set(Type entityType);
  43. int SaveChanges();
  44. IEnumerable<DbEntityValidationResult> GetValidationErrors();
  45. DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
  46. DbEntityEntry Entry(object entity);
  47. string ConnectionString { get; set; }
  48. bool AutoDetectChangedEnabled { get; set; }
  49. void ExecuteSqlCommand(string p, params object[] o);
  50. void ExecuteSqlCommand(string p);
  51. string Server { get; set; }
  52. bool ProxyCreationEnabled { get; set; }
  53. bool LazyLoadingEnabled { get; set; }
  54. }
  55.  
  56. public interface IRepository<T> where T : class
  57. {
  58. IContext oIContext { get; set; }
  59. IQueryable<T> Gets();
  60. IQueryable<T> Gets(Func<T, bool> filter);
  61. T First(Func<T, bool> filter);
  62. T Last(Func<T, bool> filter);
  63. void Insert(T entity);
  64. void Update(T entity);
  65. void Delete(T entity);
  66. }
  67.  
  68. public abstract class ARepository<T> : IRepository<T> where T : class
  69. {
  70. protected IDbSet<T> _objectSet;
  71. public IContext oIContext { get; set; }
  72.  
  73. public ARepository(IContext context)
  74. {
  75. _objectSet = context.Set<T>();
  76. oIContext = context;
  77. }
  78.  
  79. public IQueryable<T> Gets()
  80. {
  81. return _objectSet.AsQueryable<T>();
  82. }
  83.  
  84. public IQueryable<T> Gets(Func<T, bool> filter)
  85. {
  86. return _objectSet.Where(filter).AsQueryable<T>();
  87. }
  88.  
  89. public T First(Func<T, bool> filter)
  90. {
  91. return _objectSet.FirstOrDefault(filter);
  92. }
  93.  
  94. public T Last(Func<T, bool> filter)
  95. {
  96. return _objectSet.LastOrDefault(filter);
  97. }
  98.  
  99. public void Insert(T entity)
  100. {
  101. oIContext.Entry(entity).State = EntityState.Added;
  102. }
  103.  
  104. public void Update(T entity)
  105. {
  106. oIContext.Entry(entity).State = EntityState.Modified;
  107. }
  108.  
  109. public void Delete(T entity)
  110. {
  111. oIContext.Entry(entity).State = EntityState.Detached;
  112. oIContext.Entry(entity).State = EntityState.Deleted;
  113. }
  114.  
  115. }
  116.  
  117. #region Repositories Interfaces
  118. <#
  119. foreach (EntityType entity in
  120. ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
  121. {
  122. #>
  123. public partial interface IRepository<#= entity.Name #>: IRepository<#=open#><#=entity.Name#><#=close#>{}
  124. <#
  125. }
  126. #>
  127. #endregion
  128.  
  129. #region Repositories Implemented
  130. <#
  131. foreach (EntityType entity in
  132. ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
  133. {
  134. #>
  135. public partial class Repository<#= entity.Name #>: ARepository<#=open#><#=entity.Name#><#=close#>, IRepository<#=entity.Name#> { public Repository<#= entity.Name #>(IContext context) : base(context) { } }
  136. <#
  137. }
  138. #>
  139. #endregion
  140.  
  141. public interface IFactoryUnitOfWork
  142. {
  143. IUnitOfWork Resolve();
  144. void Dispose();
  145. }
  146.  
  147. public class FactoryUnitOfWork : IFactoryUnitOfWork
  148. {
  149. private readonly IUnityContainer oUC;
  150. private IUnitOfWork oUoW;
  151.  
  152. public FactoryUnitOfWork (IUnityContainer oUC)
  153. {
  154. this.oUC = oUC;
  155. }
  156.  
  157. public IUnitOfWork Resolve()
  158. {
  159. oUoW = oUC.Resolve<IUnitOfWork>();
  160. return oUoW;
  161. }
  162.  
  163. public void Dispose()
  164. {
  165. if (oUoW != null)
  166. {
  167. oUoW.Dispose();
  168. }
  169. }
  170.  
  171. }
  172.  
  173. public interface IUnitOfWork : IDisposable
  174. {
  175. #region Methods
  176.  
  177. <#
  178. foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
  179. {
  180. #>
  181. IRepository<#= set.ElementType.Name #> oRepository<#= set.Name #> { get; }
  182. <#
  183. }
  184. #>
  185. void Commit();
  186. void Dispose();
  187.  
  188. #endregion
  189. }
  190.  
  191. public partial class UnitOfWork : IUnitOfWork
  192. {
  193. #region Members
  194.  
  195. private readonly IContext _context;
  196. <#
  197. foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
  198. {
  199. #>
  200. public IRepository<#= set.ElementType.Name #> oRepository<#= set.Name #> { get;set;}
  201. <#
  202. }
  203. #>
  204. #endregion
  205.  
  206. #region Ctor
  207.  
  208. public UnitOfWork(IContext context
  209. <#
  210. foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
  211. {
  212. #>
  213. , IRepository<#= set.ElementType.Name #> oRepository<#= set.Name #>
  214. <#
  215. }
  216. #> )
  217. {
  218. if (context == null)
  219. {
  220. throw new ArgumentNullException("context wasn't supplied");
  221. }
  222.  
  223. context.LazyLoadingEnabled = false;
  224. context.ProxyCreationEnabled = false;
  225. <#
  226. foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
  227. {
  228. #>
  229. this.oRepository<#= set.Name #> = oRepository<#= set.Name #>;
  230. <#
  231. }
  232. #>
  233.  
  234. <#
  235. foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
  236. {
  237. #>
  238. this.oRepository<#= set.Name #>.oIContext = context;
  239. <#
  240. }
  241. #>
  242.  
  243. _context = context;
  244. }
  245.  
  246. #endregion
  247.  
  248.  
  249. public void Commit()
  250. {
  251. try
  252. {
  253. _context.SaveChanges();
  254. }
  255. catch (DbEntityValidationException db)
  256. {
  257. var sb = new StringBuilder();
  258. foreach (var excep in db.EntityValidationErrors)
  259. {
  260. foreach (var ve in excep.ValidationErrors)
  261. {
  262. sb.Append(ve.PropertyName);
  263. sb.Append("=");
  264. sb.AppendLine(ve.ErrorMessage);
  265. }
  266. }
  267. throw new DbEntityValidationException(sb.ToString(), db);
  268. }
  269. }
  270.  
  271. private bool bDispose;
  272. public void Dispose()
  273. {
  274. if (!bDispose)
  275. {
  276. _context.Dispose();
  277. bDispose = true;
  278. }
  279. }
  280.  
  281. }
  282.  
  283. public static class CompositionRoot
  284. {
  285. public static void Init(IUnityContainer oIUC)
  286. {
  287. oIUC.RegisterType<IUnitOfWork, UnitOfWork>();
  288. oIUC.RegisterType<IFactoryUnitOfWork, FactoryUnitOfWork>();
  289.  
  290. oIUC.RegisterType<#= open#>IContext,<#= container #><#= close #>();
  291. <#
  292. foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
  293. {
  294. #>
  295. oIUC.RegisterType<#= open#>IRepository<#= set.Name #>, Repository<#= set.Name #><#= close#>();
  296. <#
  297. }
  298. #>
  299. }
  300. }
  301.  
  302. public static class CompositionRootMock
  303. {
  304. public static void Init(IUnityContainer oIUC)
  305. {
  306. var mockUnitOfWork = new Mock<IUnitOfWork>();
  307. oIUC.RegisterInstance<IUnitOfWork>(mockUnitOfWork.Object);
  308.  
  309. var mockFactoryUnitOfWork = new Mock<IFactoryUnitOfWork>();
  310. oIUC.RegisterInstance<IFactoryUnitOfWork>(mockFactoryUnitOfWork.Object);
  311.  
  312. var mockContext = new Mock<IContext>();
  313. oIUC.RegisterInstance<IContext>(mockContext.Object);
  314. <#
  315. foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
  316. {
  317. #>
  318. var oMockRepository<#= set.Name #> = new Mock<#= open#>IRepository<#= set.Name #><#= close#>();
  319. oIUC.RegisterInstance<#= open#>IRepository<#= set.Name #><#= close#>(oMockRepository<#= set.Name #>.Object);
  320. <#
  321. }
  322. #>
  323. }
  324. }
  325.  
  326. public partial class <#= container #> : DbContext, IContext
  327. {
  328. public string ConnectionString
  329. {
  330. get
  331. {
  332. return this.Database.Connection.ConnectionString;
  333. }
  334. set
  335. {
  336. this.Database.Connection.ConnectionString = value;
  337. }
  338. }
  339.  
  340. public bool AutoDetectChangedEnabled
  341. {
  342. get
  343. {
  344. return this.Configuration.AutoDetectChangesEnabled;
  345. }
  346. set
  347. {
  348. this.Configuration.AutoDetectChangesEnabled = value;
  349. }
  350. }
  351.  
  352. public void ExecuteSqlCommand(string p, params object[] o)
  353. {
  354. this.Database.ExecuteSqlCommand(p, o);
  355. }
  356.  
  357. public void ExecuteSqlCommand(string p)
  358. {
  359. this.Database.ExecuteSqlCommand(p);
  360. }
  361.  
  362. public string Server
  363. {
  364. get
  365. {
  366. throw new NotImplementedException();
  367. }
  368. set
  369. {
  370. throw new NotImplementedException();
  371. }
  372. }
  373.  
  374. public bool ProxyCreationEnabled
  375. {
  376. get
  377. {
  378. return this.Configuration.ProxyCreationEnabled;
  379. }
  380. set
  381. {
  382. this.Configuration.ProxyCreationEnabled = value;
  383. }
  384. }
  385.  
  386. public bool LazyLoadingEnabled
  387. {
  388. get
  389. {
  390. return this.Configuration.LazyLoadingEnabled;
  391. }
  392. set
  393. {
  394. this.Configuration.LazyLoadingEnabled = value;
  395. }
  396. }
  397. }
  398.  
  399.  
  400. }
  401. <#+
  402. public string SourceCsdlPath{ get; set; }
  403. public EdmItemCollection ItemCollection{ get; set; }
  404. public IEnumerable<string> ReferenceCsdlPaths{ get; set; }
  405.  
  406. string FindEDMXFileName()
  407. {
  408. string[] entityFrameworkFiles = Directory.GetFiles
  409. (Host.ResolvePath(string.Empty), "*.edmx");
  410. if(entityFrameworkFiles.Length > 0)
  411. {
  412. return entityFrameworkFiles[0];
  413. }
  414.  
  415. return string.Empty;
  416. }
  417. #>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement