ABP 结合 MongoDB 集成依赖注入
程序员文章站
2023-11-03 13:10:45
1.我们再ABP项目添加一个.NET Core类库 类库名自定定义, 我这里定义为 TexHong_EMWX.MongoDb 添加NuGet包。 ABP mongocsharpdriver 添加 AbpMongoDbConfigurationExtensions.cs 添加 AbpMongoDbMo ......
1.我们再abp项目添加一个.net core类库 类库名自定定义, 我这里定义为 texhong_emwx.mongodb
添加nuget包。
abp
mongocsharpdriver
添加 abpmongodbconfigurationextensions.cs
/// <summary> /// 定义扩展方法 <see cref="imoduleconfigurations"/> 允许配置abp mongodb模块 /// </summary> public static class abpmongodbconfigurationextensions { /// <summary> /// 用于配置abp mongodb模块。 /// </summary> public static iabpmongodbmoduleconfiguration abpmongodb(this imoduleconfigurations configurations) { return configurations.abpconfiguration.get<iabpmongodbmoduleconfiguration>(); } }
添加 abpmongodbmoduleconfiguration.cs
internal class abpmongodbmoduleconfiguration : iabpmongodbmoduleconfiguration { public string connectionstring { get; set; } public string databasename { get; set; } }
添加 iabpmongodbmoduleconfiguration
public interface iabpmongodbmoduleconfiguration { string connectionstring { get; set; } string databasename { get; set; } }
添加 mongodbrepositorybase.cs
/// <summary> /// implements irepository for mongodb. /// </summary> /// <typeparam name="tentity">type of the entity for this repository</typeparam> public class mongodbrepositorybase<tentity> : mongodbrepositorybase<tentity, int>, irepository<tentity> where tentity : class, ientity<int> { public mongodbrepositorybase(imongodatabaseprovider databaseprovider) : base(databaseprovider) { } } /// <summary> /// implements irepository for mongodb. /// </summary> /// <typeparam name="tentity">type of the entity for this repository</typeparam> /// <typeparam name="tprimarykey">primary key of the entity</typeparam> public class mongodbrepositorybase<tentity, tprimarykey> : abprepositorybase<tentity, tprimarykey> where tentity : class, ientity<tprimarykey> { public virtual mongodatabase database { get { return _databaseprovider.database; } } public virtual mongocollection<tentity> collection { get { return _databaseprovider.database.getcollection<tentity>(typeof(tentity).name); } } private readonly imongodatabaseprovider _databaseprovider; public mongodbrepositorybase(imongodatabaseprovider databaseprovider) { _databaseprovider = databaseprovider; } public override iqueryable<tentity> getall() { return collection.asqueryable(); } public override tentity get(tprimarykey id) { var query = mongodb.driver.builders.query<tentity>.eq(e => e.id, id); var entity = collection.findone(query); if (entity == null) { throw new entitynotfoundexception("there is no such an entity with given primary key. entity type: " + typeof(tentity).fullname + ", primary key: " + id); } return entity; } public override tentity firstordefault(tprimarykey id) { var query = mongodb.driver.builders.query<tentity>.eq(e => e.id, id); return collection.findone(query); } public override tentity insert(tentity entity) { collection.insert(entity); return entity; } public override tentity update(tentity entity) { collection.save(entity); return entity; } public override void delete(tentity entity) { delete(entity.id); } public override void delete(tprimarykey id) { var query = mongodb.driver.builders.query<tentity>.eq(e => e.id, id); collection.remove(query); } }
添加 mongodbunitofwork.cs
/// <summary> /// implements unit of work for mongodb. /// </summary> public class mongodbunitofwork : unitofworkbase, itransientdependency { /// <summary> /// gets a reference to mongodb database. /// </summary> public mongodatabase database { get; private set; } private readonly iabpmongodbmoduleconfiguration _configuration; /// <summary> /// constructor. /// </summary> public mongodbunitofwork( iabpmongodbmoduleconfiguration configuration, iconnectionstringresolver connectionstringresolver, iunitofworkfilterexecuter filterexecuter, iunitofworkdefaultoptions defaultoptions) : base( connectionstringresolver, defaultoptions, filterexecuter) { _configuration = configuration; beginuow(); } #pragma warning disable protected override void beginuow() { //todo: mongoclientextensions.getserver(mongoclient)' is obsolete: 'use the new api instead. database = new mongoclient(_configuration.connectionstring) .getserver() .getdatabase(_configuration.databasename); } #pragma warning restore public override void savechanges() { } #pragma warning disable 1998 public override async task savechangesasync() { } #pragma warning restore 1998 protected override void completeuow() { } #pragma warning disable 1998 protected override async task completeuowasync() { } #pragma warning restore 1998 protected override void disposeuow() { } }
添加 unitofworkmongodatabaseprovider.cs
/// <summary> /// implements <see cref="imongodatabaseprovider"/> that gets database from active unit of work. /// </summary> public class unitofworkmongodatabaseprovider : imongodatabaseprovider, itransientdependency { public mongodatabase database { get { return _mongodbunitofwork.database; } } private readonly mongodbunitofwork _mongodbunitofwork; public unitofworkmongodatabaseprovider(mongodbunitofwork mongodbunitofwork) { _mongodbunitofwork = mongodbunitofwork; } }
添加 imongodatabaseprovider.cs
public interface imongodatabaseprovider { /// <summary> /// gets the <see cref="mongodatabase"/>. /// </summary> mongodatabase database { get; } }
添加 texhong_emwxmongodbmodule.cs
/// <summary> /// this module is used to implement "data access layer" in mongodb. /// </summary> [dependson(typeof(abpkernelmodule))] public class texhong_emwxmongodbmodule : abpmodule { public override void preinitialize() { iocmanager.register<iabpmongodbmoduleconfiguration, abpmongodbmoduleconfiguration>(); // 配置 monggodb 数据库地址与名称 iabpmongodbmoduleconfiguration abpmongodbmoduleconfiguration = configuration.modules.abpmongodb(); abpmongodbmoduleconfiguration.connectionstring = "mongodb://admin:123qwe@118.126.93.113:27017/texhong_em"; abpmongodbmoduleconfiguration.databasename = "texhong_em"; } public override void initialize() { iocmanager.registerassemblybyconvention(typeof(texhong_emwxmongodbmodule).getassembly()); iocmanager.register<mongodbrepositorybase<user, long>>(); } }
最后项目的架构
添加单元测试 mongodbappservice_tests.cs
public class mongodbappservice : texhong_emwxtestbase { private readonly mongodbrepositorybase<user,long> _mongodbuserrepositorybase; public mongodbappservice() { this._mongodbuserrepositorybase = resolve<mongodbrepositorybase<user, long>>(); } [fact] public async task createusers_test() { long id = (datetime.now.ticks - 621356256000000000) / 10000; await _mongodbuserrepositorybase.insertandgetidasync(new user() { id= id, name = "123", emailconfirmationcode = "1111", username = "2222" }); user user = _mongodbuserrepositorybase.get(id); } }
注意单元测试要引用 mongodb项目。
同时在testmodule.cs属性依赖 dependson 把mongodb 的 module添加进去,不然会导致运行失败无法注入。