private void GenerateInitContainer() { var method = new ContainerGen(); method.Signature = "private static void ConfigureIoContainer()"; var s2 = new StatementGen("//IoContainer", Data.RequestScope ? "Builder.RegisterRequestScopeHandlerModule();" : null, "Builder.RegisterType<IUnitOfWork, UnitOfWork>();"); foreach (var e in Data.Entities) { s2.Add("Builder.RegisterType<I" + e.EntityName + "Repository, " + e.EntityName + "Repository>();"); } foreach (var e in Data.Entities) { if (Data.ServicePool) { s2.Add("Builder.RegisterToPool<I" + e.EntityName + "Service, " + e.EntityName + "Service>(10, 100);"); } else { s2.Add("Builder.RegisterType<I" + e.EntityName + "Service, " + e.EntityName + "Service>();"); } } s2.Add("G.TContainer = Builder.Build();"); method.Body.Add(s2); GlobalClassBody.Add(method, new StatementGen("")); }
private void GenerateEntityViewModel() { var jIgnoreArr = new StatementGen(); jIgnoreArr.Add("var jIgnore = new string[]", "{"); foreach (var p in JsonIgnoreProps) { jIgnoreArr.Add("\t\"" + p + "\","); } jIgnoreArr.Add("};"); var exceptArr = new StatementGen(); exceptArr.Add("var except = new string[]", "{"); foreach (var p in ExceptProps) { exceptArr.Add("\t\"" + p + "\","); } exceptArr.Add("};"); Add(new TemplateCodeBlock(jIgnoreArr, exceptArr, new StatementGen( "foreach (var e in dt.Entities)", "{"), new StatementGen(true, "var vmGen = new VMGen(e, jIgnore, except, JsonPropertyFormatEnum." + Enum.GetName(typeof(JsonPropertyFormatEnum), Style) + ");", "manager.StartNewFile(e.EntityName+\"ViewModel.txt\");")), new TemplateTextBlock("<#=vmGen.Generate()#>"), new TemplateCodeBlock(new StatementGen( "}", "manager.Process();"))); }
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); }
private void GenerateInitDI() { if (Data.DIContainer == DIContainer.TContainer) { var method = new ContainerGen(); method.Signature = "private static void ConfigureIoC()"; var s2 = new StatementGen("//IoC", "var repoOpt = new BuilderOptions();", "repoOpt.InjectableConstructors = new Dictionary<int, Params[]>();", "repoOpt.InjectableConstructors[0] = new Params[] { Params.Injectable<IUnitOfWork>() };", "", "Builder.RegisterType<ITContainer>(container => container)"); s2.Add("\t.RegisterType<UnitOfWork>(container => container.Resolve<UnitOfWork>())"); s2.Add("\t.RegisterType<IUnitOfWork, UnitOfWork>(container => container.Resolve<UnitOfWork>())"); s2.Add("\t.RegisterType<" + Data.ContextName + ", UnitOfWork>(container => container.Resolve<UnitOfWork>())"); s2.Add("\t.RegisterType<DbContext, UnitOfWork>(container => container.Resolve<UnitOfWork>())"); var entities = Data.Entities; var len = entities.Count; int i = 0; for (i = 0; i < len - 1; i++) { s2.Add("\t.RegisterType<I" + entities[i].EntityName + "Repository, " + entities[i].EntityName + "Repository>(repoOpt)"); } s2.Add("\t.RegisterType<I" + entities[i].EntityName + "Repository, " + entities[i].EntityName + "Repository>(repoOpt)", "\t.AttachAllRegisteredToLifetimeScope();"); s2.Add("G.TContainer = Builder.Build();"); method.Body.Add(s2); GlobalClassBody.Add(method, new StatementGen("")); } else { var method = new ContainerGen(); method.Signature = "private static void ConfigureIoC(IServiceCollection services)"; var s2 = new StatementGen("//IoC", "services.AddScoped<UnitOfWork>()"); s2.Add("\t.AddScoped<IUnitOfWork, UnitOfWork>()"); s2.Add("\t.AddScoped<" + Data.ContextName + ", UnitOfWork>()"); s2.Add("\t.AddScoped<DbContext, UnitOfWork>()"); var entities = Data.Entities; var len = entities.Count; int i = 0; for (i = 0; i < len - 1; i++) { s2.Add("\t.AddScoped<I" + entities[i].EntityName + "Repository, " + entities[i].EntityName + "Repository>()"); } s2.Add("\t.AddScoped<I" + entities[i].EntityName + "Repository, " + entities[i].EntityName + "Repository>();"); method.Body.Add(s2); GlobalClassBody.Add(method, new StatementGen("")); } }
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("")); }
//generate UpdateVMClass body public void GenerateUpdateVMClassBody() { var s0 = new StatementGen(); foreach (var p in EInfo.PropMapping) { if (!ExceptProps.Contains(p.Key)) { if (JsonIgnoreProps.Contains(p.Key)) { s0.Add("//[JsonIgnore]"); } else { s0.Add("//[JsonProperty(\"" + GeneralHelper.ToJsonPropertyFormat(p.Key, Style) + "\")]");//+ "\", DefaultValueHandling = DefaultValueHandling.Ignore)]"); } s0.Add("public Wrapper<" + p.Value + "> " + p.Key + " { get; set; }"); } } foreach (var p in EInfo.NavPropMapping) { if (!ExceptProps.Contains(p.Key)) { if (JsonIgnoreProps.Contains(p.Key)) { s0.Add("//[JsonIgnore]"); } else { s0.Add("//[JsonProperty(\"" + GeneralHelper.ToJsonPropertyFormat(p.Key, Style) + "\")]");// + "\", DefaultValueHandling = DefaultValueHandling.Ignore)]"); } s0.Add("//public " + p.Value + " " + p.Key + "VM { get; set; }"); } } foreach (var p in EInfo.NavCollectionPropMapping) { if (!ExceptProps.Contains(p.Key)) { if (JsonIgnoreProps.Contains(p.Key)) { s0.Add("//[JsonIgnore]"); } else { s0.Add("//[JsonProperty(\"" + GeneralHelper.ToJsonPropertyFormat(p.Key, Style) + "\")]");// + "\", DefaultValueHandling = DefaultValueHandling.Ignore)]"); } s0.Add("//public " + p.Value + " " + p.Key + "VM { get; set; }"); } } UpdateVMClassBody.Add( s0, new StatementGen("")); }
private void GenerateMapperConfig() { #region MapperConfig var mapperConfig = new StatementGen("private static List<Action<IMapperConfigurationExpression>> MapperConfigs"); var open = new StatementGen( "\t= new List<Action<IMapperConfigurationExpression>>();", "//{"); var mapConfig = new StatementGen(true, "//cfg =>", "//{"); foreach (var e in Data.Entities) { mapConfig.Add("//\tcfg.CreateMap<" + e.EntityName + ", " + e.VMClass + ">().ReverseMap();"); } var close = new StatementGen("//\t}", "//};"); GlobalClassBody.Add( mapperConfig, open, mapConfig, close); #endregion }
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); }
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 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); }
private void GenerateInitMapper() { var method = new ContainerGen(); method.Signature = "private static void ConfigureAutomapper()"; var s1 = new StatementGen("//AutoMapper"); var open = new StatementGen( "var mapConfig = new MapperConfiguration(cfg =>", "{"); var mapConfig = new StatementGen(true, "foreach (var c in MapperConfigs)", "{", "\tc.Invoke(cfg);", "}"); var close = new StatementGen("});", "G.Mapper = mapConfig.CreateMapper();"); method.Body.Add( s1, open, mapConfig, close, new StatementGen("")); GlobalClassBody.Add(method, new StatementGen("")); }
public void GenerateBaseDomainBody() { var c01 = new ContainerGen(); c01.Signature = "public BaseDomain(DbContext context)"; c01.Body.Add(new StatementGen( "this.context = context;")); var s1 = new StatementGen( "protected DbContext context;"); var c1 = new ContainerGen(); c1.Signature = "public BaseDomain(IUnitOfWork uow)"; c1.Body.Add(new StatementGen( "this.uow = uow;", "this.context = uow.Context;")); var s2 = new StatementGen( "protected IUnitOfWork uow;"); var s3 = new StatementGen("public IUnitOfWork UoW { get { return uow; } }"); var s4 = new StatementGen("public DbContext Context { get { return context; } }"); if (Data.DIContainer == DIContainer.TContainer) { BaseDomainBody.Add(c01, new StatementGen("")); } BaseDomainBody.Add( c1, new StatementGen(""), s2, new StatementGen(""), s1, new StatementGen(""), s3, new StatementGen(""), s4, new StatementGen("") ); }
//generate VMClass body public void GenerateVMClassBody() { var s0 = new StatementGen(); foreach (var p in EInfo.PropMapping) { if (!ExceptProps.Contains(p.Key)) { if (JsonIgnoreProps.Contains(p.Key)) { s0.Add("[JsonIgnore]"); } else { s0.Add("[JsonProperty(\"" + GeneralHelper.ToJsonPropertyFormat(p.Key, Style) + "\")]");//+ "\", DefaultValueHandling = DefaultValueHandling.Ignore)]"); } s0.Add("public " + p.Value + " " + p.Key + " { get; set; }"); } } foreach (var p in EInfo.NavPropMapping) { if (!ExceptProps.Contains(p.Key)) { if (JsonIgnoreProps.Contains(p.Key)) { s0.Add("[JsonIgnore]"); } else { s0.Add("[JsonProperty(\"" + GeneralHelper.ToJsonPropertyFormat(p.Key, Style) + "\")]");// + "\", DefaultValueHandling = DefaultValueHandling.Ignore)]"); } s0.Add("public " + p.Value + " " + p.Key + "VM { get; set; }"); } } foreach (var p in EInfo.NavCollectionPropMapping) { if (!ExceptProps.Contains(p.Key)) { if (JsonIgnoreProps.Contains(p.Key)) { s0.Add("[JsonIgnore]"); } else { s0.Add("[JsonProperty(\"" + GeneralHelper.ToJsonPropertyFormat(p.Key, Style) + "\")]");// + "\", DefaultValueHandling = DefaultValueHandling.Ignore)]"); } s0.Add("public " + p.Value + " " + p.Key + "VM { get; set; }"); } } var c2 = new ContainerGen(); c2.Signature = "public `entityVM`(`entity` entity) : this()"; c2.Body.Add(new StatementGen("FromEntity(entity);")); var c21 = new ContainerGen(); c21.Signature = "public `entityVM`()"; foreach (var p in EInfo.NavCollectionPropMapping) { var type = p.Value; var firstBracket = type.IndexOf('<') + 1; type = type.Substring(firstBracket, type.IndexOf('>') - firstBracket); c21.Body.Add(new StatementGen("this." + p.Key + "VM = new HashSet<" + type + ">();")); } VMClassBody.Add( s0, new StatementGen(""), c2, new StatementGen(""), c21, new StatementGen("")); }
public void GenerateBaseDomainBody() { var c0 = new ContainerGen(); c0.Signature = "public BaseDomain()"; c0.Body.Add(new StatementGen( "_uow = G.TContainer.ResolveRequestScope<IUnitOfWork>();")); //"baseService = _uow.Service<S>();")); var c1 = new ContainerGen(); c1.Signature = "public BaseDomain(IUnitOfWork uow)"; c1.Body.Add(new StatementGen( "_uow = uow;")); //"baseService = _uow.Service<S>();")); var s2 = new StatementGen( "private IUnitOfWork _uow;"); var m2 = new StatementGen( "protected IUnitOfWork UoW", "{", "\tget", "\t{", "\t\treturn _uow;", "\t}", "}"); //var s3 = new StatementGen( // "private S baseService;"); //var m3 = new StatementGen( // "protected S BaseService", // "{", // "\tget", // "\t{", // "\t\treturn baseService;", // "\t}", // "}"); //var m4 = new ContainerGen(); //m4.Signature = "public E Add(E entity)"; //m4.Body.Add(new StatementGen( // "return baseService.Add(entity);")); ////var m5 = new ContainerGen(); ////m5.Signature = "public async Task<E> AddAsync(E entity)"; ////m5.Body.Add(new StatementGen( //// "return (await baseService.AddAsync(entity));")); //var m6 = new ContainerGen(); //m6.Signature = "public E Update(E entity)"; //m6.Body.Add(new StatementGen( // "return baseService.Update(entity);")); ////var m7 = new ContainerGen(); ////m7.Signature = "public async Task<E> UpdateAsync(E entity)"; ////m7.Body.Add(new StatementGen( //// "return (await baseService.UpdateAsync(entity));")); //var m8 = new ContainerGen(); //m8.Signature = "public E Remove(E entity)"; //m8.Body.Add(new StatementGen( // "return baseService.Remove(entity);")); ////var m9 = new ContainerGen(); ////m9.Signature = "public async Task<E> RemoveAsync(E entity)"; ////m9.Body.Add(new StatementGen( //// "return (await baseService.RemoveAsync(entity));")); //var m10 = new ContainerGen(); //m10.Signature = "public E Remove(K key)"; //m10.Body.Add(new StatementGen( // "var entity = baseService.Remove(key);", // "if (entity != null)", // "\treturn entity;", // "return null;")); //var m101 = new ContainerGen(); //m101.Signature = "public IEnumerable<E> RemoveIf(Expression<Func<E, bool>> expr)"; //m101.Body.Add(new StatementGen( // "return baseService.RemoveIf(expr);")); //var m102 = new ContainerGen(); //m102.Signature = "public IEnumerable<E> RemoveRange(IEnumerable<E> list)"; //m102.Body.Add(new StatementGen( // "return baseService.RemoveRange(list);")); ////var m11 = new ContainerGen(); ////m11.Signature = "public async Task<E> RemoveAsync(K key)"; ////m11.Body.Add(new StatementGen( //// "var entity = await baseService.RemoveAsync(key);", //// "if (entity != null)", //// "\treturn entity;", //// "return null;")); //var m12 = new ContainerGen(); //m12.Signature = "public E FindById(K key)"; //m12.Body.Add(new StatementGen( // "var entity = baseService.FindById(key);", // "if (entity != null)", // "\treturn entity;", // "return null;")); //var m13 = new ContainerGen(); //m13.Signature = "public async Task<E> FindByIdAsync(K key)"; //m13.Body.Add(new StatementGen( // "var entity = await baseService.FindByIdAsync(key);", // "if (entity != null)", // "\treturn entity;", // "return null;")); //var m141 = new ContainerGen(); //m141.Signature = "public E Activate(E entity)"; //m141.Body.Add(new StatementGen( // "return baseService.Activate(entity);")); //var m1411 = new ContainerGen(); //m1411.Signature = "public E Activate(K key)"; //m1411.Body.Add(new StatementGen( // "var entity = baseService.Activate(key);", // "if (entity != null)", // "\treturn entity;", // "return null;")); //var m142 = new ContainerGen(); //m142.Signature = "public E Deactivate(E entity)"; //m142.Body.Add(new StatementGen( // "return baseService.Deactivate(entity);")); //var m1422 = new ContainerGen(); //m1422.Signature = "public E Deactivate(K key)"; //m1422.Body.Add(new StatementGen( // "var entity = baseService.Deactivate(key);", // "if (entity != null)", // "\treturn entity;", // "return null;")); //var m14 = new ContainerGen(); //m14.Signature = "public IEnumerable<E> GetActive()"; //m14.Body.Add(new StatementGen( // "return baseService.GetActive().AsEnumerable();")); //var m15 = new ContainerGen(); //m15.Signature = "public IEnumerable<E> GetActive(Expression<Func<E, bool>> expr)"; //m15.Body.Add(new StatementGen( // "return baseService.GetActive(expr).AsEnumerable();")); ////var m16 = new ContainerGen(); ////m16.Signature = "public async Task<IEnumerable<E>> GetActiveAsync()"; ////m16.Body.Add(new StatementGen( //// "return await Task.Run(() =>", //// "{", //// "\treturn GetActive();", //// "});")); ////var m17 = new ContainerGen(); ////m17.Signature = "public async Task<IEnumerable<E>> GetActiveAsync(Expression<Func<E, bool>> expr)"; ////m17.Body.Add(new StatementGen( //// "return await Task.Run(() =>", //// "{", //// "\treturn GetActive(expr);", //// "});")); //var m18 = new ContainerGen(); //m18.Signature = "public E FirstOrDefault(Expression<Func<E, bool>> expr)"; //m18.Body.Add(new StatementGen( // "var entity = baseService.FirstOrDefault(expr);", // "if (entity != null)", // "\treturn entity;", // "return null;")); //var m19 = new ContainerGen(); //m19.Signature = "public async Task<E> FirstOrDefaultAsync(Expression<Func<E, bool>> expr)"; //m19.Body.Add(new StatementGen( // "var entity = await baseService.FirstOrDefaultAsync(expr);", // "if (entity != null)", // "\treturn entity;", // "return null;")); if (Data.RequestScope) { BaseDomainBody.Add(c0, new StatementGen("")); } BaseDomainBody.Add( c1, new StatementGen(""), s2, m2, new StatementGen("") //s3, m3, new StatementGen(""), //m4, new StatementGen(""), ////m5, new StatementGen(""), //m6, new StatementGen(""), ////m7, new StatementGen(""), //m8, new StatementGen(""), ////m9, new StatementGen(""), //m10, new StatementGen(""), //m101, new StatementGen(""), //m102, new StatementGen(""), ////m11, new StatementGen(""), //m12, new StatementGen(""), //m13, new StatementGen(""), //m141, new StatementGen(""), //m1411, new StatementGen(""), //m142, new StatementGen(""), //m1422, new StatementGen(""), //m14, new StatementGen(""), //m15, new StatementGen(""), ////m16, new StatementGen(""), ////m17, new StatementGen(""), //m18, new StatementGen(""), //m19, new StatementGen("") ); }
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("")); }
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); }
public void GenerateBaseRepositoryBody() { var s12 = new StatementGen( "protected `context` context;"); //"public bool AutoSave { get; set; }"); var c3 = new ContainerGen(); c3.Signature = "public BaseRepository(IUnitOfWork uow)"; c3.Body.Add(new StatementGen( //"AutoSave = true;", "this.context = uow.Context;")); var c4 = new ContainerGen(); c4.Signature = "public BaseRepository()"; //c4.Body.Add(new StatementGen( // "AutoSave = true;")); var m31 = new ContainerGen(); m31.Signature = "public int SaveChanges()"; m31.Body.Add(new StatementGen( "return context.SaveChanges();")); var m32 = new ContainerGen(); m32.Signature = "public async Task<int> SaveChangesAsync()"; m32.Body.Add(new StatementGen( "return await context.SaveChangesAsync();")); var m5 = new ContainerGen(); m5.Signature = "public DbRawSqlQuery<E> SqlQuery(string sql, params object[] parameters)"; m5.Body.Add(new StatementGen("return context.Database.SqlQuery<E>(sql, parameters);")); var m7 = new ContainerGen(); m7.Signature = "public DbRawSqlQuery<T> SqlQuery<T>(string sql, params object[] parameters)"; m7.Body.Add(new StatementGen("return context.Database.SqlQuery<T>(sql, parameters);")); var m9 = new ContainerGen(); m9.Signature = "public void ReInit(params object[] initParams)"; m9.Body.Add(new StatementGen( "//params order: 0/ uow", "context = ((IUnitOfWork)initParams[0]).Context;")); //"AutoSave = true;")); var cm10 = new CommentGen(type: CommentType.BLOCK); cm10.Add("******************** ABSTRACT AREA *********************"); var s11 = new StatementGen( "public abstract E Add(E entity);", //"public abstract Task<E> AddAsync(E entity);", "public abstract E Update(E entity);", //"public abstract Task<E> UpdateAsync(E entity);", "public abstract E Remove(E entity);", //"public abstract Task<E> RemoveAsync(E entity);", "public abstract E Remove(K key);", "public abstract IEnumerable<E> RemoveIf(Expression<Func<E, bool>> expr);", "public abstract IEnumerable<E> RemoveRange(IEnumerable<E> list);", //"public abstract Task<E> RemoveAsync(K key);", "public abstract E FindById(K key);", "public abstract Task<E> FindByIdAsync(K key);", "public abstract E FindActiveById(K key);", "public abstract Task<E> FindActiveByIdAsync(K key);", "public abstract E FindByIdInclude<TProperty>(K key, params Expression<Func<E, TProperty>>[] members);", "public abstract Task<E> FindByIdIncludeAsync<TProperty>(K key, params Expression<Func<E, TProperty>>[] members);", "public abstract E Activate(E entity);", "public abstract E Activate(K key);", "public abstract E Deactivate(E entity);", "public abstract E Deactivate(K key);", "public abstract IQueryable<E> GetActive();", "public abstract IQueryable<E> GetActive(Expression<Func<E, bool>> expr);", "public abstract E FirstOrDefault();", "public abstract E FirstOrDefault(Expression<Func<E, bool>> expr);", "public abstract Task<E> FirstOrDefaultAsync();", "public abstract Task<E> FirstOrDefaultAsync(Expression<Func<E, bool>> expr);", "public abstract E SingleOrDefault(Expression<Func<E, bool>> expr);", "public abstract Task<E> SingleOrDefaultAsync(Expression<Func<E, bool>> expr);"); BaseRepositoryBody.Add( s12, new StatementGen(""), c3, new StatementGen(""), c4, new StatementGen(""), m31, new StatementGen(""), m32, new StatementGen("", "#region SqlQuery"), m5, new StatementGen(""), m7, new StatementGen("#endregion", ""), m9, new StatementGen(""), cm10, new StatementGen(""), s11, new StatementGen("") ); }
public void GenerateBaseRepositoryBody() { var s12 = new StatementGen( "protected DbContext context;", "protected DbSet<E> dbSet;", "protected IUnitOfWork uow;"); var c4 = new ContainerGen(); c4.Signature = "public BaseRepository(IUnitOfWork uow)"; c4.Body.Add(new StatementGen( "this.uow = uow;", "this.context = uow.Context;", "this.dbSet = context.Set<E>();")); var c5 = new ContainerGen(); c5.Signature = "public BaseRepository(DbContext context)"; c5.Body.Add(new StatementGen( "this.context = context;", "this.dbSet = context.Set<E>();")); var m31 = new ContainerGen(); m31.Signature = "public int SaveChanges()"; m31.Body.Add(new StatementGen( "return context.SaveChanges();")); var m32 = new ContainerGen(); m32.Signature = "public async Task<int> SaveChangesAsync()"; m32.Body.Add(new StatementGen( "return await context.SaveChangesAsync();")); var m91 = new ContainerGen(); m91.Signature = "public void Reload(E entity)"; m91.Body.Add(new StatementGen( "context.Entry(entity).Reload();")); var m10 = new ContainerGen(); m10.Signature = "public EntityEntry<E> Create(E entity)"; m10.Body.Add(new StatementGen( "return dbSet.Add(entity);")); var m101 = new ContainerGen(); m101.Signature = "public void CreateRange(IEnumerable<E> entities)"; m101.Body.Add(new StatementGen( "dbSet.AddRange(entities);")); var m11 = new ContainerGen(); m11.Signature = "public EntityEntry<E> Remove(E entity)"; m11.Body.Add(new StatementGen( "return dbSet.Remove(entity);")); var m12 = new ContainerGen(); m12.Signature = "public EntityEntry<E> Remove(K key)"; m12.Body.Add(new StatementGen( "var entity = FindById(key);", "if (entity!=null)", "\treturn dbSet.Remove(entity);", "return null;")); var m13 = new ContainerGen(); m13.Signature = "public void RemoveIf(Expression<Func<E, bool>> expr)"; m13.Body.Add(new StatementGen( "dbSet.RemoveRange(dbSet.Where(expr).ToList());")); var m14 = new ContainerGen(); m14.Signature = "public void RemoveRange(IEnumerable<E> list)"; m14.Body.Add(new StatementGen( "dbSet.RemoveRange(list);")); var m141 = new ContainerGen(); m141.Signature = "public EntityEntry<E> Update(E entity)"; m141.Body.Add(new StatementGen( "var entry = context.Entry(entity);", "entry.State = EntityState.Modified;", "return entry;")); var m1421 = new ContainerGen(); m1421.Signature = "public EntityEntry<E> Update(E entity, Action<E> patchAction)"; m1421.Body.Add(new StatementGen( "var entry = dbSet.Attach(entity);", "patchAction.Invoke(entity);", "return entry;")); var m1411 = new ContainerGen(); m1411.Signature = "public void UpdateRange(IEnumerable<E> entities)"; m1411.Body.Add(new StatementGen( "foreach (var e in entities)", "\tcontext.Entry(e).State = EntityState.Modified;")); var m1412 = new ContainerGen(); m1412.Signature = "public EntityEntry<E> Attach(E entity)"; m1412.Body.Add(new StatementGen( "return dbSet.Attach(entity);")); var mm1 = new ContainerGen(); mm1.Signature = "public IQueryable<E> AsNoTracking()"; mm1.Body.Add(new StatementGen("return dbSet.AsNoTracking();")); var m142 = new ContainerGen(); m142.Signature = "public DbSet<E> Get()"; m142.Body.Add(new StatementGen("return dbSet;")); var m143 = new ContainerGen(); m143.Signature = "public IQueryable<E> Get(Expression<Func<E, bool>> expr)"; m143.Body.Add(new StatementGen("return dbSet.Where(expr);")); var m15 = new ContainerGen(); m15.Signature = "public E FirstOrDefault()"; m15.Body.Add(new StatementGen("return dbSet.FirstOrDefault();")); var m151 = new ContainerGen(); m151.Signature = "public E FirstOrDefault(Expression<Func<E, bool>> expr)"; m151.Body.Add(new StatementGen("return dbSet.FirstOrDefault(expr);")); var m16 = new ContainerGen(); m16.Signature = "public async Task<E> FirstOrDefaultAsync()"; m16.Body.Add(new StatementGen("return await dbSet.FirstOrDefaultAsync();")); var m161 = new ContainerGen(); m161.Signature = "public async Task<E> FirstOrDefaultAsync(Expression<Func<E, bool>> expr)"; m161.Body.Add(new StatementGen("return await dbSet.FirstOrDefaultAsync(expr);")); var m17 = new ContainerGen(); m17.Signature = "public E SingleOrDefault(Expression<Func<E, bool>> expr)"; m17.Body.Add(new StatementGen("return dbSet.SingleOrDefault(expr);")); var m18 = new ContainerGen(); m18.Signature = "public async Task<E> SingleOrDefaultAsync(Expression<Func<E, bool>> expr)"; m18.Body.Add(new StatementGen("return await dbSet.SingleOrDefaultAsync(expr);")); var cm10 = new CommentGen(type: CommentType.BLOCK); cm10.Add("******************** ABSTRACT AREA *********************"); var s11 = new StatementGen( "public abstract E FindById(K key);", "public abstract Task<E> FindByIdAsync(K key);"); BaseRepositoryBody.Add( s12, new StatementGen(""), c4, new StatementGen("")); if (Data.DIContainer == DIContainer.TContainer) { BaseRepositoryBody.Add(c5, new StatementGen("")); } BaseRepositoryBody.Add( m31, new StatementGen(""), m32, new StatementGen(""), m91, new StatementGen(""), m10, new StatementGen(""), m101, new StatementGen(""), m11, new StatementGen(""), m12, new StatementGen(""), m13, new StatementGen(""), m14, new StatementGen(""), m141, new StatementGen(""), m1411, new StatementGen(""), m1421, new StatementGen(""), m1412, new StatementGen(""), mm1, new StatementGen(""), m142, new StatementGen(""), m143, new StatementGen(""), m15, new StatementGen(""), m151, new StatementGen(""), m16, new StatementGen(""), m161, new StatementGen(""), m17, new StatementGen(""), m18, new StatementGen(""), cm10, new StatementGen(""), s11, new StatementGen("") ); }
public void GenerateBaseServiceBody() { var s1 = new StatementGen("protected IBaseRepository<E, K> repository;"); //var m3 = new ContainerGen(); //m3.Signature = "public bool AutoSave"; //m3.Body.Add(new StatementGen( // "get", // "{", // "\treturn repository.AutoSave;", // "}", // "set", // "{", // "\trepository.AutoSave = value;", // "}")); var m31 = new ContainerGen(); m31.Signature = "public int SaveChanges()"; m31.Body.Add(new StatementGen( "return repository.SaveChanges();")); var m32 = new ContainerGen(); m32.Signature = "public async Task<int> SaveChangesAsync()"; m32.Body.Add(new StatementGen( "return await repository.SaveChangesAsync();")); var m4 = new ContainerGen(); m4.Signature = "public E Add(E entity)"; m4.Body.Add(new StatementGen( "return repository.Add(entity);")); //var m5 = new ContainerGen(); //m5.Signature = "public async Task<E> AddAsync(E entity)"; //m5.Body.Add(new StatementGen( // "return await repository.AddAsync(entity);")); var m6 = new ContainerGen(); m6.Signature = "public E Update(E entity)"; m6.Body.Add(new StatementGen( "return repository.Update(entity);")); //var m7 = new ContainerGen(); //m7.Signature = "public async Task<E> UpdateAsync(E entity)"; //m7.Body.Add(new StatementGen( // "return await repository.UpdateAsync(entity);")); var m8 = new ContainerGen(); m8.Signature = "public E Remove(E entity)"; m8.Body.Add(new StatementGen( "return repository.Remove(entity);")); //var m9 = new ContainerGen(); //m9.Signature = "public async Task<E> RemoveAsync(E entity)"; //m9.Body.Add(new StatementGen( // "return await repository.RemoveAsync(entity);")); var m10 = new ContainerGen(); m10.Signature = "public E Remove(K key)"; m10.Body.Add(new StatementGen( "return repository.Remove(key);")); var m101 = new ContainerGen(); m101.Signature = "public IEnumerable<E> RemoveIf(Expression<Func<E, bool>> expr)"; m101.Body.Add(new StatementGen( "return repository.RemoveIf(expr);")); var m102 = new ContainerGen(); m102.Signature = "public IEnumerable<E> RemoveRange(IEnumerable<E> list)"; m102.Body.Add(new StatementGen( "return repository.RemoveRange(list);")); //var m11 = new ContainerGen(); //m11.Signature = "public async Task<E> RemoveAsync(K key)"; //m11.Body.Add(new StatementGen( // "return await repository.RemoveAsync(key);")); var m12 = new ContainerGen(); m12.Signature = "public E FindById(K key)"; m12.Body.Add(new StatementGen( "return repository.FindActiveById(key);")); var m13 = new ContainerGen(); m13.Signature = "public async Task<E> FindByIdAsync(K key)"; m13.Body.Add(new StatementGen("return await repository.FindActiveByIdAsync(key);")); var m141 = new ContainerGen(); m141.Signature = "public E Activate(E entity)"; m141.Body.Add(new StatementGen("return repository.Activate(entity);")); var m1411 = new ContainerGen(); m1411.Signature = "public E Activate(K key)"; m1411.Body.Add(new StatementGen("return repository.Activate(key);")); var m142 = new ContainerGen(); m142.Signature = "public E Deactivate(E entity)"; m142.Body.Add(new StatementGen("return repository.Deactivate(entity);")); var m1422 = new ContainerGen(); m1422.Signature = "public E Deactivate(K key)"; m1422.Body.Add(new StatementGen("return repository.Deactivate(key);")); var m14 = new ContainerGen(); m14.Signature = "public IQueryable<E> GetActive()"; m14.Body.Add(new StatementGen("return repository.GetActive();")); var m15 = new ContainerGen(); m15.Signature = "public IQueryable<E> GetActive(Expression<Func<E, bool>> expr)"; m15.Body.Add(new StatementGen("return repository.GetActive(expr);")); var m16 = new ContainerGen(); m16.Signature = "public E FirstOrDefault()"; m16.Body.Add(new StatementGen("return repository.FirstOrDefault();")); var m161 = new ContainerGen(); m161.Signature = "public E FirstOrDefault(Expression<Func<E, bool>> expr)"; m161.Body.Add(new StatementGen("return repository.FirstOrDefault(expr);")); var m17 = new ContainerGen(); m17.Signature = "public async Task<E> FirstOrDefaultAsync()"; m17.Body.Add(new StatementGen("return await repository.FirstOrDefaultAsync();")); var m171 = new ContainerGen(); m171.Signature = "public async Task<E> FirstOrDefaultAsync(Expression<Func<E, bool>> expr)"; m171.Body.Add(new StatementGen("return await repository.FirstOrDefaultAsync(expr);")); BaseServiceBody.Add( s1, new StatementGen(""), m31, new StatementGen(""), m32, new StatementGen("", "#region CRUD Area"), //m3, new StatementGen("" ), m4, new StatementGen(""), //m5, new StatementGen(""), m6, new StatementGen(""), //m7, new StatementGen(""), m8, new StatementGen(""), //m9, new StatementGen(""), m10, new StatementGen(""), m101, new StatementGen(""), m102, new StatementGen(""), //m11, new StatementGen(""), m12, new StatementGen(""), m13, new StatementGen(""), m141, new StatementGen(""), m1411, new StatementGen(""), m142, new StatementGen(""), m1422, new StatementGen(""), m14, new StatementGen(""), m15, new StatementGen(""), m16, new StatementGen(""), m161, new StatementGen(""), m17, new StatementGen(""), m171, new StatementGen("#endregion")); }