diff --git a/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs b/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs index 93b7306009..fda6740474 100644 --- a/src/AbpDesk/AbpDesk.Web.Mvc/AbpDeskWebMvcModule.cs +++ b/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) diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EfCoreRepositoryExtensions.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EfCoreRepositoryExtensions.cs index 100a66363d..84471504f7 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EfCoreRepositoryExtensions.cs +++ b/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(this IRepository repository) - where TEntity : class, IEntity, new() + where TEntity : class, IEntity { return repository.ToEfCoreRepository().DbContext; } public static DbSet GetDbSet(this IRepository repository) - where TEntity : class, IEntity, new() + where TEntity : class, IEntity { return repository.ToEfCoreRepository().DbSet; } public static IEfCoreRepository ToEfCoreRepository(this IRepository repository) - where TEntity : class, IEntity, new() + where TEntity : class, IEntity { var efCoreRepository = repository as IEfCoreRepository; if (efCoreRepository == null) diff --git a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs b/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs index 65a841e499..40eaa5ad39 100644 --- a/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs +++ b/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 InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = new CancellationToken()) + public override async Task InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default(CancellationToken)) { var savedEntity = DbSet.Add(entity).Entity; diff --git a/src/Volo.Abp.Identity/Volo/Abp/Identity/IdentityUser.cs b/src/Volo.Abp.Identity/Volo/Abp/Identity/IdentityUser.cs index 3003897b52..73eff54d4b 100644 --- a/src/Volo.Abp.Identity/Volo/Abp/Identity/IdentityUser.cs +++ b/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 /// /// Navigation property for the roles this user belongs to. /// - public virtual ICollection Roles { get; protected set; } + public virtual IReadOnlyList Roles => RolesCollection; + protected virtual List RolesCollection { get; set; } /// /// 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(); + RolesCollection = new List(); Claims = new Collection(); Logins = new Collection(); Tokens = new Collection(); @@ -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) diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs b/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs index 02c370121b..ee443f8940 100644 --- a/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs +++ b/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 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 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.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 UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)) { var filter = Builders.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.Filter.Eq(e => e.Id, id); + Collection.DeleteOne(filter); + } + + public override void Delete(Expression> predicate) + { + var filter = Builders.Filter.Where(predicate); Collection.DeleteOne(filter); } + public override Task DeleteAsync(TPrimaryKey id, CancellationToken cancellationToken = default(CancellationToken)) + { + var filter = Builders.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> predicate, CancellationToken cancellationToken = default(CancellationToken)) + { + var filter = Builders.Filter.Where(predicate); + return Collection.DeleteOneAsync(filter, cancellationToken: cancellationToken); + } + protected override IQueryable GetQueryable() { return Collection.AsQueryable(); diff --git a/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDbCoreRepositoryExtensions.cs b/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDbCoreRepositoryExtensions.cs new file mode 100644 index 0000000000..b553637002 --- /dev/null +++ b/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(this IRepository repository) + where TEntity : class, IEntity + { + return repository.ToMongoDbRepository().Database; + } + + public static IMongoCollection GetCollection(this IRepository repository) + where TEntity : class, IEntity + { + return repository.ToMongoDbRepository().Collection; + } + + public static string GetCollectionName(this IRepository repository) + where TEntity : class, IEntity + { + return repository.ToMongoDbRepository().CollectionName; + } + + public static IMongoDbRepository ToMongoDbRepository(this IRepository repository) + where TEntity : class, IEntity + { + var mongoDbRepository = repository as IMongoDbRepository; + if (mongoDbRepository == null) + { + throw new ArgumentException("Given repository does not implement " + typeof(IMongoDbRepository).AssemblyQualifiedName, nameof(repository)); + } + + return mongoDbRepository; + } + } +} \ No newline at end of file