Ejemplo n.º 1
0
        public static SyntaxNode CreatEFCoreDefaultInterfaceService(DatabaseServiceModel model, GeneratorModel generator)
        {
            IPluralize pluralizer       = new Pluralizer();
            string     namespace_string = generator.NamespaceString;

            List <String> usingStrings = model.ServiceUsingList;
            string        serviceName  = model.ServiceName;

            string dbName = generator.ApplicationDbName;

            var EFCoreString = new StringBuilder();

            if (usingStrings.Count > 0)
            {
                foreach (var usingstring in usingStrings)
                {
                    EFCoreString.Append($@"using {usingstring};");
                }
            }
            EFCoreString.Append($@"


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace {namespace_string}.Service.Factory
{{
    public interface I{serviceName}Service
    {{ 

        Task<{model.ClassName}> Get{serviceName}Async(Guid id);       

        Task<List<{model.ClassName}>> Get{pluralizer.Pluralize(serviceName)}Async(string query);
       
");
            if (model.RelationDbList.Count > 0)
            {
                foreach (var item in model.RelationDbList)
                {
                    EFCoreString.Append($@"
          Task<List<{model.ClassName}>> Get{pluralizer.Pluralize(serviceName)}From{item.RelationItemName}IdAsync(Guid id);
        
");
                }
            }

            EFCoreString.Append($@"
         Task<int> Update{serviceName}Async({model.ClassName} {model.ClassName.ToLower()});
       
        Task<int> Delete{serviceName}Async(params {model.ClassName}[] {model.DbName.ToLower()});
        
    }}
}}");
            var newNode = SyntaxFactory.ParseSyntaxTree(EFCoreString.ToString()).GetRoot().NormalizeWhitespace();

            return(newNode);
        }
Ejemplo n.º 2
0
        public static SyntaxNode CreatEFCoreCenterService(DatabaseServiceModel model, GeneratorModel generator)
        {
            IPluralize pluralizer       = new Pluralizer();
            string     namespace_string = generator.NamespaceString;

            List <String> usingStrings = model.ServiceUsingList;


            string dbName = generator.ApplicationDbName;

            var EFCoreString = new StringBuilder();

            if (usingStrings.Count > 0)
            {
                foreach (var usingstring in usingStrings)
                {
                    EFCoreString.Append($@"using {usingstring};");
                }
            }
            EFCoreString.Append($@"
using {namespace_string}.Database;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace {namespace_string}.Service
{{
    public partial class {generator.UniformServiceName}Service
    {{
        private readonly {dbName} DbContext = null;
        public {generator.UniformServiceName}Service({dbName} dbContext)
        {{
            DbContext = dbContext;
        }}

    
    }}
}}");
            var newNode = SyntaxFactory.ParseSyntaxTree(EFCoreString.ToString()).GetRoot().NormalizeWhitespace();

            return(newNode);
        }
Ejemplo n.º 3
0
 public GeneratorModel(string applicationDbName, string namespaceString, List <DatabaseServiceModel> db_List, string uniformServiceName, DatabaseServiceModel uniformModel) : this(applicationDbName, namespaceString, db_List)
 {
     UniformServiceName = uniformServiceName;
     UniformModel       = uniformModel;
 }
Ejemplo n.º 4
0
        public static SyntaxNode CreatEFCoreDefaultService(DatabaseServiceModel model, GeneratorModel generator)
        {
            IPluralize pluralizer       = new Pluralizer();
            string     namespace_string = generator.NamespaceString;

            List <String> usingStrings = model.ServiceUsingList;
            string        serviceName  = model.ServiceName;

            string dbName = generator.ApplicationDbName;

            var EFCoreString = new StringBuilder();

            if (usingStrings.Count > 0)
            {
                foreach (var usingstring in usingStrings)
                {
                    EFCoreString.Append($@"using {usingstring};");
                }
            }
            EFCoreString.Append($@"
using {namespace_string}.Database;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace {namespace_string}.Service
{{
    public partial class {generator.UniformServiceName}Service
    {{
       
        public async Task<{model.ClassName}> Get{serviceName}Async(Guid id)
        {{
            var result = await DbContext.{model.DbName}.AsNoTracking().FirstOrDefaultAsync(b => b.Id == id);
            return result;
        }}

        public async Task<List<{model.ClassName}>> Get{pluralizer.Pluralize(serviceName)}Async(string query)
        {{
            var result = await DbContext.{model.DbName}.AsNoTracking().ToListAsync();
            //if(!String.IsNullOrWhiteSpace(query))
            //{{
            //    var param = query.Split("","");
            //    result.Where(b=>b.)
            //}}
            return result;
        }}
");
            if (model.RelationDbList.Count > 0)
            {
                foreach (var item in model.RelationDbList)
                {
                    EFCoreString.Append($@"
         public async Task<List<{model.ClassName}>> Get{pluralizer.Pluralize(serviceName)}From{item.RelationItemName}IdAsync(Guid id)
        {{
            var result = await DbContext.{model.DbName}.Where(b=>b.{item.MemberName}==id).AsNoTracking().ToListAsync();
            //if(!String.IsNullOrWhiteSpace(query))
            //{{
            //    var param = query.Split("","");
            //    result.Where(b=>b.)
            //}}
            return result;
        }}     
");
                }
            }

            EFCoreString.Append($@"
        public async Task<int> Update{serviceName}Async({model.ClassName} {model.ClassName.ToLower()})
        {{
            var item = await DbContext.{model.DbName}.FirstOrDefaultAsync(b => b.Id == {model.ClassName.ToLower()}.Id);
            if (item == null)
            {{
                DbContext.{model.DbName}.Add({model.ClassName.ToLower()});
            }}
            else
            {{
                DbContext.Entry(item).CurrentValues.SetValues({model.ClassName.ToLower()});
            }}
            return await DbContext.SaveChangesAsync();
        }}

        public async Task<int> Add{pluralizer.Pluralize(serviceName)}Async(List<{model.ClassName}> {model.ClassName.ToLower()})
        {{
            DbContext.{model.DbName}.AddRange({model.ClassName.ToLower()});
            return await DbContext.SaveChangesAsync();
        }}

        public async Task<int> Delete{serviceName}Async(params {model.ClassName}[] {model.DbName.ToLower()})
        {{
            var removelist = new List<{model.ClassName}>();
            foreach (var item in {model.DbName.ToLower()})
            {{
                var Exist = await DbContext.{model.DbName}.FirstOrDefaultAsync(b => b.Id == item.Id);
                if (Exist != null)
                {{
                    removelist.Add(Exist);
                }}


            }}
            DbContext.{model.DbName}.RemoveRange(removelist);
            return await DbContext.SaveChangesAsync();
        }}
    }}
}}");
            var newNode = SyntaxFactory.ParseSyntaxTree(EFCoreString.ToString()).GetRoot().NormalizeWhitespace();

            return(newNode);
        }