ABP 结合 MongoDB 集成依赖注入
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 - ) / ;
await _mongoDbUserRepositoryBase.InsertAndGetIdAsync(new User() { Id= Id, Name = "", EmailConfirmationCode = "", UserName = "" });
User user = _mongoDbUserRepositoryBase.Get(Id);
}
}
注意单元测试要引用 MongoDb项目。
同时在TestModule.cs属性依赖 DependsOn 把Mongodb 的 Module添加进去,不然会导致运行失败无法注入。

源码下载:https://download.csdn.net/download/liaoyide/11742718
ABP 结合 MongoDB 集成依赖注入的更多相关文章
- 基于ABP模块组件与依赖注入组件的项目插件开发
注意,阅读本文,需要先阅读以下两篇文章,并且对依赖注入有一定的基础. 模块系统:http://www.cnblogs.com/mienreal/p/4537522.html 依赖注入:http://w ...
- Asp.Net Core 3.1 Api 集成Abp项目依赖注入
Abp 框架 地址https://aspnetboilerplate.com/ 我们下面来看如何在自己的项目中集成abp的功能 我们新建core 3.1 API项目和一个core类库 然后 两个项目都 ...
- ABP+AdminLTE+Bootstrap Table权限管理系统第四节--仓储,服务,服务接口及依赖注入
在ABP框架中,仓储,服务,这块算是最为重要一块之一了.ABP框架提供了创建和组装模块的基础,一个模块能够依赖于另一个模块,一个程序集可看成一个模块, 一个模块可以通过一个类来定义这个模块,而给定义这 ...
- ABP中的依赖注入思想
在充分理解整个ABP系统架构之前首先必须充分了解ABP中最重要的依赖注入思想,在后面会具体举出一些实例来帮助你充分了解ABP中的依赖注入思想,在了解这个之前我们首先来看看什么是依赖注入?来看看维基百科 ...
- 基于DDD的.NET开发框架 - ABP日志Logger集成
返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...
- ABP(现代ASP.NET样板开发框架)系列之6、ABP依赖注入
点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之6.ABP依赖注入 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)” ...
- ABP框架 - 依赖注入
文档目录 本节内容: 什么是依赖注入 传统方式的问题 解决方案 构造器注入模式 属性注入模式 依赖注入框架 ABP 依赖注入基础 注册依赖 约定注入 辅助接口 自定义/直接 注册 使用IocManag ...
- ABP理论学习之依赖注入
返回总目录 本篇目录 什么是依赖注入 传统方式产生的问题 解决办法 依赖注入框架 ABP中的依赖注入基础设施 注册 解析 其他 ASP.NET MVC和ASP.NET Web API集成 最后提示 什 ...
- 基于DDD的.NET开发框架 - ABP依赖注入
返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...
随机推荐
- 5G 融合计费系统架构设计与实现(一)
5G 融合计费系统架构设计与实现(一) 随着5G商用临近,5G的各个子系统也在加紧研发调试,本人有兴全程参与5G中的融合计费系统(CCS)的设计.开发.联调工作.接下来将用几篇文章介绍我们在CCS实现 ...
- Linux系统实现虚拟内存有两种方法:交换分区(swap分区)和交换文件
Linux系统实现虚拟内存有两种方法:交换分区(swap分区)和交换文件 交换文件 查看内存:free -m , -m是显示单位为MB,-g单位GB 创建一个文件:touch /root/swapfi ...
- HIVE出现Read past end of RLE integer from compressed stream Stream for column 1 kind LENGTH position: 359 length: 359 range: 0错误
错误日志 Diagnostic Messages for this Task: Error: java.io.IOException: java.io.IOException: java.io.EOF ...
- odoo开发笔记 -- odoo权限管理
odoo框架 整体权限可以分为4个级别: (1) 菜单级别: 不属于指定菜单所包含组的用,看不到相应菜单.不安全,只是隐藏菜单,若用户知道菜单ID,仍然可以通过指定URL访问(2) 对象级别: 对某个 ...
- Java13新特性 -- switch表达式动态CDS档案(动态类数据共享归档)
支持在Java application执行之后进行动态archive.存档类将包括默认的基础层CDS存档中不存在的所有已加载的应用程序和库类.也就是说,在Java 13中再使用AppCDS的时候,就不 ...
- Java 各种时间日期相关的操作
目录 1.获取当前时间的时间戳 1.1.时间进制 1.2.获取毫秒级时间戳 1.3.获取纳秒级时间戳 2.java.util包 2.1.Data 2.2.Calendar 3.java.time包 3 ...
- Java之数组类型
如果我们有一组类型相同的变量.例如,5位同学的成绩,可以这么写 public class Main { public static void main(String[] args) { // 5位同学 ...
- K3Wise插件开发实战教程(全套)持续更新中。。。
这是林枫山自己编写制作的全套K3wise插件教程,欢迎下载学习. 下载目录链接如下(如果链接下载不了,请加QQ:714259796获取教程): 进度01-K3Wise数据表详解 下载学习文档 ...
- VS2015 控制台cl编译器全局环境变量配置
Visual C++的cl.exe编译器是微软推出的编译器 为了可以在CMD里使用cl.exe手工执行编译操作 设置环境变量 PATH C:\Program Files (x86)\Microsoft ...
- 【miscellaneous】【C/C++语言】UTF8与GBK字符编码之间的相互转换
UTF8与GBK字符编码之间的相互转换 C++ UTF8编码转换 CChineseCode 一 预备知识 1,字符:字符是抽象的最小文本单位.它没有固定的形状(可能是一个字形),而且没有值." ...