public void GenerateOpenGetMethod(bool transaction = false)
        {
            var method     = new ContainerGen();
            var methodName = transaction ? "OpenTransactionGet" : "OpenGet";

            method.Signature = "public static T " + methodName + "<T>(Func<UnitOfWork, T> func)";

            method.Body.Add(new StatementGen(
                                "using (var scope = G.TContainer.CreateScope())"));

            var usingBody = new BodyGen();

            usingBody.Add(new StatementGen("var context = new `context`();"));
            if (transaction)
            {
                usingBody.Add(new StatementGen("var transaction = context.Database.BeginTransaction();"));
            }

            usingBody.Add(new StatementGen(
                              "scope.ManageResources(context" + (transaction ? ", transaction" : "") + ");",
                              "var res = func.Invoke(new UnitOfWork(scope, context" + (transaction ? ", transaction" : "") + "));"));

            usingBody.Add(new StatementGen(
                              "return res;"));
            method.Body.Add(usingBody);

            ContextManagerBody.Add(method, new StatementGen(""));
        }
Exemplo n.º 2
0
 public void GenerateExtensionNamespace()
 {
     ENamespace = new ContainerGen();
     ENamespace.ResolveMapping = ResolveMapping;
     ENamespace.Signature      = "namespace " + EInfo.Data.ProjectName + ".Models.Extensions";
     ENamespaceBody            = ENamespace.Body;
 }
        public void GenerateOpenAsyncMethod(bool transaction = false)
        {
            var method     = new ContainerGen();
            var methodName = transaction ? "OpenTransactionAsync" : "OpenAsync";

            method.Signature = "public static async void " + methodName + "(Action<UnitOfWork> action)";

            method.Body.Add(new StatementGen(
                                "await Task.Run(() =>",
                                "{"),
                            new StatementGen(
                                "using (var scope = G.TContainer.CreateScope())"));

            var usingBody = new BodyGen();

            usingBody.Add(new StatementGen("var context = new `context`();"));
            if (transaction)
            {
                usingBody.Add(new StatementGen("var transaction = context.Database.BeginTransaction();"));
            }

            usingBody.Add(new StatementGen(
                              "scope.ManageResources(context" + (transaction ? ", transaction" : "") + ");",
                              "action.Invoke(new UnitOfWork(scope, context" + (transaction ? ", transaction" : "") + "));"));

            method.Body.Add(usingBody);
            method.Body.Add(new StatementGen(
                                "});"));

            ContextManagerBody.Add(method, new StatementGen(""));
        }
Exemplo n.º 4
0
        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(""));
        }
Exemplo n.º 5
0
        public void GenerateIandClass()
        {
            var iWrapper = new ContainerGen();

            iWrapper.Signature = "public interface IWrapper";
            iWrapper.Body.Add(new StatementGen("object GetValue();"));

            NamespaceBody.Add(iWrapper, new StatementGen(""));
            var wrapper = new ContainerGen();

            wrapper.Signature = "public class Wrapper<T> : IWrapper";
            wrapper.Body.Add(new StatementGen(
                                 "public T Value { get; set; }", ""));

            var c00 = new ContainerGen();

            c00.Signature = "public Wrapper(T value)";
            c00.Body.Add(new StatementGen("Value = value;"));
            wrapper.Body.Add(c00);

            var c01 = new ContainerGen();

            c01.Signature = "public Wrapper()";
            wrapper.Body.Add(c01);

            var m1 = new ContainerGen();

            m1.Signature = "public object GetValue()";
            m1.Body.Add(new StatementGen("return Value;"));
            wrapper.Body.Add(m1);

            NamespaceBody.Add(wrapper, new StatementGen(""));
        }
Exemplo n.º 6
0
        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 GenerateContextManager()
        {
            ContextManager           = new ContainerGen();
            ContextManager.Signature = "public partial class ContextManager";
            ContextManagerBody       = ContextManager.Body;

            NamespaceBody.Add(ContextManager);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        public void GenerateBaseEntityExtension()
        {
            BaseEntityExtension           = new ContainerGen();
            BaseEntityExtension.Signature = "public partial class `entity` : BaseEntity";
            BaseEntityExtensionBody       = BaseEntityExtension.Body;

            NamespaceBody.Add(BaseEntityExtension, new StatementGen(""));
        }
Exemplo n.º 10
0
        public void GenerateNamespace()
        {
            Namespace           = new ContainerGen();
            Namespace.Signature = "namespace " + Data.ProjectName + ".Models.Repositories";
            NamespaceBody       = Namespace.Body;

            Content = Namespace;
        }
Exemplo n.º 11
0
        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(""));
        }
Exemplo n.º 12
0
        public void GenerateVMClass()
        {
            VMClass           = new ContainerGen();
            VMClass.Signature = "public partial class `entityVM`: BaseViewModel<`entity`>";
            VMClassBody       = VMClass.Body;

            NamespaceBody.Add(VMClass);
        }
        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(""));
        }
Exemplo n.º 14
0
        private void GenerateNamespace()
        {
            Namespace           = new ContainerGen();
            Namespace.Signature = "namespace " + Data.ProjectName + ".Global";
            NamespaceBody       = Namespace.Body;

            Content = Namespace;
        }
Exemplo n.º 15
0
        public void GenerateNamespace()
        {
            Namespace           = new ContainerGen();
            Namespace.Signature = "namespace " + Data.ContextNamespace;
            NamespaceBody       = Namespace.Body;

            Content = Namespace;
        }
Exemplo n.º 16
0
        public void GenerateNamespace()
        {
            Namespace           = new ContainerGen();
            Namespace.Signature = "namespace " + Data.ProjectName + ".Managers";
            NamespaceBody       = Namespace.Body;

            Content = Namespace;
        }
Exemplo n.º 17
0
        public void GenerateVMClass()
        {
            VMClass           = new ContainerGen();
            VMClass.Signature = "public partial class `entityVM`: BaseServiceModel<`entity`>";
            VMClassBody       = VMClass.Body;

            NamespaceBody.Add(VMClass, new StatementGen(""));
        }
Exemplo n.º 18
0
        public void GenerateNamespace()
        {
            Namespace           = new ContainerGen();
            Namespace.Signature = "namespace " + EInfo.Data.ProjectName + ".ViewModels";
            NamespaceBody       = Namespace.Body;

            Content = Namespace;
        }
Exemplo n.º 19
0
        public void GenerateUpdateVMClass()
        {
            UpdateVMClass           = new ContainerGen();
            UpdateVMClass.Signature = "public partial class Update`entityVM`: BaseUpdateServiceModel<Update`entityVM`, `entity`>";
            UpdateVMClassBody       = UpdateVMClass.Body;

            NamespaceBody.Add(UpdateVMClass);
        }
Exemplo n.º 20
0
        public void GenerateEntityExtension()
        {
            EntityExtension           = new ContainerGen();
            EntityExtension.Signature = "public partial class `entity` : BaseEntity";
            EntityExtensionBody       = EntityExtension.Body;

            NamespaceBody.Add(EntityExtension);
        }
Exemplo n.º 21
0
        public void GenerateBaseDomain()
        {
            BaseDomain           = new ContainerGen();
            BaseDomain.Signature = "public abstract partial class BaseDomain";
            BaseDomainBody       = BaseDomain.Body;

            NamespaceBody.Add(BaseDomain);
        }
        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);
        }
Exemplo n.º 23
0
        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);
        }
Exemplo n.º 24
0
        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);
        }
Exemplo n.º 25
0
        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);
        }
Exemplo n.º 26
0
        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(""));
            }
        }
Exemplo n.º 27
0
        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(""));
        }
Exemplo n.º 28
0
        //generate IBaseEntity
        public void GenerateIEntity()
        {
            var IEntity = new ContainerGen();

            IEntity.Signature = "public partial interface IBaseEntity";
            IEntity.Body.Add(
                new StatementGen(
                    "E To<E>();",
                    "void CopyTo(object dest);"
                    ));
            NamespaceBody.Add(IEntity, new StatementGen(""));
        }
Exemplo n.º 29
0
        private void GenerateInitMethod()
        {
            var initMethod = new ContainerGen();

            initMethod.Signature = "private static void DefaultConfigure()";

            initMethod.Body.Add(
                new StatementGen("ConfigureAutomapper();"),
                new StatementGen("ConfigureIoContainer();")
                );

            GlobalClassBody.Add(initMethod, new StatementGen(""));
        }
Exemplo n.º 30
0
        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(""));
        }