Browse Source

mongodb integration enhancements.

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
46d0e1d0e4
  1. 8
      src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs
  2. 6
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EfCoreRepositoryExtensions.cs
  3. 2
      src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  4. 12
      src/Volo.Abp.Identity/Volo/Abp/Identity/IdentityUser.cs
  5. 60
      src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs
  6. 40
      src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDbCoreRepositoryExtensions.cs

8
src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs

@ -1,20 +1,20 @@
using AbpDesk.EntityFrameworkCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Volo.Abp;
using Volo.Abp.AspNetCore.Modularity;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Modularity;
namespace AbpDesk.Web.Mvc
{
[DependsOn(typeof(AbpAspNetCoreMvcModule), typeof(AbpDeskApplicationModule), typeof(AbpDeskEntityFrameworkCoreModule))]
[DependsOn(
typeof(AbpAspNetCoreMvcModule),
typeof(AbpDeskApplicationModule),
typeof(AbpDeskEntityFrameworkCoreModule))]
public class AbpDeskWebMvcModule : AbpModule
{
public override void ConfigureServices(IServiceCollection services)

6
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EfCoreRepositoryExtensions.cs

@ -8,19 +8,19 @@ namespace Volo.Abp.Domain.Repositories
public static class EfCoreRepositoryExtensions
{
public static DbContext GetDbContext<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
where TEntity : class, IEntity<TPrimaryKey>, new()
where TEntity : class, IEntity<TPrimaryKey>
{
return repository.ToEfCoreRepository().DbContext;
}
public static DbSet<TEntity> GetDbSet<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
where TEntity : class, IEntity<TPrimaryKey>, new()
where TEntity : class, IEntity<TPrimaryKey>
{
return repository.ToEfCoreRepository().DbSet;
}
public static IEfCoreRepository<TEntity, TPrimaryKey> ToEfCoreRepository<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
where TEntity : class, IEntity<TPrimaryKey>, new()
where TEntity : class, IEntity<TPrimaryKey>
{
var efCoreRepository = repository as IEfCoreRepository<TEntity, TPrimaryKey>;
if (efCoreRepository == null)

2
src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs

@ -75,7 +75,7 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
return savedEntity;
}
public override async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = new CancellationToken())
public override async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default(CancellationToken))
{
var savedEntity = DbSet.Add(entity).Entity;

12
src/Volo.Abp.Identity/Volo/Abp/Identity/IdentityUser.cs

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security.Claims;
@ -102,7 +103,8 @@ namespace Volo.Abp.Identity
/// <summary>
/// Navigation property for the roles this user belongs to.
/// </summary>
public virtual ICollection<IdentityUserRole> Roles { get; protected set; }
public virtual IReadOnlyList<IdentityUserRole> Roles => RolesCollection;
protected virtual List<IdentityUserRole> RolesCollection { get; set; }
/// <summary>
/// Navigation property for the claims this user possesses.
@ -132,7 +134,7 @@ namespace Volo.Abp.Identity
UserName = userName;
ConcurrencyStamp = Guid.NewGuid().ToString();
Roles = new Collection<IdentityUserRole>();
RolesCollection = new List<IdentityUserRole>();
Claims = new Collection<IdentityUserClaim>();
Logins = new Collection<IdentityUserLogin>();
Tokens = new Collection<IdentityUserToken>();
@ -142,12 +144,12 @@ namespace Volo.Abp.Identity
{
Check.NotNull(roleId, nameof(roleId));
if (Roles.Any(r => r.RoleId == roleId))
if (IsInRole(roleId))
{
return;
}
Roles.Add(new IdentityUserRole(guidGenerator.Create(), Id, roleId));
RolesCollection.Add(new IdentityUserRole(guidGenerator.Create(), Id, roleId));
}
public void RemoveRole(Guid roleId)
@ -159,7 +161,7 @@ namespace Volo.Abp.Identity
return;
}
Roles.RemoveAll(r => r.RoleId == roleId);
RolesCollection.RemoveAll(r => r.RoleId == roleId);
}
public bool IsInRole(Guid roleId)

60
src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs

@ -1,5 +1,8 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using Volo.Abp.Domain.Entities;
using Volo.Abp.MongoDB;
@ -36,12 +39,32 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
DatabaseProvider = databaseProvider;
}
//TODO: Override other methods?
public override async Task<TEntity> GetAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken))
{
var entity = await FindAsync(id, cancellationToken);
if (entity == null)
{
throw new EntityNotFoundException(typeof(TEntity), id);
}
return entity;
}
public override TEntity Insert(TEntity entity, bool autoSave = false)
{
Collection.InsertOne(entity);
return entity;
}
public override async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default(CancellationToken))
{
await Collection.InsertOneAsync(entity, cancellationToken: cancellationToken);
return entity;
}
public override TEntity Update(TEntity entity)
{
var filter = Builders<TEntity>.Filter.Eq(e => e.Id, entity.Id);
@ -49,12 +72,47 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
return entity;
}
public override void Delete(TEntity entity)
public override async Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
var filter = Builders<TEntity>.Filter.Eq(e => e.Id, entity.Id);
await Collection.ReplaceOneAsync(filter, entity, cancellationToken: cancellationToken);
return entity;
}
public override void Delete(TEntity entity)
{
Delete(entity.Id);
}
public override void Delete(TPrimaryKey id)
{
var filter = Builders<TEntity>.Filter.Eq(e => e.Id, id);
Collection.DeleteOne(filter);
}
public override void Delete(Expression<Func<TEntity, bool>> predicate)
{
var filter = Builders<TEntity>.Filter.Where(predicate);
Collection.DeleteOne(filter);
}
public override Task DeleteAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken))
{
var filter = Builders<TEntity>.Filter.Eq(e => e.Id, id);
return Collection.DeleteOneAsync(filter, cancellationToken: cancellationToken);
}
public override Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
return DeleteAsync(entity.Id, cancellationToken);
}
public override Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
{
var filter = Builders<TEntity>.Filter.Where(predicate);
return Collection.DeleteOneAsync(filter, cancellationToken: cancellationToken);
}
protected override IQueryable<TEntity> GetQueryable()
{
return Collection.AsQueryable();

40
src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDbCoreRepositoryExtensions.cs

@ -0,0 +1,40 @@
using System;
using MongoDB.Driver;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories.MongoDB;
namespace Volo.Abp.Domain.Repositories
{
public static class MongoDbCoreRepositoryExtensions
{
public static IMongoDatabase GetDatabase<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
where TEntity : class, IEntity<TPrimaryKey>
{
return repository.ToMongoDbRepository().Database;
}
public static IMongoCollection<TEntity> GetCollection<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
where TEntity : class, IEntity<TPrimaryKey>
{
return repository.ToMongoDbRepository().Collection;
}
public static string GetCollectionName<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
where TEntity : class, IEntity<TPrimaryKey>
{
return repository.ToMongoDbRepository().CollectionName;
}
public static IMongoDbRepository<TEntity, TPrimaryKey> ToMongoDbRepository<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
where TEntity : class, IEntity<TPrimaryKey>
{
var mongoDbRepository = repository as IMongoDbRepository<TEntity, TPrimaryKey>;
if (mongoDbRepository == null)
{
throw new ArgumentException("Given repository does not implement " + typeof(IMongoDbRepository<TEntity, TPrimaryKey>).AssemblyQualifiedName, nameof(repository));
}
return mongoDbRepository;
}
}
}
Loading…
Cancel
Save