public void GenerateUnitOfWork() { UnitOfWork = new ContainerGen(); UnitOfWork.Signature = "public partial class UnitOfWork : `context`, IUnitOfWork"; UnitOfWorkBody = UnitOfWork.Body; var s1 = new StatementGen( "public " + Container + " Scope { get; }", "public DbContext Context { get; }"); var c21 = new ContainerGen(); c21.Signature = "public UnitOfWork(" + Container + " scope) : base()"; c21.Body.Add(new StatementGen( "Scope = scope;", "Context = this;")); var c22 = new ContainerGen(); c22.Signature = "public UnitOfWork(" + Container + " scope, DbContextOptions<`context`> options) : base(options)"; c22.Body.Add(new StatementGen( "Scope = scope;", "Context = this;")); var method = Data.DIContainer == DIContainer.TContainer ? "Resolve" : "GetService"; var m3 = new ContainerGen(); m3.Signature = "public S Repository<S>() where S : class, IRepository"; m3.Body.Add(new StatementGen( "var repository = Scope." + method + "<S>();", "return repository;" )); var m4 = new ContainerGen(); m4.Signature = "public D Domain<D>() where D : BaseDomain"; m4.Body.Add(new StatementGen( "var domain = Scope." + method + "<D>();", "return domain;" )); var m6 = new ContainerGen(); m6.Signature = "public IDbContextTransaction BeginTransaction()"; m6.Body.Add(new StatementGen( "var trans = this.Database.BeginTransaction();", "return trans;")); UnitOfWorkBody.Add( c21, new StatementGen(""), c22, new StatementGen(""), s1, new StatementGen(""), m3, new StatementGen(""), m4, new StatementGen(""), m6, new StatementGen("") ); NamespaceBody.Add(UnitOfWork); }
public void GenerateIEntityRepository() { IEntityRepository = new ContainerGen(); IEntityRepository.Signature = "public partial interface I`entity`Repository : IBaseRepository<`entity`, `entityPK`>"; IEntityRepositoryBody = IEntityRepository.Body; NamespaceBody.Add(IEntityRepository, new StatementGen("")); }
public void GenerateUpdateVMClass() { UpdateVMClass = new ContainerGen(); UpdateVMClass.Signature = "public partial class Update`entityVM`: BaseUpdateServiceModel<Update`entityVM`, `entity`>"; UpdateVMClassBody = UpdateVMClass.Body; NamespaceBody.Add(UpdateVMClass); }
public void GenerateVMClass() { VMClass = new ContainerGen(); VMClass.Signature = "public partial class `entityVM`: BaseServiceModel<`entity`>"; VMClassBody = VMClass.Body; NamespaceBody.Add(VMClass, new StatementGen("")); }
public void GenerateEntityExtension() { EntityExtension = new ContainerGen(); EntityExtension.Signature = "public partial class `entity` : BaseEntity"; EntityExtensionBody = EntityExtension.Body; NamespaceBody.Add(EntityExtension); }
public void GenerateBaseEntityExtension() { BaseEntityExtension = new ContainerGen(); BaseEntityExtension.Signature = "public partial class `entity` : BaseEntity"; BaseEntityExtensionBody = BaseEntityExtension.Body; NamespaceBody.Add(BaseEntityExtension, new StatementGen("")); }
public void GenerateBaseDomain() { BaseDomain = new ContainerGen(); BaseDomain.Signature = "public abstract partial class BaseDomain"; BaseDomainBody = BaseDomain.Body; NamespaceBody.Add(BaseDomain); }
public void GenerateVMClass() { VMClass = new ContainerGen(); VMClass.Signature = "public partial class `entityVM`: BaseViewModel<`entity`>"; VMClassBody = VMClass.Body; NamespaceBody.Add(VMClass); }
public void GenerateContextManager() { ContextManager = new ContainerGen(); ContextManager.Signature = "public partial class ContextManager"; ContextManagerBody = ContextManager.Body; NamespaceBody.Add(ContextManager); }
public void GenerateBaseDomain() { BaseDomain = new ContainerGen(); BaseDomain.Signature = "public abstract partial class BaseDomain";/*<E, VM, K, S> : IBaseDomain<E, VM, K> where S: class, IBaseService<E, VM, K> where E: class, IBaseEntity<VM> where VM: class";*/ BaseDomainBody = BaseDomain.Body; NamespaceBody.Add(BaseDomain); }
public void GenerateIEntityService() { IEntityService = new ContainerGen(); IEntityService.Signature = "public partial interface I`entity`Service : IBaseService<`entity`, `entityPK`>"; IEntityServiceBody = IEntityService.Body; NamespaceBody.Add(IEntityService, new StatementGen("")); }
public void GenerateBaseRepository() { BaseRepository = new ContainerGen(); BaseRepository.Signature = "public abstract partial class BaseRepository<E, K> : IBaseRepository<E, K> where E : class"; BaseRepositoryBody = BaseRepository.Body; NamespaceBody.Add(BaseRepository); }
public void GenerateEntityRepository() { EntityRepository = new ContainerGen(); EntityRepository.Signature = "public partial class `entity`Repository : BaseRepository<`entity`, `entityPK`>, I`entity`Repository"; EntityRepositoryBody = EntityRepository.Body; NamespaceBody.Add(EntityRepository); }
public void GenerateBaseService() { BaseService = new ContainerGen(); BaseService.Signature = "public abstract partial class BaseService<E, K> : " + (Data.ServicePool ? "Resource, " : "") + "IBaseService<E, K>"; BaseServiceBody = BaseService.Body; NamespaceBody.Add(BaseService); }
public void GenerateEntityService() { EntityService = new ContainerGen(); EntityService.Signature = "public partial class `entity`Service : BaseService<`entity`, `entityPK`>, I`entity`Service"; EntityServiceBody = EntityService.Body; NamespaceBody.Add(EntityService); }
private void GenerateBaseUpdateVM() { var baseUpdate = new ContainerGen(); baseUpdate.Signature = "public abstract partial class BaseUpdateServiceModel<VM, Entity>"; var m1 = new ContainerGen(); m1.Signature = "public void PatchTo(Entity dest)"; m1.Body.Add(new StatementGen( "foreach (var map in vPropMappings)", "{", "\tvar srcProp = map.Value;", "\tvar srcVal = srcProp.GetValue(this);", "\tif (srcVal != null)", "\t{", "\t\tvar destProp = ePropMappings[map.Key];", "\t\tdestProp.SetValue(dest, (srcVal as IWrapper).GetValue());", "\t}", "}" )); var s2 = new StatementGen("protected static IDictionary<string, PropertyInfo> vPropMappings; // ServiceModel"); var s3 = new StatementGen("protected static IDictionary<string, PropertyInfo> ePropMappings; // entity"); var m4 = new ContainerGen(); m4.Signature = "static BaseUpdateServiceModel()"; m4.Body.Add(new StatementGen( "var entityType = typeof(Entity);", "var vmType = typeof(VM);", "vPropMappings = new Dictionary<string, PropertyInfo>();", "ePropMappings = new Dictionary<string, PropertyInfo>();", "var props = entityType.GetProperties();", "foreach (var p in props)", "{", "\tePropMappings[p.Name] = p;", "}", "props = vmType.GetProperties();", "foreach (var p in props)", "{", "\tif (ePropMappings.ContainsKey(p.Name))", "\t\tvPropMappings[p.Name] = p;", "}")); baseUpdate.Body.Add( m1, new StatementGen(""), s2, new StatementGen(""), s3, new StatementGen(""), m4, new StatementGen("") ); NamespaceBody.Add(baseUpdate, new StatementGen("")); }
public void GenerateIBaseRepository() { var IRepository = new ContainerGen(); IRepository.Signature = "public partial interface IRepository: IReusable"; NamespaceBody.Add(IRepository); IBaseRepository = new ContainerGen(); IBaseRepository.Signature = "public partial interface IBaseRepository<E, K> : IRepository"; IBaseRepositoryBody = IBaseRepository.Body; IBaseRepositoryBody.Add( new StatementGen( //"bool AutoSave { get; set; }", "int SaveChanges();", "Task<int> SaveChangesAsync();", "", "E Add(E entity);", //"Task<E> AddAsync(E entity);", "E Update(E entity);", //"Task <E> UpdateAsync(E entity);", "E Remove(E entity);", //"Task<E> RemoveAsync(E entity);", "E Remove(K key);", "IEnumerable<E> RemoveIf(Expression<Func<E, bool>> expr);", "IEnumerable<E> RemoveRange(IEnumerable<E> list);", //"Task<E> RemoveAsync(K key);", "E FindById(K key);", "Task<E> FindByIdAsync(K key);", "E FindActiveById(K key);", "Task<E> FindActiveByIdAsync(K key);", "E FindByIdInclude<TProperty>(K key, params Expression<Func<E, TProperty>>[] members);", "Task<E> FindByIdIncludeAsync<TProperty>(K key, params Expression<Func<E, TProperty>>[] members);", "E Activate(E entity);", "E Activate(K key);", "E Deactivate(E entity);", "E Deactivate(K key);", "IQueryable<E> GetActive();", "IQueryable<E> GetActive(Expression<Func<E, bool>> expr);", "E FirstOrDefault();", "E FirstOrDefault(Expression<Func<E, bool>> expr);", "Task<E> FirstOrDefaultAsync();", "Task<E> FirstOrDefaultAsync(Expression<Func<E, bool>> expr);", "E SingleOrDefault(Expression<Func<E, bool>> expr);", "Task<E> SingleOrDefaultAsync(Expression<Func<E, bool>> expr);", "DbRawSqlQuery<E> SqlQuery(string sql, params object[] parameters);", "DbRawSqlQuery<T> SqlQuery<T>(string sql, params object[] parameters);" )); NamespaceBody.Add(IBaseRepository, new StatementGen("")); }
private void GenerateGlobal() { GlobalClass = new ContainerGen(); GlobalClass.Signature = "public static partial class G"; GlobalClassBody = GlobalClass.Body; GlobalClassBody.Add( new StatementGen( "public static IMapper Mapper;", "public static ITContainer TContainer;", "private static ITContainerBuilder Builder = new TContainerBuilder();" )); NamespaceBody.Add(GlobalClass); }
public void GenerateIEntityRepository() { IEntityRepository = new ContainerGen(); IEntityRepository.Signature = "public partial interface I`entity`Repository : IBaseRepository<`entity`, `entityPK`>"; IEntityRepositoryBody = IEntityRepository.Body; if (EInfo.Activable) { IEntityRepositoryBody.Add( new StatementGen("`entity` FindActiveById(`entityPK` key);"), new StatementGen("Task<`entity`> FindActiveByIdAsync(`entityPK` key);"), new StatementGen("IQueryable<`entity`> GetActive();"), new StatementGen("IQueryable<`entity`> GetActive(Expression<Func<`entity`, bool>> expr);") ); } NamespaceBody.Add(IEntityRepository, new StatementGen("")); }
public void GenerateIBaseService() { var IService = new ContainerGen(); IService.Signature = "public partial interface IService" + (Data.ServicePool ? ": IResource" : ""); NamespaceBody.Add(IService); IBaseService = new ContainerGen(); IBaseService.Signature = "public partial interface IBaseService<E, K> : IService"; IBaseServiceBody = IBaseService.Body; IBaseServiceBody.Add( new StatementGen( //"bool AutoSave { get; set; }", "int SaveChanges();", "Task<int> SaveChangesAsync();", "", "E Add(E entity);", //"Task<E> AddAsync(E entity);", "E Update(E entity);", //"Task<E> UpdateAsync(E entity);", "E Remove(E entity);", //"Task<E> RemoveAsync(E entity);", "E Remove(K key);", "IEnumerable<E> RemoveIf(Expression<Func<E, bool>> expr);", "IEnumerable<E> RemoveRange(IEnumerable<E> list);", //"Task<E> RemoveAsync(K key);", "E FindById(K key);", "Task<E> FindByIdAsync(K key);", "E Activate(E entity);", "E Activate(K key);", "E Deactivate(E entity);", "E Deactivate(K key);", "IQueryable<E> GetActive();", "IQueryable<E> GetActive(Expression<Func<E, bool>> expr);", "E FirstOrDefault();", "E FirstOrDefault(Expression<Func<E, bool>> expr);", "Task<E> FirstOrDefaultAsync();", "Task<E> FirstOrDefaultAsync(Expression<Func<E, bool>> expr);" )); NamespaceBody.Add(IBaseService, new StatementGen("")); }
public void GenerateIBaseRepository() { var IRepository = new ContainerGen(); IRepository.Signature = "public partial interface IRepository"; NamespaceBody.Add(IRepository); IBaseRepository = new ContainerGen(); IBaseRepository.Signature = "public partial interface IBaseRepository<E, K> : IRepository where E : class"; IBaseRepositoryBody = IBaseRepository.Body; IBaseRepositoryBody.Add( new StatementGen( "int SaveChanges();", "Task<int> SaveChangesAsync();", "", "void Reload(E entity);", "EntityEntry<E> Create(E entity);", "void CreateRange(IEnumerable<E> entities);", "EntityEntry<E> Update(E entity);", "void UpdateRange(IEnumerable<E> entities);", "EntityEntry<E> Update(E entity, Action<E> patchAction);", "EntityEntry<E> Attach(E entity);", "EntityEntry<E> Remove(E entity);", "EntityEntry<E> Remove(K key);", "void RemoveIf(Expression<Func<E, bool>> expr);", "void RemoveRange(IEnumerable<E> list);", "E FindById(K key);", "Task<E> FindByIdAsync(K key);", "IQueryable<E> AsNoTracking();", "DbSet<E> Get();", "IQueryable<E> Get(Expression<Func<E, bool>> expr);", "E FirstOrDefault();", "E FirstOrDefault(Expression<Func<E, bool>> expr);", "Task<E> FirstOrDefaultAsync();", "Task<E> FirstOrDefaultAsync(Expression<Func<E, bool>> expr);", "E SingleOrDefault(Expression<Func<E, bool>> expr);", "Task<E> SingleOrDefaultAsync(Expression<Func<E, bool>> expr);" )); NamespaceBody.Add(IBaseRepository, new StatementGen("")); }
public void GenerateIUnitOfWork() { IUnitOfWork = new ContainerGen(); IUnitOfWork.Signature = "public partial interface IUnitOfWork : IDisposable"; IUnitOfWorkBody = IUnitOfWork.Body; var s1 = new StatementGen( //"bool AutoSave { get; set; }", "ITContainer Scope { get; set; }", "`context` Context { get; set; }", "S Service<S>() where S : class, IService;", "int SaveChanges();", "Task<int> SaveChangesAsync();", "DbContextTransaction BeginTransaction();"); IUnitOfWorkBody.Add( s1, new StatementGen("") ); NamespaceBody.Add(IUnitOfWork); }
public void GenerateEntityPKClass() { if (EInfo.PKPropMapping.Count > 1) { EntityPKClass = new ContainerGen(); EntityPKClass.Signature = "public partial class `entityPK`"; EntityPKClassBody = EntityPKClass.Body; var statements = new StatementGen(); foreach (var kvp in EInfo.PKPropMapping) { var propName = kvp.Key; var propType = kvp.Value; statements.Add("public " + propType + " " + propName + " { get; set; }"); } EntityPKClassBody.Add(statements); NamespaceBody.Add(EntityPKClass, new StatementGen("")); } }
public void GenerateGeneralExtension() { GeneralExtension = new ContainerGen(); GeneralExtension.Signature = "public static partial class GeneralUtils"; GeneralExtensionBody = GeneralExtension.Body; var m1 = new ContainerGen(); m1.Signature = "public static List<VM> ToListVM<E, VM>(this IEnumerable<E> list) where E: IBaseEntity"; m1.Body.Add(new StatementGen("return list.Select(e => e.To<VM>()).ToList();")); var m2 = new ContainerGen(); m2.Signature = "public static List<E> ToListEntities<VM, E>(this IEnumerable<VM> list) where VM: IViewModel"; m2.Body.Add(new StatementGen("return list.Select(vm => vm.To<E>()).ToList();")); GeneralExtensionBody.Add( m1, new StatementGen(""), m2, new StatementGen("")); NamespaceBody.Add(GeneralExtension); }
public void GenerateEntityDomain() { EntityDomain = new ContainerGen(); EntityDomain.Signature = "public partial class `entity`Domain : BaseDomain<Models.`entity`, `entity`ViewModel, `entityPK`, `service`>"; EntityDomainBody = EntityDomain.Body; var c0 = new ContainerGen(); c0.Signature = "public `entity`Domain() : base()"; if (EInfo.Data.RequestScope) { EntityDomainBody.Add(c0); } var c1 = new ContainerGen(); c1.Signature = "public `entity`Domain(IUnitOfWork uow) : base(uow)"; EntityDomainBody.Add(c1); NamespaceBody.Add(EntityDomain, new StatementGen("")); }
public void GenerateIUnitOfWork() { IUnitOfWork = new ContainerGen(); IUnitOfWork.Signature = "public partial interface IUnitOfWork : IDisposable"; IUnitOfWorkBody = IUnitOfWork.Body; var s1 = new StatementGen( Container + " Scope { get; }", "DbContext Context { get; }", "DbSet<E> Set<E>() where E : class;", "S Repository<S>() where S : class, IRepository;", "D Domain<D>() where D : BaseDomain;", "int SaveChanges();", "Task<int> SaveChangesAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken));", "IDbContextTransaction BeginTransaction();"); IUnitOfWorkBody.Add( s1, new StatementGen("")); NamespaceBody.Add(IUnitOfWork); }
private void GenerateIBaseVM() { var IViewModel = new ContainerGen(); IViewModel.Signature = "public partial interface IViewModel"; IViewModel.Body.Add( new StatementGen( "void CopyFrom(object src);", "void CopyTo(object dest);", "ET To<ET>();")); NamespaceBody.Add(IViewModel, new StatementGen("")); IBaseVM = new ContainerGen(); IBaseVM.Signature = "public partial interface IBaseViewModel<E>: IViewModel"; IBaseVMBody = IBaseVM.Body; IBaseVMBody.Add( new StatementGen( "void FromEntity(E entity);", "E ToEntity();" )); NamespaceBody.Add(IBaseVM, new StatementGen("")); }
public void GenerateBaseEntity() { var baseEntity = new ContainerGen(); baseEntity.Signature = "public abstract partial class BaseEntity : IBaseEntity"; var m2 = new ContainerGen(); m2.Signature = "public virtual E To<E>()"; m2.Body.Add(new StatementGen( "return G.Mapper.Map<E>(this);")); var m3 = new ContainerGen(); m3.Signature = "public virtual void CopyTo(object dest)"; m3.Body.Add(new StatementGen( "G.Mapper.Map(this, dest);")); baseEntity.Body.Add( m2, new StatementGen(""), m3, new StatementGen("")); NamespaceBody.Add(baseEntity, new StatementGen("")); }
public void GenerateUnitOfWork() { UnitOfWork = new ContainerGen(); UnitOfWork.Signature = "public partial class UnitOfWork : IUnitOfWork"; UnitOfWorkBody = UnitOfWork.Body; var s1 = new StatementGen( //"public bool AutoSave { get; set; }", "public ITContainer Scope { get; set; }", "public `context` Context { get; set; }", "private IDictionary<Type, object> ResourcePool { get; set; }"); var c2 = new ContainerGen(); c2.Signature = "public UnitOfWork()"; c2.Body.Add(new StatementGen( //"AutoSave = true;", "Scope = G.TContainer.RequestScope;", "Context = new `context`();", "ResourcePool = new Dictionary<Type, object>();" /*"Scope.ManageResources(Context);"*/)); var c21 = new ContainerGen(); c21.Signature = "public UnitOfWork(ITContainer scope)"; c21.Body.Add(new StatementGen( //"AutoSave = true;", "Scope = scope;", "Context = new `context`();", "ResourcePool = new Dictionary<Type, object>();" /*"Scope.ManageResources(Context);"*/)); //var c22 = new ContainerGen(); //c22.Signature = "public UnitOfWork(bool reqScope)"; //c22.Body.Add(new StatementGen( // "AutoSave = true;", // "Scope = reqScope ? G.TContainer.CreateRequestScope() : G.TContainer.CreateScope();", // "Scope.ManageResources(this);", // "Context = new `context`();", // "ResourcePool = new Dictionary<Type, object>();" // /*"Scope.ManageResources(Context);"*/)); var m3 = new ContainerGen(); m3.Signature = "public S Service<S>() where S : class, IService"; m3.Body.Add(new StatementGen( "var type = typeof(S);", "if (ResourcePool.ContainsKey(type))", "\treturn (S)ResourcePool[type];", "var service = " + (Data.ServicePool ? "Scope.ResolveFromPool<S>(this);" : "Scope.Resolve<S>(this);"), "ResourcePool.Add(type, service);", "return service;" )); var m4 = new ContainerGen(); m4.Signature = "public int SaveChanges()"; m4.Body.Add(new StatementGen( "return Context.SaveChanges();")); var m5 = new ContainerGen(); m5.Signature = "public async Task<int> SaveChangesAsync()"; m5.Body.Add(new StatementGen( "return await Context.SaveChangesAsync();")); var m6 = new ContainerGen(); m6.Signature = "public DbContextTransaction BeginTransaction()"; m6.Body.Add(new StatementGen( "var trans = Context.Database.BeginTransaction();", "Scope.ManageResources(trans);", "return trans;")); var s7 = new StatementGen( "#region IDisposable Support", "protected bool disposedValue = false;"); var m8 = new ContainerGen(); m8.Signature = "protected virtual void Dispose(bool disposing)"; m8.Body.Add(new StatementGen( "if (!disposedValue)", "{"), new StatementGen(true, "if (disposing)", "{", "}", "", "disposedValue = true;", "Context.Dispose();"), //"if (AutoSave)", //"try", //"{", //"\tContext.SaveChanges();", //"\tContext.Dispose();", //"}", //"catch (Exception e)", //"{", //"\tContext.Dispose();", //"\tthrow e;", //"}"), new StatementGen("}")); var m9 = new ContainerGen(); m9.Signature = "public void Dispose()"; m9.Body.Add(new StatementGen( "Dispose(true);", "GC.SuppressFinalize(this);")); var s10 = new StatementGen("#endregion"); var m11 = new ContainerGen(); m11.Signature = "~UnitOfWork()"; m11.Body.Add(new StatementGen("Dispose(false);")); if (Data.RequestScope) { UnitOfWorkBody.Add(c2, new StatementGen("")); } UnitOfWorkBody.Add( c21, new StatementGen(""), s1, new StatementGen(""), //c22, new StatementGen(""), m3, new StatementGen(""), m4, new StatementGen(""), m5, new StatementGen(""), m6, new StatementGen(""), s7, new StatementGen(""), m8, new StatementGen(""), m9, new StatementGen(""), s10, new StatementGen(""), m11, new StatementGen("") ); NamespaceBody.Add(UnitOfWork); }
private void GenerateBaseVM() { var BaseViewModel = new ContainerGen(); BaseViewModel.Signature = "public abstract partial class BaseViewModel<E>: IBaseViewModel<E>"; var s1 = new StatementGen("protected E Entity { get; set; }"); var m3 = new ContainerGen(); m3.Signature = "public virtual void FromEntity(E entity)"; m3.Body.Add(new StatementGen( "Entity = entity;", "G.Mapper.Map(entity, this);")); var m31 = new ContainerGen(); m31.Signature = "public virtual void CopyFrom(object src)"; m31.Body.Add(new StatementGen( "G.Mapper.Map(src, this);")); var m32 = new ContainerGen(); m32.Signature = "public virtual void CopyTo(object dest)"; m32.Body.Add(new StatementGen( "G.Mapper.Map(this, dest);")); var m33 = new ContainerGen(); m33.Signature = "public virtual ET To<ET>()"; m33.Body.Add(new StatementGen( "return G.Mapper.Map<ET>(this);")); var m4 = new ContainerGen(); m4.Signature = "public virtual E ToEntity()"; m4.Body.Add(new StatementGen( "if (Entity!=null)", "\treturn Entity;", "return G.Mapper.Map<E>(this);")); var m5 = new ContainerGen(); m5.Signature = "public virtual E ToEntity(bool copyToEntity)"; m5.Body.Add(new StatementGen( "if (Entity!=null)", "{", "\tif (copyToEntity) CopyTo(Entity);", "\treturn Entity;", "}", "return G.Mapper.Map<E>(this);")); BaseViewModel.Body.Add( s1, new StatementGen(""), m3, new StatementGen(""), m31, new StatementGen(""), m32, new StatementGen(""), m33, new StatementGen(""), m4, new StatementGen(""), m5, new StatementGen("")); NamespaceBody.Add(BaseViewModel, new StatementGen("")); }