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应 ...
随机推荐
- mfs分布式文件系统,分布式存储,高可用(pacemaker+corosync+pcs),磁盘共享(iscsi),fence解决脑裂问题
一.MFS概述 MooseFS是一个分布式存储的框架,其具有如下特性:(1)通用文件系统,不需要修改上层应用就可以使用(那些需要专门api的dfs很麻烦!).(2)可以在线扩容,体系架构可伸缩性极强. ...
- Gaze Estimation学习笔记(2)-It's Written All Over Your Face Full-Face Appearance-Based Gaze Estimation
目录 前言 将完整脸部图像作为输入的空间权重CNN方法 将full-face image作为输入的原因 加入空间权重的CNN方法 基础CNN结构 空间权重机制 实验及分析 头部姿态.面部表现视线方向的 ...
- IIS 7中添加匿名访问FTP站点
1. 开启FTP和IIS服务: 2.打开IIS 管理器: 我电脑上是IIS 7.5 ,所以选择第一个并点击打开哦. 如果你想知道自己IIS的版本,打开帮助菜单: 3. 新建FTP站点: 4. 填写站点 ...
- windows获取管理员权限 操作文件
1.文件右键属性 2.安全tab 一路 “应用”和“确定”
- 字符串反转(java和js)
写在前面 关于字符串反转的奇技淫巧很多, 会一种就行了, 但是解锁更多姿势可谓艺多不压身啊~~ 正文 java https://www.cnblogs.com/binye-typing/p/92609 ...
- 图片上传: ajax-formdata-upload
传送门:https://www.cnblogs.com/qiumingcheng/p/6854933.html ajax-formdata-upload.html <!DOCTYPE html& ...
- 【C++】C++ 左值、右值、右值引用详解
左值.右值 在C++11中所有的值必属于左值.右值两者之一,右值又可以细分为纯右值.将亡值.在C++11中可以取地址的.有名字的就是左值,反之,不能取地址的.没有名字的就是右值(将亡值或纯右值).举个 ...
- 如何将业务代码写得像诗一样(使用注解+单例+工厂去掉一大波if和else判断)
1.订单控制器,提供一个根据商品id和银行渠道id计算商品折后价格的接口: import org.springframework.web.bind.annotation.GetMapping; imp ...
- Xcodebuild稳定性测试go脚本
[本文出自天外归云的博客园] 简单封装下xcodebuild test命令,写一个执行xcode测试的go程序,可以设定单case执行次数,也可以二次组装调用进行多个case的测试,代码如下: pac ...
- flutter 打包apk之后,安装在手机上无法访问网络解决方法
</application> <uses-permission android:name="android.permission.READ_PHONE_STATE" ...