Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#@ template debug="true" hostspecific="true" language="C#" #>
- <#@ include file="EF.Utility.CS.ttinclude"#>
- <#@ import namespace="System.IO" #>
- <#@ output extension=".cs" #>
- <#
- if(Errors.HasErrors)
- {
- return String.Empty;
- }
- CodeGenerationTools code = new CodeGenerationTools(this)
- {FullyQualifySystemTypes = true, CamelCaseFields = false};
- MetadataLoader loader = new MetadataLoader(this);
- string open = "<";
- string close = ">";
- string SourceCsdlPath = FindEDMXFileName();
- ReferenceCsdlPaths = new string[] {};
- string namespaceName = code.VsNamespaceSuggestion();
- ItemCollection = loader.CreateEdmItemCollection
- (SourceCsdlPath, ReferenceCsdlPaths.ToArray());
- EntityContainer container = ItemCollection.GetItems<EntityContainer>().FirstOrDefault();
- #>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Linq.Expressions;
- using System.Data.Entity;
- using System.Data.Entity.Validation;
- using System.Data.Entity.Infrastructure;
- using System.Data;
- using Microsoft.Practices.Unity;
- using!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! add your domain or entity model namespace here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- using Moq;
- namespace <#=namespaceName#>
- {
- public interface IContext : IDisposable
- {
- DbSet<TEntity> Set<TEntity>() where TEntity : class;
- DbSet Set(Type entityType);
- int SaveChanges();
- IEnumerable<DbEntityValidationResult> GetValidationErrors();
- DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
- DbEntityEntry Entry(object entity);
- string ConnectionString { get; set; }
- bool AutoDetectChangedEnabled { get; set; }
- void ExecuteSqlCommand(string p, params object[] o);
- void ExecuteSqlCommand(string p);
- string Server { get; set; }
- bool ProxyCreationEnabled { get; set; }
- bool LazyLoadingEnabled { get; set; }
- }
- public interface IRepository<T> where T : class
- {
- IContext oIContext { get; set; }
- IQueryable<T> Gets();
- IQueryable<T> Gets(Func<T, bool> filter);
- T First(Func<T, bool> filter);
- T Last(Func<T, bool> filter);
- void Insert(T entity);
- void Update(T entity);
- void Delete(T entity);
- }
- public abstract class ARepository<T> : IRepository<T> where T : class
- {
- protected IDbSet<T> _objectSet;
- public IContext oIContext { get; set; }
- public ARepository(IContext context)
- {
- _objectSet = context.Set<T>();
- oIContext = context;
- }
- public IQueryable<T> Gets()
- {
- return _objectSet.AsQueryable<T>();
- }
- public IQueryable<T> Gets(Func<T, bool> filter)
- {
- return _objectSet.Where(filter).AsQueryable<T>();
- }
- public T First(Func<T, bool> filter)
- {
- return _objectSet.FirstOrDefault(filter);
- }
- public T Last(Func<T, bool> filter)
- {
- return _objectSet.LastOrDefault(filter);
- }
- public void Insert(T entity)
- {
- oIContext.Entry(entity).State = EntityState.Added;
- }
- public void Update(T entity)
- {
- oIContext.Entry(entity).State = EntityState.Modified;
- }
- public void Delete(T entity)
- {
- oIContext.Entry(entity).State = EntityState.Detached;
- oIContext.Entry(entity).State = EntityState.Deleted;
- }
- }
- #region Repositories Interfaces
- <#
- foreach (EntityType entity in
- ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
- {
- #>
- public partial interface IRepository<#= entity.Name #>: IRepository<#=open#><#=entity.Name#><#=close#>{}
- <#
- }
- #>
- #endregion
- #region Repositories Implemented
- <#
- foreach (EntityType entity in
- ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
- {
- #>
- public partial class Repository<#= entity.Name #>: ARepository<#=open#><#=entity.Name#><#=close#>, IRepository<#=entity.Name#> { public Repository<#= entity.Name #>(IContext context) : base(context) { } }
- <#
- }
- #>
- #endregion
- public interface IFactoryUnitOfWork
- {
- IUnitOfWork Resolve();
- void Dispose();
- }
- public class FactoryUnitOfWork : IFactoryUnitOfWork
- {
- private readonly IUnityContainer oUC;
- private IUnitOfWork oUoW;
- public FactoryUnitOfWork (IUnityContainer oUC)
- {
- this.oUC = oUC;
- }
- public IUnitOfWork Resolve()
- {
- oUoW = oUC.Resolve<IUnitOfWork>();
- return oUoW;
- }
- public void Dispose()
- {
- if (oUoW != null)
- {
- oUoW.Dispose();
- }
- }
- }
- public interface IUnitOfWork : IDisposable
- {
- #region Methods
- <#
- foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
- {
- #>
- IRepository<#= set.ElementType.Name #> oRepository<#= set.Name #> { get; }
- <#
- }
- #>
- void Commit();
- void Dispose();
- #endregion
- }
- public partial class UnitOfWork : IUnitOfWork
- {
- #region Members
- private readonly IContext _context;
- <#
- foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
- {
- #>
- public IRepository<#= set.ElementType.Name #> oRepository<#= set.Name #> { get;set;}
- <#
- }
- #>
- #endregion
- #region Ctor
- public UnitOfWork(IContext context
- <#
- foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
- {
- #>
- , IRepository<#= set.ElementType.Name #> oRepository<#= set.Name #>
- <#
- }
- #> )
- {
- if (context == null)
- {
- throw new ArgumentNullException("context wasn't supplied");
- }
- context.LazyLoadingEnabled = false;
- context.ProxyCreationEnabled = false;
- <#
- foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
- {
- #>
- this.oRepository<#= set.Name #> = oRepository<#= set.Name #>;
- <#
- }
- #>
- <#
- foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
- {
- #>
- this.oRepository<#= set.Name #>.oIContext = context;
- <#
- }
- #>
- _context = context;
- }
- #endregion
- public void Commit()
- {
- try
- {
- _context.SaveChanges();
- }
- catch (DbEntityValidationException db)
- {
- var sb = new StringBuilder();
- foreach (var excep in db.EntityValidationErrors)
- {
- foreach (var ve in excep.ValidationErrors)
- {
- sb.Append(ve.PropertyName);
- sb.Append("=");
- sb.AppendLine(ve.ErrorMessage);
- }
- }
- throw new DbEntityValidationException(sb.ToString(), db);
- }
- }
- private bool bDispose;
- public void Dispose()
- {
- if (!bDispose)
- {
- _context.Dispose();
- bDispose = true;
- }
- }
- }
- public static class CompositionRoot
- {
- public static void Init(IUnityContainer oIUC)
- {
- oIUC.RegisterType<IUnitOfWork, UnitOfWork>();
- oIUC.RegisterType<IFactoryUnitOfWork, FactoryUnitOfWork>();
- oIUC.RegisterType<#= open#>IContext,<#= container #><#= close #>();
- <#
- foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
- {
- #>
- oIUC.RegisterType<#= open#>IRepository<#= set.Name #>, Repository<#= set.Name #><#= close#>();
- <#
- }
- #>
- }
- }
- public static class CompositionRootMock
- {
- public static void Init(IUnityContainer oIUC)
- {
- var mockUnitOfWork = new Mock<IUnitOfWork>();
- oIUC.RegisterInstance<IUnitOfWork>(mockUnitOfWork.Object);
- var mockFactoryUnitOfWork = new Mock<IFactoryUnitOfWork>();
- oIUC.RegisterInstance<IFactoryUnitOfWork>(mockFactoryUnitOfWork.Object);
- var mockContext = new Mock<IContext>();
- oIUC.RegisterInstance<IContext>(mockContext.Object);
- <#
- foreach (EntitySet set in container.BaseEntitySets.OfType<EntitySet>())
- {
- #>
- var oMockRepository<#= set.Name #> = new Mock<#= open#>IRepository<#= set.Name #><#= close#>();
- oIUC.RegisterInstance<#= open#>IRepository<#= set.Name #><#= close#>(oMockRepository<#= set.Name #>.Object);
- <#
- }
- #>
- }
- }
- public partial class <#= container #> : DbContext, IContext
- {
- public string ConnectionString
- {
- get
- {
- return this.Database.Connection.ConnectionString;
- }
- set
- {
- this.Database.Connection.ConnectionString = value;
- }
- }
- public bool AutoDetectChangedEnabled
- {
- get
- {
- return this.Configuration.AutoDetectChangesEnabled;
- }
- set
- {
- this.Configuration.AutoDetectChangesEnabled = value;
- }
- }
- public void ExecuteSqlCommand(string p, params object[] o)
- {
- this.Database.ExecuteSqlCommand(p, o);
- }
- public void ExecuteSqlCommand(string p)
- {
- this.Database.ExecuteSqlCommand(p);
- }
- public string Server
- {
- get
- {
- throw new NotImplementedException();
- }
- set
- {
- throw new NotImplementedException();
- }
- }
- public bool ProxyCreationEnabled
- {
- get
- {
- return this.Configuration.ProxyCreationEnabled;
- }
- set
- {
- this.Configuration.ProxyCreationEnabled = value;
- }
- }
- public bool LazyLoadingEnabled
- {
- get
- {
- return this.Configuration.LazyLoadingEnabled;
- }
- set
- {
- this.Configuration.LazyLoadingEnabled = value;
- }
- }
- }
- }
- <#+
- public string SourceCsdlPath{ get; set; }
- public EdmItemCollection ItemCollection{ get; set; }
- public IEnumerable<string> ReferenceCsdlPaths{ get; set; }
- string FindEDMXFileName()
- {
- string[] entityFrameworkFiles = Directory.GetFiles
- (Host.ResolvePath(string.Empty), "*.edmx");
- if(entityFrameworkFiles.Length > 0)
- {
- return entityFrameworkFiles[0];
- }
- return string.Empty;
- }
- #>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement