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应 ...
随机推荐
- Linux apache自建证书搭建https
前言 搭建https有两种方式,分为单向认证和双向认证.单向认证就是传输的数据加密过了,但是不会校验客户端的来源,也就只有客户端验证服务端证书. 单向认证 1.安装mod_ssl ...
- webpack vue-cli2 配置打包测试环境
目前vue-cli2上原配置是只有开发环境dev和线上环境prod的配置,但是我们实际场景上还有很多需要一个测试环境test,下面就是对测试环境的配置,将测试环境和线上环境的打包代码分开就不需要切来切 ...
- js转义问题
js转义问题有很多场景,比如常见的根据某个字符串删除或者修改以及将某字符串传递至某个页面. 今天以一个简单的示例代码为例: <html> <head> <meta htt ...
- Leetcode 222:完全二叉树的节点个数
题目 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置. ...
- [python]pypy优化python性能
下载地址:https://pypy.org/download.html # python2.7版本 yum install pypy # python3.6版本https://bitbucket.or ...
- 【Python】解析Python中的迭代器
目录结构: contents structure [-] Iterator VS Iterable Itertools 模块 生成器(Generator) 在开始文章之前,先贴上一张Iterable. ...
- 基于webpack4的react开发环境配置
一.基础配置 1.init项目 mkdir react-webpack4-cook cd react-webpack4-cook mkdir src mkdir dist npm init -y 复制 ...
- Gossip和Redis集群原理
https://blog.csdn.net/weixin_33755847/article/details/89561666 http://redisbook.com/preview/cluster/ ...
- matlab学习笔记13_2匿名函数
一起来学matlab-matlab学习笔记13函数 13_2 匿名函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 https://ww2.mathworks.cn/help/m ...
- requests库学习案例
requests库使用流程 使用流程/编码流程 1.指定url 2.基于requests模块发起请求 3.获取响应对象中的数据值 4.持久化存储 分析案例 需求:爬取搜狗首页的页面数据 # 爬取搜狗首 ...