Commit 3c7acca0 authored by 阿星Plus's avatar 阿星Plus

MongoDb support

parent b4171fc1
......@@ -3,13 +3,12 @@
namespace Plus.MongoDb
{
/// <summary>
/// Defines interface to obtain a <see cref="MongoDatabase"/> object.
/// IMongoDatabaseProvider
/// </summary>
public interface IMongoDatabaseProvider
{
/// <summary>
/// Gets the <see cref="MongoDatabase"/>.
/// </summary>
MongoDatabase Database { get; }
IMongoClient Client { get; }
IMongoDatabase Database { get; }
}
}
\ No newline at end of file
using MongoDB.Bson.Serialization;
using System;
namespace Plus.MongoDb.IdGenerator
{
public abstract class IntIdGeneratorBase : IIdGenerator
{
private readonly string _idCollectionName;
protected IntIdGeneratorBase(string idCollectionName)
{
_idCollectionName = idCollectionName;
}
protected IntIdGeneratorBase()
: this("IDs")
{
}
public object GenerateId(object container, object document)
{
throw new NotImplementedException();
}
public bool IsEmpty(object id)
{
throw new NotImplementedException();
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Copyright>https://meowv.com</Copyright>
<Authors>阿星Plus</Authors>
<Company>https://meowv.com</Company>
<Product>Plus.MongoDb</Product>
<Title>Plus.MongoDb - .netcoreplus</Title>
<Description>.netcoreplus(Plus.MongoDb) - .NET Core 简易版快速开发框架</Description>
<RepositoryUrl>https://github.com/Meowv/.netcoreplus</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://github.com/Meowv/.netcoreplus</PackageProjectUrl>
<PackageIconUrl>https://avatars2.githubusercontent.com/u/13010050</PackageIconUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>plus;plus.mongodb;.netcoreplus;</PackageTags>
<PackageReleaseNotes>Plus.MongoDb</PackageReleaseNotes>
<Version>1.0.3</Version>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;1591</NoWarn>
<DocumentationFile>Plus.MongoDb.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<NoWarn>1701;1702;1591</NoWarn>
<DocumentationFile>Plus.MongoDb.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="mongocsharpdriver" Version="2.8.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Plus\Plus.csproj" />
</ItemGroup>
</Project>
using Plus.Modules;
using Plus.MongoDb.Configuration;
using Plus.MongoDb.Uow;
using System.Reflection;
namespace Plus.MongoDb
......@@ -17,6 +18,7 @@ namespace Plus.MongoDb
public override void Initialize()
{
IocManager.Register<IMongoDatabaseProvider, UnitOfWorkMongoDatabaseProvider>();
IocManager.RegisterAssembly(Assembly.GetExecutingAssembly());
}
}
......
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using Plus.Dependency;
using Plus.Domain.Entities;
using Plus.Domain.Entities.Auditing;
using Plus.Domain.Repositories;
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace Plus.MongoDb.Repositories
{
......@@ -10,8 +15,7 @@ namespace Plus.MongoDb.Repositories
/// MongoDB Repository
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public class MongoDbRepositoryBase<TEntity> : MongoDbRepositoryBase<TEntity, int>, IRepository<TEntity>
where TEntity : class, IEntity<int>
public abstract class MongoDbRepositoryBase<TEntity> : MongoDbRepositoryBase<TEntity, int>, IRepository<TEntity>, IRepository<TEntity, int>, IRepository, ITransientDependency where TEntity : class, IEntity<int>
{
public MongoDbRepositoryBase(IMongoDatabaseProvider databaseProvider)
: base(databaseProvider)
......@@ -24,24 +28,27 @@ namespace Plus.MongoDb.Repositories
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TPrimaryKey"></typeparam>
public class MongoDbRepositoryBase<TEntity, TPrimaryKey> : PlusRepositoryBase<TEntity, TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
public abstract class MongoDbRepositoryBase<TEntity, TPrimaryKey> : PlusRepositoryBase<TEntity, TPrimaryKey> where TEntity : class, IEntity<TPrimaryKey>
{
public virtual MongoDatabase Database
{
get { return _databaseProvider.Database; }
}
private readonly IMongoDatabaseProvider _databaseProvider;
public virtual IMongoDatabase Database => _databaseProvider.Database;
public virtual MongoCollection<TEntity> Collection
public abstract string CollectionName { get; }
public virtual IMongoCollection<TEntity> Collection
{
get
{
return _databaseProvider.Database.GetCollection<TEntity>(typeof(TEntity).Name);
string text = CollectionName;
if (Extensions.IsNullOrEmpty(CollectionName))
{
text = typeof(TEntity).Name;
}
return _databaseProvider.Database.GetCollection<TEntity>(text, null);
}
}
private readonly IMongoDatabaseProvider _databaseProvider;
public MongoDbRepositoryBase(IMongoDatabaseProvider databaseProvider)
{
_databaseProvider = databaseProvider;
......@@ -49,48 +56,84 @@ namespace Plus.MongoDb.Repositories
public override IQueryable<TEntity> GetAll()
{
return Collection.AsQueryable();
return Collection.AsQueryable(new AggregateOptions() { AllowDiskUse = true });
}
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.IsNull())
var val = Builders<TEntity>.Filter.Eq((TEntity e) => e.Id, id);
TEntity entity = IFindFluentExtensions.First(IMongoCollectionExtensions.Find(Collection, val, null), default);
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 async Task<TEntity> GetAsync(TPrimaryKey id)
{
FilterDefinition<TEntity> query = Builders<TEntity>.Filter.Eq((TEntity e) => e.Id, id);
TEntity entity = await IFindFluentExtensions.FirstAsync(IMongoCollectionExtensions.Find(Collection, query, null), default);
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);
FilterDefinition<TEntity> val = Builders<TEntity>.Filter.Eq((TEntity e) => e.Id, id);
return IFindFluentExtensions.FirstOrDefault(IMongoCollectionExtensions.Find(Collection, val, null), default);
}
public override TEntity Insert(TEntity entity)
{
Collection.Insert(entity);
SetCreationAuditProperties(entity);
CheckAndSetDefaultValue(entity);
Collection.InsertOne(entity, null, default);
return entity;
}
public override TEntity Update(TEntity entity)
{
Collection.Save(entity);
return entity;
throw new NotImplementedException("更新实体指定列及查询条件,暂未实现");
}
public override void Delete(TEntity entity)
{
Delete(entity.Id);
throw new NotImplementedException("删除实体,暂未实现");
}
public override void Delete(TPrimaryKey id)
{
var query = MongoDB.Driver.Builders.Query<TEntity>.EQ(e => e.Id, id);
Collection.Remove(query);
TEntity val = this.Get(id);
this.Delete(val);
}
protected virtual void SetCreationAuditProperties(object entityAsObj)
{
if (entityAsObj is IHasCreationTime val)
{
if (val.CreationTime == default)
{
val.CreationTime = DateTime.Now;
}
if (entityAsObj is ICreationAudited)
{
}
}
}
protected virtual void CheckAndSetDefaultValue(object entityAsObj)
{
PropertyInfo[] properties = entityAsObj.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in properties)
{
if (propertyInfo.PropertyType == typeof(string) && propertyInfo.GetValue(entityAsObj) == null)
{
propertyInfo.SetValue(entityAsObj, string.Empty);
}
}
}
}
}
\ No newline at end of file
using MongoDB.Driver;
using Plus.Dependency;
using Plus.Domain.Uow;
using Plus.MongoDb.Configuration;
namespace Plus.MongoDb.Uow
{
/// <summary>
/// Implements <see cref="IMongoDatabaseProvider"/> that gets database from active unit of work.
/// </summary>
public class UnitOfWorkMongoDatabaseProvider : IMongoDatabaseProvider, ITransientDependency
public class UnitOfWorkMongoDatabaseProvider : IMongoDatabaseProvider
{
public MongoDatabase Database
{
get
{
return ((MongoDbUnitOfWork)_currentUnitOfWork.Current).Database;
}
}
private IMongoDbModuleConfiguration _configuration;
public IMongoClient Client { get; private set; }
private readonly ICurrentUnitOfWorkProvider _currentUnitOfWork;
public IMongoDatabase Database { get; private set; }
public UnitOfWorkMongoDatabaseProvider(ICurrentUnitOfWorkProvider currentUnitOfWork)
public UnitOfWorkMongoDatabaseProvider(IMongoDbModuleConfiguration configuration)
{
_currentUnitOfWork = currentUnitOfWork;
_configuration = configuration;
Client = new MongoClient(_configuration.ConnectionString);
Database = Client.GetDatabase(_configuration.DatabaseName, null);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment