使用ABP框架踩过的坑系列5
Posted szdlsoft
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用ABP框架踩过的坑系列5相关的知识,希望对你有一定的参考价值。
DDD领域驱动开发,实际是为复杂的业务场景而生的,为了让开发人员专注于业务,而非操作系统、数据库、网络之类的技术细节,必须要持久透明化:实际就是数据库系统DBMS的ORM抽象,目标就是业务不需要考虑数据是如何存储的,业务是独立于DBMS, 通俗讲业务系统不依赖某个DBMS实现,可以通过配置,灵活动态支持各种DBMS,如MS SQL、mysql、Orcacle等。ABP的目标是DDD, 在持久透明化这块,是用IRepository仓储抽象来做的,具体的DBMS实现的ORM就放在基础架构层Infrastructure里。理想是很美好的,实际还是有些坑的,我在做一个项目,当把MSSQL切换到MySql,还是遇到了问题!
public static class DbContextConfigurer { public static void Configure(DbContextOptionsBuilder<DbContext> builder, string connectionString) { //builder.UseSqlServer(connectionString); //使用MSSQL builder //.UseLoggerFactory(MyLoggerFactory) .UseMySql(connectionString); //使用MySql } public static void Configure(DbContextOptionsBuilder<DbContext> builder, DbConnection connection) { //builder.UseSqlServer(connection); builder.UseMySql(connection); } }
用ABP模板生成的项目中,如果使用.netcore和EFcore, 就会有DbContextConfigurer,只要安装了Microsoft.entityframeworkcore.mysql, 就可以使用UseMySql这个扩展方法,
"ConnectionStrings": { //"Default": "Server=localhost; Database=Db3; Trusted_Connection=True;" "Default": "Server=localhost;port=3306;database=Db3;uid=root;password=root;character set=utf8;Old Guids=true" },
appsettings.json 里ConnectionStrings,也使用Mysql连接字符串,然后再PMC(程序包控制台)执行
update-dabase
完成以上步骤,从MSSQL切换到了Mysql, 大功告成!理想是这样的,事实上也大部分可行,但会报一些莫名其妙的错误:约束错误!这些问题,一度让我很沮丧,甚至想放弃mysql, 迫于linux下装MSSQL的恐怖,还是坚持用mysql, 后在baidu的帮助下,找到了原因:Microsoft.entityframeworkcore.mysql 有问题,必须用melo.EntityFrameworkCore.MySql
然后再PMC(程序包控制台)执行
install-package Pomelo.EntityFrameworkCore.MySql
再执行update-dabase, 真的一切都好了,其实在ORM的抽象和实现方面,ABP都比较好的Module, 当家的是Microsoft的EF
Abp.EntityFrameWork.Common //EF的公共部分
Abp.EntityFrameWork //EF
Abp.EntityFrameWorkCore //EFCore
还有来自Java阵营的
Abp.NHibernate
还有几个比较流行的,
Abp.Dapper //据说性能是ef几十倍的Dapper
Abp.MongoDB //nosql,KV和文档数据库
Abp.MemoryDb //内存数据库,可用于单元测试,棒棒的
个人还是偏爱EF系列,特别是linux下用EFCore+Mysql, 是最佳组合,经过几个项目的实战,证明是个不错选择,可以放心使用!
ABP有关ORM的抽象机制,主要通过Repostitory和UnitOfWork来做的
namespace Abp.Domain.Repositories { /// <summary> /// This interface is implemented by all repositories to ensure implementation of fixed methods. 说白了就是CRUD得封装 /// </summary> /// <typeparam name="TEntity">Main Entity type this repository works on</typeparam> /// <typeparam name="TPrimaryKey">Primary key type of the entity</typeparam> public interface IRepository<TEntity, TPrimaryKey> : IRepository where TEntity : class, IEntity<TPrimaryKey> { #region Select/Get/Query ...#endregion #region Insert ...#endregion #region Update ...#endregion #region Delete ...#endregion #region Aggregates ...#endregion } }
/// <summary> /// Defines a unit of work. /// This interface is internally used by ABP. 实际上就是数据库连接和事务的封装 /// Use <see cref="IUnitOfWorkManager.Begin()"/> to start a new unit of work. /// </summary> public interface IUnitOfWork : IActiveUnitOfWork, IUnitOfWorkCompleteHandle { /// <summary> /// Unique id of this UOW. /// </summary> string Id { get; } /// <summary> /// Reference to the outer UOW if exists. /// </summary> IUnitOfWork Outer { get; set; } /// <summary> /// Begins the unit of work with given options. /// </summary> /// <param name="options">Unit of work options</param> void Begin(UnitOfWorkOptions options);
具体我们在Service中,只要用DI就可以注入了,其实现机理,以EFCore为例
/// <summary> /// This module is used to implement "Data Access Layer" in EntityFramework. 类似Mybatis的数据访问层,在ABP中叫基础架构 /// </summary> [DependsOn(typeof(AbpKernelModule))] public class AbpEntityFrameworkCoreModule : AbpModule { 。。。private void RegisterGenericRepositoriesAndMatchDbContexes() { var dbContextTypes = _typeFinder.Find(type => type.IsPublic && !type.IsAbstract && type.IsClass && typeof(AbpDbContext).IsAssignableFrom(type) ); if (dbContextTypes.IsNullOrEmpty()) { Logger.Warn("No class found derived from AbpDbContext."); return; } using (var repositoryRegistrar = IocManager.ResolveAsDisposable<IEfCoreGenericRepositoryRegistrar>()) { foreach (var dbContextType in dbContextTypes) { Logger.Debug("Registering DbContext: " + dbContextType.AssemblyQualifiedName); repositoryRegistrar.Object.RegisterForDbContext(dbContextType, IocManager); } } 。。。 } }
public class EfCoreGenericRepositoryRegistrar : IEfCoreGenericRepositoryRegistrar, ITransientDependency { 。。。public void RegisterForDbContext(Type dbContextType, IIocManager iocManager) { var autoRepositoryAttr = dbContextType.GetSingleAttributeOrNull<AutoRepositoryTypesAttribute>() ?? EfCoreAutoRepositoryTypes.Default; foreach (var entityTypeInfo in DbContextHelper.GetEntityTypeInfos(dbContextType)) { var primaryKeyType = EntityHelper.GetPrimaryKeyType(entityTypeInfo.EntityType); if (primaryKeyType == typeof(int)) { var genericRepositoryType = autoRepositoryAttr.RepositoryInterface.MakeGenericType(entityTypeInfo.EntityType); if (!iocManager.IsRegistered(genericRepositoryType)) { var implType = autoRepositoryAttr.RepositoryImplementation.GetGenericArguments().Length == 1 ? autoRepositoryAttr.RepositoryImplementation.MakeGenericType(entityTypeInfo.EntityType) : autoRepositoryAttr.RepositoryImplementation.MakeGenericType(entityTypeInfo.DeclaringType, entityTypeInfo.EntityType); iocManager.Register( genericRepositoryType, implType, DependencyLifeStyle.Transient ); } } var genericRepositoryTypeWithPrimaryKey = autoRepositoryAttr.RepositoryInterfaceWithPrimaryKey.MakeGenericType(entityTypeInfo.EntityType, primaryKeyType); if (!iocManager.IsRegistered(genericRepositoryTypeWithPrimaryKey)) { var implType = autoRepositoryAttr.RepositoryImplementationWithPrimaryKey.GetGenericArguments().Length == 2 ? autoRepositoryAttr.RepositoryImplementationWithPrimaryKey.MakeGenericType(entityTypeInfo.EntityType, primaryKeyType) : autoRepositoryAttr.RepositoryImplementationWithPrimaryKey.MakeGenericType(entityTypeInfo.DeclaringType, entityTypeInfo.EntityType, primaryKeyType); iocManager.Register( genericRepositoryTypeWithPrimaryKey, implType, DependencyLifeStyle.Transient ); } } } }
整个目的就是为了可以注入泛型的IRepostitory<TEntity, TPrimaryKey>,我们的service,就可以愉快的使用了
public IRepository<FoodMaterial, long> FoodMaterialRepository { get; set; } //属性注入 //或 private IRepository<FoodMaterialCategory, long> _categoryRepository; public FoodMaterialAppService(IRepository<FoodMaterial, long> repository , IRepository<FoodMaterialCategory, long> categoryRepository) : base(repository) { _categoryRepository = categoryRepository; } // 构造注入
总结:.net 已经拥抱开源和linux了,曾经的四大金刚LAMP(Linux+Apache+Mysql+php),如今.net也可以Cover了,跨平台(操作系统、数据库等),是一个上线系统的必须,特别部署到云上,linux+mysql的性价比还是比较高的,可真正要做到跨平台和持久透明,在设计DB时,还是要注意:不要用存储过程,所有逻辑必须在代码里完成,DB只是用来存储的;ABP在这个领域,给了我们OutOfBox开箱即用的很多Module, 可以让我们效率大大提升!
以上是关于使用ABP框架踩过的坑系列5的主要内容,如果未能解决你的问题,请参考以下文章