Exemplo n.º 1
0
        /// <summary>
        ///     生成服务接口,与服务方法
        /// </summary>
        /// <param name="type"></param>
        public static void Create(Type type, string idType, string entityPath)
        {
            //type = typeof(Theme);

            if (!type.BaseType.FullName.Contains("Entities"))
            {
                throw new ValidException("命名空间必须包含Entities");
            }

            var testBuilder = new StringBuilder();

            var filePath = BaseTemplate.GetFilePath(type, "Repositories", entityPath);
            var fileName = $"{filePath}\\I{type.Name}Repository.cs".Replace("test\\", "app\\");

            if (!File.Exists(fileName))
            {
                testBuilder.AppendLine(
                    "using System;using Alabo.Domains.Repositories.EFCore;using Alabo.Domains.Repositories.Model;");
                testBuilder.AppendLine("using System.Linq;");
                testBuilder.AppendLine("using MongoDB.Bson;");
                testBuilder.AppendLine("using Alabo.Domains.Repositories;");
                testBuilder.AppendLine($"using {type.Namespace};");

                if (testBuilder.ToString().IndexOf(type.Namespace, StringComparison.OrdinalIgnoreCase) == -1)
                {
                    testBuilder.AppendLine($"using {type.Namespace};");
                }

                testBuilder.AppendLine();
                testBuilder.AppendLine($"namespace {type.Namespace.Replace("Entities", "Repositories")} {{");
                ;

                testBuilder.AppendLine(
                    $"\tpublic interface I{type.Name}Repository : IRepository<{type.Name}, {idType}>  {{");

                testBuilder.AppendLine("\t}");
                testBuilder.AppendLine("}");
                //创建文件

                using (var stream = File.Create(fileName))
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(testBuilder);
                    }
                }
            }

            fileName = $"{filePath}\\{type.Name}Repository.cs".Replace("test\\", "app\\")
                       .Replace("Entities", "Services");
            if (!File.Exists(fileName))
            {
                testBuilder = new StringBuilder();
                testBuilder.AppendLine(
                    "using System;using Alabo.Domains.Repositories.EFCore;using Alabo.Domains.Repositories.Model;");
                testBuilder.AppendLine("using System.Linq;");
                testBuilder.AppendLine("using MongoDB.Bson;");
                testBuilder.AppendLine($"using {type.Namespace};");
                testBuilder.AppendLine("using Alabo.Domains.Repositories;");
                testBuilder.AppendLine("using Alabo.Datas.UnitOfWorks;");
                testBuilder.AppendLine($"using  {type.Namespace.Replace("Entities", "Repositories")};");

                if (testBuilder.ToString().IndexOf(type.Namespace, StringComparison.OrdinalIgnoreCase) == -1)
                {
                    testBuilder.AppendLine($"using {type.Namespace};");
                }

                testBuilder.AppendLine();
                testBuilder.AppendLine($"namespace {type.Namespace.Replace("Entities", "Repositories")} {{");
                ;

                testBuilder.AppendLine(
                    $"\tpublic class {type.Name}Repository : RepositoryMongo<{type.Name}, {idType}>,I{type.Name}Repository  {{");

                testBuilder.AppendLine($"\tpublic  {type.Name}Repository(IUnitOfWork unitOfWork) : base(unitOfWork){{");
                testBuilder.AppendLine("\t}");
                testBuilder.AppendLine("\t}");
                testBuilder.AppendLine("}");
                //创建文件

                using (var stream = File.Create(fileName))
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(testBuilder);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///     生成服务方法
        ///     生成服务接口,与服务方法
        /// </summary>
        /// <param name="type"></param>
        public static void CreateApiController(Type type, string idType, string entityPath)
        {
            //  type = typeof(WidgetHistory);
            //if (type.BaseType.FullName != typeof(MongoEntity).FullName) throw new ValidException("非Mongodb实体方法,不支持服务方法生成");

            if (!type.BaseType.FullName.Contains("Entities"))
            {
                throw new ValidException("命名空间必须包含Entities");
            }

            var testBuilder = new StringBuilder();
            var filePath    = BaseTemplate.GetFilePath(type, "Controllers", entityPath);

            DirectoryHelper.CreateIfNotExists(filePath);
            var fileName = filePath + $"\\Api{type.Name}Controller.cs";

            if (!File.Exists(fileName))
            {
                testBuilder.AppendLine(
                    "using System;using Alabo.Domains.Repositories.EFCore;using Alabo.Domains.Repositories.Model;");
                testBuilder.AppendLine("using System.Linq;");

                testBuilder.AppendLine("using Alabo.Domains.Entities;");
                testBuilder.AppendLine("using Microsoft.AspNetCore.Mvc;");
                testBuilder.AppendLine("using Alabo.Framework.Core.WebApis.Filter;");
                testBuilder.AppendLine("");
                testBuilder.AppendLine("using MongoDB.Bson;");
                testBuilder.AppendLine("using Alabo.Framework.Core.WebApis.Controller;");
                testBuilder.AppendLine("using Alabo.RestfulApi;using ZKCloud.Open.ApiBase.Configuration;");
                testBuilder.AppendLine("using Alabo.Domains.Services;");
                testBuilder.AppendLine("using Alabo.Web.Mvc.Attributes;");
                testBuilder.AppendLine("using Alabo.Web.Mvc.Controllers;");
                testBuilder.AppendLine($"using {type.Namespace};");
                testBuilder.AppendLine($"using {type.Namespace.Replace("Entities", "Services")};");

                if (testBuilder.ToString().IndexOf(type.Namespace, StringComparison.OrdinalIgnoreCase) == -1)
                {
                    testBuilder.AppendLine($"using {type.Namespace};");
                }

                testBuilder.AppendLine();
                testBuilder.AppendLine(
                    $"namespace {type.Namespace.Replace("Entities", "Controllers").Replace("Domain.", "")} {{");

                testBuilder.AppendLine("\t\t[ApiExceptionFilter]");
                testBuilder.AppendLine($"\t\t[Route(\"Api/{type.Name}/[action]\")]");
                testBuilder.AppendLine(
                    $"\t\tpublic class Api{type.Name}Controller : ApiBaseController<{type.Name},{idType}>  {{");

                testBuilder.AppendLine(
                    $" public Api{type.Name}Controller() : base() ");
                testBuilder.AppendLine("\t{ ");
                testBuilder.AppendLine($"\t\tBaseService = Resolve<I{type.Name}Service>();");
                testBuilder.AppendLine("\t}");
                testBuilder.AppendLine();

                testBuilder.AppendLine("\t}");
                testBuilder.AppendLine("}");
                //创建文件

                using (var stream = File.Create(fileName))
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(testBuilder);
                    }
                }
            }
        }