public override IEnumerable <TargetInfo> BuildCodeInfo() { foreach (var type in GetTypes()) { var areaNameSpace = T4Help.GenerateNameSpace(type, null); yield return(new TargetInfo() { NamespaceName = $"FastFrame.Repository.{areaNameSpace}", ImportNames = new string[] { $"FastFrame.Entity.{areaNameSpace}", "FastFrame.Database", "FastFrame.Infrastructure.Interface" }, Summary = T4Help.GetClassSummary(type, XmlDocDir) + "[数据访问]", Name = $"{type.Name}Repository", BaseNames = new string[] { $"BaseRepository<{type.Name}>", $"IRepository<{type.Name}>" }, Path = $"{TargetPath}", CategoryName = "class", Constructor = new ConstructorInfo() { Parms = new ParameterInfo[] { new ParameterInfo() { TypeName = "DataBase", DefineName = "context" }, new ParameterInfo() { TypeName = "ICurrentUserProvider", DefineName = "currentUserProvider" } }, Super = new string[] { "context", "currentUserProvider" } } }); } }
private IEnumerable <PropInfo> GetPropInfos(Type type) { var fieldNames = type.GetCustomAttribute <RelatedFieldAttribute>()?.FieldNames.ToList(); if (fieldNames == null) { fieldNames = type.GetProperties().Select(v => v.Name).ToList(); var baseFieldNames = type.BaseType.GetProperties().Select(v => v.Name).ToList(); fieldNames = fieldNames.Where(v => !baseFieldNames.Any(r => r == v)).ToList(); } if (!fieldNames.Contains("Id")) { fieldNames.Add("Id"); } foreach (var item in fieldNames) { var property = type.GetProperty(item); yield return(new PropInfo { Name = item, TypeName = T4Help.GetTypeName(property.PropertyType), Summary = T4Help.GetPropertySummary(property, XmlDocDir) }); } }
public override IEnumerable <Info.TargetInfo> GetTargetInfoList() { foreach (var type in GetTypes()) { var exportAttr = type.GetCustomAttribute <ExportAttribute>(); if (exportAttr == null || !exportAttr.ExportMarks.Contains(ExportMark.ViewModel)) { continue; } var areaNameSpace = T4Help.GenerateNameSpace(type, null); yield return(new Info.TargetInfo { NamespaceName = $"FastFrame.Application.{areaNameSpace}", ImportNames = new[] { "System", "FastFrame.Entity.Enums" } .Concat(type.GetProperties().Select(v => v.PropertyType.Namespace)) .Distinct(), Summary = T4Help.GetClassSummary(type, XmlDocDir), Name = $"{type.Name}ViewModel", Path = $"{TargetPath}\\{areaNameSpace}\\{type.Name}\\Dto\\{type.Name}ViewModel.template.cs", CategoryName = "class", PropInfos = GetPropInfos(type), BaseNames = new[] { "IViewModel" }, Constructor = new Info.ConstructorInfo { Modifier = "protected", } }); } }
public override IEnumerable <TargetInfo> GetTargetInfoList() { foreach (var type in GetTypes()) { var areaNameSpace = T4Help.GenerateNameSpace(type, null); yield return(new TargetInfo() { NamespaceName = $"FastFrame.Database.Mapping.{areaNameSpace}", ImportNames = new string[] { $"FastFrame.Entity.{areaNameSpace}", "Microsoft.EntityFrameworkCore.Metadata.Builders" }, Summary = T4Help.GetClassSummary(type, XmlDocDir), Name = $"{type.Name}Mapping", BaseNames = new string[] { $"BaseEntityMapping<{type.Name}>" }, Path = $"{TargetPath}\\{areaNameSpace}\\{type.Name}\\{type.Name}Mapping.template.cs", CategoryName = "class" }); } }
public TargetInfo GetTargetInfo(Type type) { var areaNameSpace = T4Help.GenerateNameSpace(type, null); var attrs = type.GetCustomAttributes <UniqueAttribute>().Select(x => new AttrInfo() { Name = "Unique", Parameters = x.UniqueNames.Select(y => $"\"{y}\"") }).Union(type.GetCustomAttributes <RelatedFieldAttribute>().Select(x => new AttrInfo() { Name = "RelatedField", Parameters = new[] { $"\"{x.DefaultName}\"" }.Union(x.OtherNames.Select(y => $"\"{y}\"")) })); return(new Info.TargetInfo() { NamespaceName = $"FastFrame.Application.{areaNameSpace}", ImportNames = new string[] { $"FastFrame.Entity.{areaNameSpace}", "FastFrame.Infrastructure", "System.ComponentModel.DataAnnotations", "FastFrame.Entity.Enums", "FastFrame.Entity.Basis", "FastFrame.Entity", "System.Collections.Generic", "System" } .Union(type.GetProperties() .Select(x => x.GetCustomAttribute <RelatedToAttribute>()) .Where(x => x != null) .SelectMany(x => new[] { x.RelatedType.Namespace, $"FastFrame.Application.{T4Help.GenerateNameSpace(x.RelatedType,"")}" })) .Distinct(), Summary = T4Help.GetClassSummary(type, XmlDocDir), Name = $"{type.Name}Dto", BaseNames = new string[] { $"BaseDto<{type.Name}>" } .Concat(typeof(IHaveMultiFile).IsAssignableFrom(type) ? new[] { "IHaveMultiFileDto" } : Array.Empty <string>()), Path = $"{TargetPath}\\{areaNameSpace}\\{type.Name}\\Dto\\{type.Name}Dto.template.cs", CategoryName = "class", PropInfos = GetPropInfos(type), AttrInfos = Array.Empty <AttrInfo>() /*attrs*/ }); }
public override IEnumerable <BuildTarget> Build(params string[] targetNames) { var types = this.GetTypes(); var listVueContent = File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "List.vue")); var addVueContent = File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "Add.vue")); foreach (var type in types) { if (targetNames.Length > 0 && !targetNames.Any(v => type.Name.StartsWith(v))) { continue; } var exportAttr = type.GetCustomAttribute <Entity.ExportAttribute>(); if (exportAttr == null || !exportAttr.ExportMarks.Contains(Entity.ExportMark.VuePage)) { continue; } var areaName = T4Help.GenerateNameSpace(type, null); var path = $"{TargetPath}\\{areaName}\\{type.Name}"; yield return(new BuildTarget { CodeBlock = ReplacePlaceholder(listVueContent, type), TargetPath = Path.Combine(path, "List.vue"), Forcibly = this.Forcibly }); yield return(new BuildTarget { CodeBlock = ReplacePlaceholder(addVueContent, type), TargetPath = Path.Combine(path, "Add.vue"), Forcibly = this.Forcibly }); } }
private string ReplacePlaceholder(string line, Type type) { return(line.Replace("{{AreaName}}", T4Help.GenerateNameSpace(type, "")) .Replace("{{ModuleName}}", type.Name) .Replace("{{Description}}", T4Help.GetClassSummary(type, this.XmlDocDir))); }
public override IEnumerable <TargetInfo> GetTargetInfoList() { foreach (var type in GetTypes()) { var exportAttr = type.GetCustomAttribute <ExportAttribute>(); if (exportAttr == null || !exportAttr.ExportMarks.Contains(ExportMark.Controller)) { continue; } var spaceName = T4Help.GenerateNameSpace(type, null); var name = type.Name; var summary = T4Help.GetClassSummary(type, XmlDocDir); var baseNames = new List <string>(); if (typeof(Entity.IHasManage).IsAssignableFrom(type)) { baseNames.Add($"BaseCURDController<{name}Dto>"); } else { baseNames.Add($"BaseController<{name}Dto>"); } yield return(new TargetInfo() { Summary = summary, Path = $"{TargetPath}\\{spaceName}\\{type.Name}\\{type.Name}Controller.template.cs", NamespaceName = $"FastFrame.WebHost.Controllers.{spaceName}", ImportNames = new string[] { //$"FastFrame.Entity.{spaceName}", $"FastFrame.Application.{spaceName}", //"FastFrame.Infrastructure.Permission", //"FastFrame.Infrastructure.Interface" }, CategoryName = "class", Name = $"{name}Controller", BaseNames = baseNames, FieldInfos = new FieldInfo[] { new FieldInfo() { TypeName = $"{name}Service", FieldName = "service" } }, Constructor = new Info.ConstructorInfo() { Parms = new ParameterInfo[] { new ParameterInfo() { TypeName = $"{name}Service", DefineName = "service" } }, Super = new string[] { "service" }, CodeBlock = new string[] { "this.service = service;" } }, //AttrInfos = new[] { // new AttrInfo() // { // Name="PermissionGroup", // Parameters=new string[]{ $"nameof({name})" , $"\"{summary}\"" } // } //} }); } }
public IEnumerable <PropInfo> GetPropInfos(Type type) { var instance = type.Assembly.CreateInstance(type.FullName); foreach (var item in type.GetProperties()) { if (item.Name == "Id") { continue; } if (item.GetCustomAttribute <ExcludeAttribute>() != null) { continue; } var summary = T4Help.GetPropertySummary(item, XmlDocDir); var defaultValue = instance.GetValue(item.Name)?.ToString(); if (defaultValue == null) { defaultValue = "null"; } if (T4Help.GetNullableType(item.PropertyType) == typeof(string)) { defaultValue = $"\"{defaultValue}\""; } if (T4Help.GetNullableType(item.PropertyType) == typeof(string)) { defaultValue = $"\"{defaultValue}\""; } yield return(new PropInfo() { Summary = summary, AttrInfos = GetAttrInfos(item), TypeName = T4Help.GetTypeName(item.PropertyType), Name = item.Name, DefaultValue = defaultValue }); if (TryGetAttribute <RelatedToAttribute>(item, out var relatedToAttribute)) { var relateTypeName = relatedToAttribute.RelatedType.Name; yield return(new PropInfo() { Summary = summary, TypeName = $"{relateTypeName}ViewModel", Name = item.Name.Replace("_Id", "") }); } } if (typeof(ITreeEntity).IsAssignableFrom(type)) { yield return new PropInfo { Name = "HasTreeChildren", DefaultValue = "false", Summary = "是否有下级", TypeName = "bool" } } ; if (typeof(IHaveMultiFile).IsAssignableFrom(type)) { yield return new PropInfo { Name = "Files", DefaultValue = "Array.Empty<ResourceModel>()", Summary = "附件", TypeName = "IEnumerable<ResourceModel>" } } ; }
public virtual void ModelEntityCreating(EntityTypeBuilder <T> entityTypeBuilder) { //Debugger.Launch(); var entityType = typeof(T); var currNameSpace = string.Join(",", entityType.Namespace.Split(new char[] { '.' }).Skip(2)); if (typeof(IEntity).IsAssignableFrom(entityType)) { /*指定主键*/ entityTypeBuilder.HasKey("Id"); entityTypeBuilder.Property("Id").ValueGeneratedNever(); entityTypeBuilder.ToTable($"{currNameSpace}_{entityType.Name}".ToLower()); entityTypeBuilder.HasComment(T4Help.GetClassSummary(entityType, AppDomain.CurrentDomain.BaseDirectory)); } else { entityTypeBuilder .HasNoKey() .ToView($"{currNameSpace}_{entityType.Name}".ToLower()); } /*过滤掉软删除的*/ if (typeof(IHasSoftDelete).IsAssignableFrom(entityType)) { entityTypeBuilder.Property <bool>("isdeleted"); entityTypeBuilder.HasQueryFilter(v => !EF.Property <bool>(v, "isdeleted")); entityTypeBuilder.HasIndex("isdeleted").HasDatabaseName($"Index_{entityType.Name}_isdeleted"); } if (typeof(IHasTenant).IsAssignableFrom(entityType)) { entityTypeBuilder.Property <string>("tenant_id").HasMaxLength(25); entityTypeBuilder.HasIndex("tenant_id").HasDatabaseName($"Index_{entityType.Name}_tenant_id"); } foreach (var item in typeof(T).GetProperties()) { if (item.GetCustomAttribute <NotMappedAttribute>() != null) { continue; } var propType = T4Help.GetNullableType(item.PropertyType); var propertyBuilder = entityTypeBuilder .Property(item.Name) .HasColumnName(item.Name.ToLower()); propertyBuilder.HasComment(T4Help.GetPropertySummary(item, AppDomain.CurrentDomain.BaseDirectory)); /*索引ID*/ if (typeof(IEntity).IsAssignableFrom(entityType)) { if (item.Name.EndsWith("Id") && item.Name != "Id") { entityTypeBuilder.HasIndex(item.Name).HasDatabaseName($"Index_{entityType.Name}_{item.Name}"); } if (propType == typeof(string)) { /*所有字符串,指定为unicode*/ propertyBuilder.IsUnicode(); /*所有ID,指定长度为25*/ if (item.Name.EndsWith("Id")) { propertyBuilder.HasMaxLength(25); } } } if (propType == typeof(decimal)) { propertyBuilder.HasPrecision(10, 2); } else if (propType.IsEnum) { propertyBuilder.HasConversion <string>().HasMaxLength(50); } } }
static void Main() { string typeName = ""; string rootPath = new DirectoryInfo("../../../../").FullName; var baseType = typeof(IEntity); var types = baseType .Assembly .GetTypes() .Where(x => baseType.IsAssignableFrom(x) && x.IsClass && !x.IsAbstract) .OrderBy(v => v.Namespace) .ThenBy(v => v.Name) .ToArray(); var typeGroups = types .Select((x, index) => new { Index = index + 1, Type = x }) .GroupBy(v => T4Help.GenerateNameSpace(v.Type, null)); START: Console.WriteLine("请输入要生成的类型:"); foreach (var g in typeGroups) { Console.WriteLine(); Console.WriteLine($"命名空间:{g.Key}"); foreach (var item in g) { var str = $"{item.Index}:{item.Type.Name}".PadRight(20, ' '); Console.Write(str); if (item.Index % 5 == 0) { Console.WriteLine(); } } Console.WriteLine(); } Console.WriteLine(); Console.WriteLine("0:全部"); INPUT: Console.Write(">:"); var inputIndex = Console.ReadLine(); if (int.TryParse(inputIndex, out var intIndex) && intIndex >= 0 && types.Length > intIndex - 1) { typeName = intIndex == 0 ? "" : types[intIndex].Name; } else if (types.Any(v => v.Name == inputIndex)) { typeName = inputIndex; } else { goto INPUT; } var codeBuildType = typeof(BaseCodeBuilder); var builds = codeBuildType.Assembly .GetTypes() .Where(x => codeBuildType.IsAssignableFrom(x) && !x.IsAbstract); foreach (var item in builds) { var constructorInfo = item.GetConstructors().FirstOrDefault(); var obj = constructorInfo.Invoke(new object[] { rootPath, baseType }); var builder = (IBaseCodeBuilder)obj; RunWrite(builder, new string[] { typeName }, v => Console.WriteLine(Path.GetFullPath(v.TargetPath))); } goto START; }
public Info.TargetInfo Build(Type type) { /*当前类型区域名称*/ string areaName = T4Help.GenerateNameSpace(type, null); /*所依赖[引用]的类型*/ IEnumerable <RelatedToAttribute> relatedTypes = type.GetProperties() .Select(x => x.GetCustomAttribute <RelatedToAttribute>()) .Where(x => x != null); /*要导入的命名空间*/ IEnumerable <string> importNames = relatedTypes .Select(x => T4Help.GenerateNameSpace(x.RelatedType, null)) .Union(new[] { areaName }) .SelectMany(x => new string[] { $"FastFrame.Entity.{x}", }) .Union(new string[] { $"FastFrame.Infrastructure.Interface", "FastFrame.Infrastructure", "FastFrame.Repository", "FastFrame.Entity.Basis", "System.Linq", "Microsoft.EntityFrameworkCore", "System.Threading.Tasks" }) .Union(type.GetProperties() .Select(x => x.GetCustomAttribute <RelatedToAttribute>()) .Where(x => x != null) .SelectMany(x => new[] { x.RelatedType.Namespace, $"FastFrame.Application.{T4Help.GenerateNameSpace(x.RelatedType,"")}" })) .Distinct(); /*要导入的依赖*/ var depends = relatedTypes .Select(x => x.RelatedType.Name) .Union(new[] { "User", type.Name }) .Distinct() .Select(x => new { type = $"IRepository<{x}>", name = $"{x.ToFirstLower()}Repository" }); return(new Info.TargetInfo() { NamespaceName = $"FastFrame.Application.{areaName}", ImportNames = importNames, Name = $"{type.Name}Service", CategoryName = "class", BaseNames = new string[] { $"BaseService<{type.Name}, {type.Name}Dto>" }, FieldInfos = depends.Select(x => new FieldInfo() { FieldName = x.name, TypeName = x.type }), Constructor = new Info.ConstructorInfo() { Parms = depends.Select(x => new ParameterInfo() { TypeName = x.type, DefineName = x.name }), Super = new string[] { $"{type.Name.ToFirstLower()}Repository" }, CodeBlock = depends.Select(x => $"this.{x.name}={x.name};") }, MethodInfos = GetMethods(type), Summary = $"{T4Help.GetClassSummary(type, XmlDocDir)} 服务实现", Path = $"{TargetPath}\\{areaName}\\{type.Name}\\{type.Name}Service.template.cs" }); }