public static SyntaxNode GenerateDb(GeneratorModel generatorModel)
        {
            string namespace_string         = generatorModel.NamespaceString;
            List <DatabaseServiceModel> dbs = generatorModel.Db_List;
            string DbContextName            = generatorModel.ApplicationDbName;

            var DefaultDBContextString = new StringBuilder($@"
using Microsoft.EntityFrameworkCore;
using System.Diagnostics.CodeAnalysis;
namespace {namespace_string}.Database
            
{{
    public class {DbContextName}: DbContext
    {{
        public {DbContextName}([NotNullAttribute] DbContextOptions options) : base(options)
        {{
            
        }}

        protected {DbContextName}()
        {{

        }}
");

            foreach (var db in dbs)
            {
                DefaultDBContextString.Append($"public DbSet<{db.ClassName}> {db.DbName} {{ get; set; }}");
            }
            DefaultDBContextString.Append(@" 
    } 
}"
                                          );
            var newNode = SyntaxFactory.ParseSyntaxTree(DefaultDBContextString.ToString()).GetRoot().NormalizeWhitespace();

            return(newNode);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }