[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

基础层

1、Cloud.Core项目是核心项目,主要实现缓存的操作、dapper操作、EFRepository、PageList、日志等操作

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

2、Cloud.Utility属于帮助类

领域层

3、Cloud.Entity实体对象,存放数据库映射实体、FluentAPI配置、枚举字典、DbContext等

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

4、Cloud.UnitOfWork,操作数据库的网关,里面封装了对仓储的操作、dapper的操作、事务等

服务层

5、Cloud.Service业务逻辑的实现

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

6、Cloud.Dto数据传输对象,实体对象不直接和表现层接触,通过dto互转

表现层

7、Cloud.Framework,表现层框架,封装了超类controller,全局授权过滤器,全局异常过滤器,ActionFilter,HtmlHelper等操作

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

8、Cloud.Boss启动项目

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

使用的技术

基于.NETCore2.0的ASP.NETCoreMVC

基于.NETCore2.0的EF

Dapper

Mysql

前端框架:aceAdmin(http://ace.jeka.by/#)

技术要点

1、实体基类定义

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

2、泛型仓储的封装

2.1、仓储接口的定义,泛型约束T必须是BaseEntity类型

publicinterfaceIRepositorywhereT:BaseEntity

{

DatabaseFacadeDatabase{get;}

IQueryableEntities{get;}

intSaveChanges();

TaskSaveChangesAsync();

voidDisposed();

boolDelete(Listentitys,boolisSaveChange=true);

boolDelete(Tentity,boolisSaveChange=true);

TaskDeleteAsync(Listentitys,boolisSaveChange=true);

TaskDeleteAsync(Tentity,boolisSaveChange=true);

TaskGetAsync(Expression>predicate=null);

Task>GetListAsync(Expression>predicate=null);

TGet(objectid);

TGet(Expression>predicate=null);

TaskGetAsync(objectid);

Task>LoadAsync(Expression>predicate=null);

boolInsert(Listentitys,boolisSaveChange=true);

boolInsert(Tentity,boolisSaveChange=true);

TaskInsertAsync(Listentitys,boolisSaveChange=true);

TaskInsertAsync(Tentity,boolisSaveChange=true);

boolUpdate(Listentitys,boolisSaveChange=true);

boolUpdate(Tentity,boolisSaveChange=true,ListupdatePropertyList=null);

TaskUpdateAsync(Listentitys,boolisSaveChange=true);

TaskUpdateAsync(Tentity,boolisSaveChange=true,ListupdatePropertyList=null);

}

publicinterfaceIRepositorywhereT:BaseEntity

{

DatabaseFacadeDatabase{get;}

IQueryableEntities{get;}

intSaveChanges();

TaskSaveChangesAsync();

voidDisposed();

boolDelete(Listentitys,boolisSaveChange=true);

boolDelete(Tentity,boolisSaveChange=true);

TaskDeleteAsync(Listentitys,boolisSaveChange=true);

TaskDeleteAsync(Tentity,boolisSaveChange=true);

TaskGetAsync(Expression>predicate=null);

Task>GetListAsync(Expression>predicate=null);

TGet(objectid);

TGet(Expression>predicate=null);

TaskGetAsync(objectid);

Task>LoadAsync(Expression>predicate=null);

boolInsert(Listentitys,boolisSaveChange=true);

boolInsert(Tentity,boolisSaveChange=true);

TaskInsertAsync(Listentitys,boolisSaveChange=true);

TaskInsertAsync(Tentity,boolisSaveChange=true);

boolUpdate(Listentitys,boolisSaveChange=true);

boolUpdate(Tentity,boolisSaveChange=true,ListupdatePropertyList=null);

TaskUpdateAsync(Listentitys,boolisSaveChange=true);

TaskUpdateAsync(Tentity,boolisSaveChange=true,ListupdatePropertyList=null);

}

2.2、仓储接口的实现

publicclassRepository:IRepositorywhereT:BaseEntity

{

DbContext_dbContext;

publicRepository(DbContextdbContext)

{

_dbContext=dbContext;

}

publicintSaveChanges()

{

return_dbContext.SaveChanges();

}

publicasyncTaskSaveChangesAsync()

{

returnawait_dbContext.SaveChangesAsync();

}

publicvoidDisposed()

{

thrownewException("不允许在这里释放上下文,请在UnitOfWork中操作");

_dbContext.Dispose();

}

#region插入数据

publicboolInsert(Tentity,boolisSaveChange=true)

{

_dbContext.Set().Add(entity);

if(isSaveChange)

{

returnSaveChanges()>0;

}

returnfalse;

}

publicasyncTaskInsertAsync(Tentity,boolisSaveChange=true)

{

_dbContext.Set().Add(entity);

if(isSaveChange)

{

returnawaitSaveChangesAsync()>0;

}

returnfalse;

}

publicboolInsert(Listentitys,boolisSaveChange=true)

{

_dbContext.Set().AddRange(entitys);

if(isSaveChange)

{

returnSaveChanges()>0;

}

returnfalse;

}

publicasyncTaskInsertAsync(Listentitys,boolisSaveChange=true)

{

_dbContext.Set().AddRange(entitys);

if(isSaveChange)

{

returnawaitSaveChangesAsync()>0;

}

returnfalse;

}

#endregion

#region更新数据

publicboolUpdate(Tentity,boolisSaveChange=true,ListupdatePropertyList=null)

{

if(entity==null)

{

returnfalse;

}

_dbContext.Set().Attach(entity);

if(updatePropertyList==null)

{

_dbContext.Entry(entity).State=EntityState.Modified;//全字段更新

}

else

{

updatePropertyList.ForEach(c=>{

_dbContext.Entry(entity).Property(c).IsModified=true;//部分字段更新的写法

});

}

if(isSaveChange)

{

returnSaveChanges()>0;

}

returnfalse;

}

publicboolUpdate(Listentitys,boolisSaveChange=true)

{

if(entitys==null||entitys.Count==0)

{

returnfalse;

}

entitys.ForEach(c=>{

Update(c,false);

});

if(isSaveChange)

{

returnSaveChanges()>0;

}

returnfalse;

}

publicasyncTaskUpdateAsync(Tentity,boolisSaveChange=true,ListupdatePropertyList=null)

{

if(entity==null)

{

returnfalse;

}

_dbContext.Set().Attach(entity);

if(updatePropertyList==null)

{

_dbContext.Entry(entity).State=EntityState.Modified;//全字段更新

}

else

{

updatePropertyList.ForEach(c=>{

_dbContext.Entry(entity).Property(c).IsModified=true;//部分字段更新的写法

});

}

if(isSaveChange)

{

returnawaitSaveChangesAsync()>0;

}

returnfalse;

}

publicasyncTaskUpdateAsync(Listentitys,boolisSaveChange=true)

{

if(entitys==null||entitys.Count==0)

{

returnfalse;

}

entitys.ForEach(c=>{

_dbContext.Set().Attach(c);

_dbContext.Entry(c).State=EntityState.Modified;

});

if(isSaveChange)

{

returnawaitSaveChangesAsync()>0;

}

returnfalse;

}

#endregion

#region删除

publicboolDelete(Tentity,boolisSaveChange=true)

{

_dbContext.Set().Attach(entity);

_dbContext.Set().Remove(entity);

returnisSaveChange?SaveChanges()>0:false;

}

publicboolDelete(Listentitys,boolisSaveChange=true)

{

entitys.ForEach(entity=>

{

_dbContext.Set().Attach(entity);

_dbContext.Set().Remove(entity);

});

returnisSaveChange?SaveChanges()>0:false;

}

publicvirtualasyncTaskDeleteAsync(Tentity,boolisSaveChange=true)

{

_dbContext.Set().Attach(entity);

_dbContext.Set().Remove(entity);

returnisSaveChange?awaitSaveChangesAsync()>0:false;

}

publicvirtualasyncTaskDeleteAsync(Listentitys,boolisSaveChange=true)

{

entitys.ForEach(entity=>

{

_dbContext.Set().Attach(entity);

_dbContext.Set().Remove(entity);

});

returnisSaveChange?awaitSaveChangesAsync()>0:false;

}

#endregion

publicIQueryableEntities=>_dbContext.Set().AsQueryable().AsNoTracking();

//publicasyncTask>EntitiesAsync=>Task.Run(()=>_dbContext.Set().AsQueryable().AsNoTracking());

publicDatabaseFacadeDatabase=>_dbContext.Database;

#region查找

publicTGet(objectid)

{

return_dbContext.Set().Find(id);

}

publicTGet(Expression>predicate=null)

{

return_dbContext.Set().Where(predicate).AsNoTracking().FirstOrDefault();

}

publicasyncTaskGetAsync(objectid)

{

returnawait_dbContext.Set().FindAsync(id);

}

publicasyncTaskGetAsync(Expression>predicate=null)

{

returnawait_dbContext.Set().Where(predicate).AsNoTracking().FirstOrDefaultAsync();

}

publicasyncTask>GetListAsync(Expression>predicate=null)

{

returnawait_dbContext.Set().Where(predicate).AsNoTracking().ToListAsync();

}

publicasyncTask>LoadAsync(Expression>predicate=null)

{

if(predicate==null)

{

predicate=c=>true;

}

returnawaitTask.Run(()=>_dbContext.Set().Where(predicate).AsNoTracking());

}

publicvoidDispose()

{

thrownewNotImplementedException();

}

#endregion

}

publicclassRepository:IRepositorywhereT:BaseEntity

{

DbContext_dbContext;

publicRepository(DbContextdbContext)

{

_dbContext=dbContext;

}

publicintSaveChanges()

{

return_dbContext.SaveChanges();

}

publicasyncTaskSaveChangesAsync()

{

returnawait_dbContext.SaveChangesAsync();

}

publicvoidDisposed()

{

thrownewException("不允许在这里释放上下文,请在UnitOfWork中操作");

_dbContext.Dispose();

}

#region插入数据

publicboolInsert(Tentity,boolisSaveChange=true)

{

_dbContext.Set().Add(entity);

if(isSaveChange)

{

returnSaveChanges()>0;

}

returnfalse;

}

publicasyncTaskInsertAsync(Tentity,boolisSaveChange=true)

{

_dbContext.Set().Add(entity);

if(isSaveChange)

{

returnawaitSaveChangesAsync()>0;

}

returnfalse;

}

publicboolInsert(Listentitys,boolisSaveChange=true)

{

_dbContext.Set().AddRange(entitys);

if(isSaveChange)

{

returnSaveChanges()>0;

}

returnfalse;

}

publicasyncTaskInsertAsync(Listentitys,boolisSaveChange=true)

{

_dbContext.Set().AddRange(entitys);

if(isSaveChange)

{

returnawaitSaveChangesAsync()>0;

}

returnfalse;

}

#endregion

#region更新数据

publicboolUpdate(Tentity,boolisSaveChange=true,ListupdatePropertyList=null)

{

if(entity==null)

{

returnfalse;

}

_dbContext.Set().Attach(entity);

if(updatePropertyList==null)

{

_dbContext.Entry(entity).State=EntityState.Modified;//全字段更新

}

else

{

updatePropertyList.ForEach(c=>{

_dbContext.Entry(entity).Property(c).IsModified=true;//部分字段更新的写法

});

}

if(isSaveChange)

{

returnSaveChanges()>0;

}

returnfalse;

}

publicboolUpdate(Listentitys,boolisSaveChange=true)

{

if(entitys==null||entitys.Count==0)

{

returnfalse;

}

entitys.ForEach(c=>{

Update(c,false);

});

if(isSaveChange)

{

returnSaveChanges()>0;

}

returnfalse;

}

publicasyncTaskUpdateAsync(Tentity,boolisSaveChange=true,ListupdatePropertyList=null)

{

if(entity==null)

{

returnfalse;

}

_dbContext.Set().Attach(entity);

if(updatePropertyList==null)

{

_dbContext.Entry(entity).State=EntityState.Modified;//全字段更新

}

else

{

updatePropertyList.ForEach(c=>{

_dbContext.Entry(entity).Property(c).IsModified=true;//部分字段更新的写法

});

}

if(isSaveChange)

{

returnawaitSaveChangesAsync()>0;

}

returnfalse;

}

publicasyncTaskUpdateAsync(Listentitys,boolisSaveChange=true)

{

if(entitys==null||entitys.Count==0)

{

returnfalse;

}

entitys.ForEach(c=>{

_dbContext.Set().Attach(c);

_dbContext.Entry(c).State=EntityState.Modified;

});

if(isSaveChange)

{

returnawaitSaveChangesAsync()>0;

}

returnfalse;

}

#endregion

#region删除

publicboolDelete(Tentity,boolisSaveChange=true)

{

_dbContext.Set().Attach(entity);

_dbContext.Set().Remove(entity);

returnisSaveChange?SaveChanges()>0:false;

}

publicboolDelete(Listentitys,boolisSaveChange=true)

{

entitys.ForEach(entity=>

{

_dbContext.Set().Attach(entity);

_dbContext.Set().Remove(entity);

});

returnisSaveChange?SaveChanges()>0:false;

}

publicvirtualasyncTaskDeleteAsync(Tentity,boolisSaveChange=true)

{

_dbContext.Set().Attach(entity);

_dbContext.Set().Remove(entity);

returnisSaveChange?awaitSaveChangesAsync()>0:false;

}

publicvirtualasyncTaskDeleteAsync(Listentitys,boolisSaveChange=true)

{

entitys.ForEach(entity=>

{

_dbContext.Set().Attach(entity);

_dbContext.Set().Remove(entity);

});

returnisSaveChange?awaitSaveChangesAsync()>0:false;

}

#endregion

publicIQueryableEntities=>_dbContext.Set().AsQueryable().AsNoTracking();

//publicasyncTask>EntitiesAsync=>Task.Run(()=>_dbContext.Set().AsQueryable().AsNoTracking());

publicDatabaseFacadeDatabase=>_dbContext.Database;

#region查找

publicTGet(objectid)

{

return_dbContext.Set().Find(id);

}

publicTGet(Expression>predicate=null)

{

return_dbContext.Set().Where(predicate).AsNoTracking().FirstOrDefault();

}

publicasyncTaskGetAsync(objectid)

{

returnawait_dbContext.Set().FindAsync(id);

}

publicasyncTaskGetAsync(Expression>predicate=null)

{

returnawait_dbContext.Set().Where(predicate).AsNoTracking().FirstOrDefaultAsync();

}

publicasyncTask>GetListAsync(Expression>predicate=null)

{

returnawait_dbContext.Set().Where(predicate).AsNoTracking().ToListAsync();

}

publicasyncTask>LoadAsync(Expression>predicate=null)

{

if(predicate==null)

{

predicate=c=>true;

}

returnawaitTask.Run(()=>_dbContext.Set().Where(predicate).AsNoTracking());

}

publicvoidDispose()

{

thrownewNotImplementedException();

}

#endregion

}

3、表部分字段更新实现

EF默认的更新方式是一个实体对应的表全部字段更新,那么我们想更新表的部分字段怎么处理?

首先定义需要更新的字段:

publicclassPropertyExpressionwhereT:BaseEntity

{

privatePropertyExpression(){}

privatestaticListpropertyList=newList();

publicstaticPropertyExpressionInit

{

get

{

propertyList.Clear();

returnnewPropertyExpression();

}

}

publicPropertyExpressionProperty(Expression>expr)

{

varrtn="";

if(expr.BodyisUnaryExpression)

{

rtn=((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;

}

elseif(expr.BodyisMemberExpression)

{

rtn=((MemberExpression)expr.Body).Member.Name;

}

elseif(expr.BodyisParameterExpression)

{

rtn=((ParameterExpression)expr.Body).Type.Name;

}

propertyList.Add(rtn);

returnthis;

}

publicListToList()

{

returnpropertyList;

}

}

publicclassPropertyExpressionwhereT:BaseEntity

{

privatePropertyExpression(){}

privatestaticListpropertyList=newList();

publicstaticPropertyExpressionInit

{

get

{

propertyList.Clear();

returnnewPropertyExpression();

}

}

publicPropertyExpressionProperty(Expression>expr)

{

varrtn="";

if(expr.BodyisUnaryExpression)

{

rtn=((MemberExpression)((UnaryExpression)expr.Body).Operand).Member.Name;

}

elseif(expr.BodyisMemberExpression)

{

rtn=((MemberExpression)expr.Body).Member.Name;

}

elseif(expr.BodyisParameterExpression)

{

rtn=((ParameterExpression)expr.Body).Type.Name;

}

propertyList.Add(rtn);

returnthis;

}

publicListToList()

{

returnpropertyList;

}

}

EF更新的处理

publicboolUpdate(Tentity,boolisSaveChange=true,ListupdatePropertyList=null)

{

if(entity==null)

{

returnfalse;

}

_dbContext.Set().Attach(entity);

if(updatePropertyList==null)

{

_dbContext.Entry(entity).State=EntityState.Modified;//全字段更新

}

else

{

updatePropertyList.ForEach(c=>{

_dbContext.Entry(entity).Property(c).IsModified=true;//部分字段更新的写法

});

}

if(isSaveChange)

{

returnSaveChanges()>0;

}

returnfalse;

}

publicboolUpdate(Tentity,boolisSaveChange=true,ListupdatePropertyList=null)

{

if(entity==null)

{

returnfalse;

}

_dbContext.Set().Attach(entity);

if(updatePropertyList==null)

{

_dbContext.Entry(entity).State=EntityState.Modified;//全字段更新

}

else

{

updatePropertyList.ForEach(c=>{

_dbContext.Entry(entity).Property(c).IsModified=true;//部分字段更新的写法

});

}

if(isSaveChange)

{

returnSaveChanges()>0;

}

returnfalse;

}

使用

varentity=_unitOfWork.SysRoleRep.Get(model.RoleId);

if(entity==null)

{

thrownewException("要查找的对象不存在");

}

entity.Name=model.RoleName;

varupdatedPropertyList=PropertyExpression.Init.Property(c=>c.Name).ToList();

_unitOfWork.SysRoleRep.Update(entity,true,updatedPropertyList);

varentity=_unitOfWork.SysRoleRep.Get(model.RoleId);

if(entity==null)

{

thrownewException("要查找的对象不存在");

}

entity.Name=model.RoleName;

varupdatedPropertyList=PropertyExpression.Init.Property(c=>c.Name).ToList();

_unitOfWork.SysRoleRep.Update(entity,true,updatedPropertyList);

4、动态加载实体到DbContext

publicclassEntityTypeConfiguration:IEntityTypeConfigurationwhereT:class

{

publicvoidConfigure(EntityTypeBuilderbuilder)

{

RelyConfigure(builder);

}

publicvirtualvoidRelyConfigure(EntityTypeBuilderbuilder)

{

}

}

publicclassEntityTypeConfiguration:IEntityTypeConfigurationwhereT:class

{

publicvoidConfigure(EntityTypeBuilderbuilder)

{

RelyConfigure(builder);

}

publicvirtualvoidRelyConfigure(EntityTypeBuilderbuilder)

{

}

}

publicclassSys_Error_LogConfiguration:EntityTypeConfiguration

{

publicoverridevoidRelyConfigure(EntityTypeBuilderbuilder)

{

builder.ToTable("sys_error_log");

builder.HasKey(x=>x.Id);

base.RelyConfigure(builder);

}

}

publicclassSys_Error_LogConfiguration:EntityTypeConfiguration

{

publicoverridevoidRelyConfigure(EntityTypeBuilderbuilder)

{

builder.ToTable("sys_error_log");

builder.HasKey(x=>x.Id);

base.RelyConfigure(builder);

}

}

publicclassEfDbContext:DbContext

{

publicEfDbContext(DbContextOptionsoptions):base(options)

{

}

//配置数据库连接

protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)

{

//optionsBuilder.UseSqlServer("xxxxconnectionstring");

base.OnConfiguring(optionsBuilder);

}

//第一次使用EF功能时执行一次,以后不再执行

protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder)

{

//获取当前程序集中有基类并且基类是泛型的类

vartypesToRegister=Assembly.GetExecutingAssembly().GetTypes().Where(c=>c.BaseType!=null&&c.BaseType.IsGenericType).ToList();

foreach(vartypeintypesToRegister)

{

//泛型定义相同

if(type.BaseType.GetGenericTypeDefinition()==typeof(EntityTypeConfiguration))

{

dynamicconfigurationInstance=Activator.CreateInstance(type);

modelBuilder.ApplyConfiguration(configurationInstance);

}

}

base.OnModelCreating(modelBuilder);

}

}

publicclassEfDbContext:DbContext

{

publicEfDbContext(DbContextOptionsoptions):base(options)

{

}

//配置数据库连接

protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)

{

//optionsBuilder.UseSqlServer("xxxxconnectionstring");

base.OnConfiguring(optionsBuilder);

}

//第一次使用EF功能时执行一次,以后不再执行

protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder)

{

//获取当前程序集中有基类并且基类是泛型的类

vartypesToRegister=Assembly.GetExecutingAssembly().GetTypes().Where(c=>c.BaseType!=null&&c.BaseType.IsGenericType).ToList();

foreach(vartypeintypesToRegister)

{

//泛型定义相同

if(type.BaseType.GetGenericTypeDefinition()==typeof(EntityTypeConfiguration))

{

dynamicconfigurationInstance=Activator.CreateInstance(type);

modelBuilder.ApplyConfiguration(configurationInstance);

}

}

base.OnModelCreating(modelBuilder);

}

}

5、工作单元

工作单元是对仓储和事务的封装

原理参考:https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

publicclassEfUnitOfWork:IUnitOfWork

{

privateEfDbContext_dbContext;//每次请求上下文只会创建一个

publicEfUnitOfWork(EfDbContextcontext)

{

this._dbContext=context;

}

publicintSaveChanges()

{

return_dbContext.SaveChanges();

}

publicasyncTaskSaveChangesAsync()

{

returnawait_dbContext.SaveChangesAsync();

}

privatebooldisposed=false;

protectedvirtualvoidDispose(booldisposing)

{

if(!this.disposed)

{

if(disposing)

{

_dbContext.Dispose();//随着工作单元的销毁而销毁

}

}

this.disposed=true;

}

publicvoidDispose()

{

Dispose(true);

GC.SuppressFinalize(this);

}

publicIDbContextTransactionBeginTransaction()

{

varscope=_dbContext.Database.BeginTransaction();

returnscope;

}

publicListSqlQuery(stringsql,objectparam=null)whereT:class

{

varcon=_dbContext.Database.GetDbConnection();

if(con.State!=ConnectionState.Open)

{

con.Open();

}

varlist=MysqlDapperReader.SqlQuery(con,sql,param);

returnlist;

//thrownewNotImplementedException();

}

publicTask>SqlQueryAsync(stringsql,objectparam=null)whereT:class

{

thrownewNotImplementedException();

}

#regionSysRepository

privateIRepository_sysUserRep;

publicIRepositorySysUserRep

{

get

{

if(_sysUserRep==null)

{

//vars=HttpContext.Current.Items["currentUser"];

//vars=HttpContext.Current.RequestServices.GetService>();

//HttpContext.RequestServices.GetService>();

_sysUserRep=newRepository(_dbContext);

}

return_sysUserRep;

}

}

privateIRepository_sysRoleRep;

publicIRepositorySysRoleRep

{

get

{

if(_sysRoleRep==null)

{

_sysRoleRep=newRepository(_dbContext);

}

return_sysRoleRep;

}

}

privateIRepository_sysRoleUserRep;

publicIRepositorySysRoleUserRep

{

get

{

if(_sysRoleUserRep==null)

{

_sysRoleUserRep=newRepository(_dbContext);

}

return_sysRoleUserRep;

}

}

privateIRepository_sysPermissionRep;

publicIRepositorySysPermissionRep

{

get

{

if(_sysPermissionRep==null)

{

_sysPermissionRep=newRepository(_dbContext);

}

return_sysPermissionRep;

}

}

privateIRepository_sysModuleRep;

publicIRepositorySysModuleRep

{

get

{

if(_sysModuleRep==null)

{

_sysModuleRep=newRepository(_dbContext);

}

return_sysModuleRep;

}

}

privateIRepository_sysErrorLogRep;

publicIRepositorySysErrorLogRep

{

get

{

if(_sysErrorLogRep==null)

{

_sysErrorLogRep=newRepository(_dbContext);

}

return_sysErrorLogRep;

}

}

privateIRepository_sysOperationLogRep;

publicIRepositorySysOperationLogRep

{

get

{

if(_sysOperationLogRep==null)

{

_sysOperationLogRep=newRepository(_dbContext);

}

return_sysOperationLogRep;

}

}

#endregion

}

publicclassEfUnitOfWork:IUnitOfWork

{

privateEfDbContext_dbContext;//每次请求上下文只会创建一个

publicEfUnitOfWork(EfDbContextcontext)

{

this._dbContext=context;

}

publicintSaveChanges()

{

return_dbContext.SaveChanges();

}

publicasyncTaskSaveChangesAsync()

{

returnawait_dbContext.SaveChangesAsync();

}

privatebooldisposed=false;

protectedvirtualvoidDispose(booldisposing)

{

if(!this.disposed)

{

if(disposing)

{

_dbContext.Dispose();//随着工作单元的销毁而销毁

}

}

this.disposed=true;

}

publicvoidDispose()

{

Dispose(true);

GC.SuppressFinalize(this);

}

publicIDbContextTransactionBeginTransaction()

{

varscope=_dbContext.Database.BeginTransaction();

returnscope;

}

publicListSqlQuery(stringsql,objectparam=null)whereT:class

{

varcon=_dbContext.Database.GetDbConnection();

if(con.State!=ConnectionState.Open)

{

con.Open();

}

varlist=MysqlDapperReader.SqlQuery(con,sql,param);

returnlist;

//thrownewNotImplementedException();

}

publicTask>SqlQueryAsync(stringsql,objectparam=null)whereT:class

{

thrownewNotImplementedException();

}

#regionSysRepository

privateIRepository_sysUserRep;

publicIRepositorySysUserRep

{

get

{

if(_sysUserRep==null)

{

//vars=HttpContext.Current.Items["currentUser"];

//vars=HttpContext.Current.RequestServices.GetService>();

//HttpContext.RequestServices.GetService>();

_sysUserRep=newRepository(_dbContext);

}

return_sysUserRep;

}

}

privateIRepository_sysRoleRep;

publicIRepositorySysRoleRep

{

get

{

if(_sysRoleRep==null)

{

_sysRoleRep=newRepository(_dbContext);

}

return_sysRoleRep;

}

}

privateIRepository_sysRoleUserRep;

publicIRepositorySysRoleUserRep

{

get

{

if(_sysRoleUserRep==null)

{

_sysRoleUserRep=newRepository(_dbContext);

}

return_sysRoleUserRep;

}

}

privateIRepository_sysPermissionRep;

publicIRepositorySysPermissionRep

{

get

{

if(_sysPermissionRep==null)

{

_sysPermissionRep=newRepository(_dbContext);

}

return_sysPermissionRep;

}

}

privateIRepository_sysModuleRep;

publicIRepositorySysModuleRep

{

get

{

if(_sysModuleRep==null)

{

_sysModuleRep=newRepository(_dbContext);

}

return_sysModuleRep;

}

}

privateIRepository_sysErrorLogRep;

publicIRepositorySysErrorLogRep

{

get

{

if(_sysErrorLogRep==null)

{

_sysErrorLogRep=newRepository(_dbContext);

}

return_sysErrorLogRep;

}

}

privateIRepository_sysOperationLogRep;

publicIRepositorySysOperationLogRep

{

get

{

if(_sysOperationLogRep==null)

{

_sysOperationLogRep=newRepository(_dbContext);

}

return_sysOperationLogRep;

}

}

#endregion

}

6、业务的实现方式

以前我是service中直接创建仓储然后用仓储操作数据库,方式如下:

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

这种方式比较繁琐,后来我将创建仓储统一放在工作单元中进行,在service中直接创建UnitOfWork,方式如下:

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

7、Service的动态注册

publicstaticclassAutoIocRegister

{

///

///动态注入IOC,注意类和接口的命名规则,接口在类名前面加"I"

///

///

///程序集名称

publicstaticvoidBatchAddScoped(thisIServiceCollectionservices,stringassemblyName)

{

varlibs=DependencyContext.Default.CompileLibraries;

varserviceLib=libs.Where(c=>c.Name.Contains(assemblyName)).FirstOrDefault();

varassembly=AssemblyLoadContext.Default.LoadFromAssemblyName(newAssemblyName(serviceLib.Name));

varserviceClassList=assembly.GetTypes().Where(c=>c.IsClass).ToList();

foreach(variteminserviceClassList)

{

varinterfaceName="I"+item.Name;

varinterfaceType=assembly.GetTypes().Where(c=>c.IsInterface&&c.Name==interfaceName).FirstOrDefault();

if(interfaceType==null)continue;

services.AddScoped(interfaceType,item);

}

}

}

publicstaticclassAutoIocRegister

{

///

///动态注入IOC,注意类和接口的命名规则,接口在类名前面加"I"

///

///

///程序集名称

publicstaticvoidBatchAddScoped(thisIServiceCollectionservices,stringassemblyName)

{

varlibs=DependencyContext.Default.CompileLibraries;

varserviceLib=libs.Where(c=>c.Name.Contains(assemblyName)).FirstOrDefault();

varassembly=AssemblyLoadContext.Default.LoadFromAssemblyName(newAssemblyName(serviceLib.Name));

varserviceClassList=assembly.GetTypes().Where(c=>c.IsClass).ToList();

foreach(variteminserviceClassList)

{

varinterfaceName="I"+item.Name;

varinterfaceType=assembly.GetTypes().Where(c=>c.IsInterface&&c.Name==interfaceName).FirstOrDefault();

if(interfaceType==null)continue;

services.AddScoped(interfaceType,item);

}

}

}

调用:

services.BatchAddScoped("Cloud.Service");

services.BatchAddScoped("Cloud.Service");

8、日志记录

日志分操作日志和错误日志,可以设置在数据库和文本中同时记录:

通过全局过滤器GlobalExceptionFilter和GlobalAuthorizeFilter处理

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

9、前端的封装(分页、弹出层、ajax等)

先放几张图吧,详细的以后再介绍

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

演示地址:http://cloud.eggtwo.com/main/index

●编号172,输入编号直达本文

更多推荐《25个技术类公众微信》

[.net开发框架].NETCore2.0+Mysql+AceAdmin搭建快速开发框架

上一篇:移动端之Web及app设计尺寸


下一篇:Appium+python+html生成饼图测试报告