Пример #1
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(""));
        }
        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();")));
        }
Пример #3
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(""));
            }
        }
Пример #4
0
        //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(""));
        }
Пример #5
0
 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();");
     }
     mapConfig.Add("//\tAutoMapper.Mapper.Initialize(cfg as MapperConfigurationExpression);");
     var close = new StatementGen("//\t}", "//};");
     GlobalClassBody.Add(
         mapperConfig,
         open,
         mapConfig,
         close);
     #endregion
 }
Пример #6
0
        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(""));
            }
        }
Пример #7
0
        //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(""));
        }