Browse Source

tag refactorings.

- Make methods virtual
- set injected instances to a protected setter-only property
- use protected setters and expose set methods for properties need validation
pull/7764/head
Ilkay Ilknur 6 years ago
parent
commit
ff5264ebd9
  1. 55
      modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/EntityTagAdminAppService.cs
  2. 2
      modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/TagAdminAppService.cs
  3. 1
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/CmsKitTagOptions.cs
  4. 15
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/DefaultTagDefinitionStore.cs
  5. 6
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityTag.cs
  6. 25
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityTagManager.cs
  7. 21
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/Tag.cs
  8. 3
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagEntityTypeDefiniton.cs
  9. 62
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs
  10. 24
      modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreEntityTagRepository.cs
  11. 2
      modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreTagRepository.cs

55
modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/EntityTagAdminAppService.cs

@ -1,19 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using Volo.CmsKit.Admin.Tags;
using Volo.CmsKit.Tags;
namespace Volo.CmsKit.Admin.Tags
{
public class EntityTagAdminAppService : CmsKitAdminAppServiceBase, IEntityTagAdminAppService
{
protected readonly ITagDefinitionStore _tagDefinitionStore;
protected readonly IEntityTagManager _entityTagManager;
protected readonly ITagManager _tagManager;
protected readonly ITagRepository _tagRepository;
protected readonly IEntityTagRepository _entityTagRepository;
protected ITagDefinitionStore TagDefinitionStore { get; }
protected IEntityTagManager EntityTagManager { get; }
protected ITagManager TagManager { get; }
protected ITagRepository TagRepository { get; }
protected IEntityTagRepository EntityTagRepository { get; }
public EntityTagAdminAppService(
ITagDefinitionStore tagDefinitionStore,
@ -22,60 +20,61 @@ namespace Volo.CmsKit.Admin.Tags
ITagRepository tagRepository,
IEntityTagRepository entityTagRepository)
{
_tagDefinitionStore = tagDefinitionStore;
_entityTagManager = entityTagManager;
_tagManager = tagManager;
_tagRepository = tagRepository;
_entityTagRepository = entityTagRepository;
TagDefinitionStore = tagDefinitionStore;
EntityTagManager = entityTagManager;
TagManager = tagManager;
TagRepository = tagRepository;
EntityTagRepository = entityTagRepository;
}
public async Task AddTagToEntityAsync(EntityTagCreateDto input)
public virtual async Task AddTagToEntityAsync(EntityTagCreateDto input)
{
var definition = await _tagDefinitionStore.GetTagEntityTypeDefinitionsAsync(input.EntityType);
var definition = await TagDefinitionStore.GetTagEntityTypeDefinitionsAsync(input.EntityType);
await CheckPolicyAsync(definition.CreatePolicy);
var tag = await _tagManager.GetOrAddAsync(input.EntityType, input.TagName, CurrentTenant?.Id);
var tag = await TagManager.GetOrAddAsync(input.EntityType, input.TagName, CurrentTenant?.Id);
await _entityTagManager.AddTagToEntityAsync(
await EntityTagManager.AddTagToEntityAsync(
tag.Id,
input.EntityType,
input.EntityId,
CurrentTenant?.Id);
}
public async Task RemoveTagFromEntityAsync(EntityTagRemoveDto input)
public virtual async Task RemoveTagFromEntityAsync(EntityTagRemoveDto input)
{
var definition = await _tagDefinitionStore.GetTagEntityTypeDefinitionsAsync(input.EntityType);
var definition = await TagDefinitionStore.GetTagEntityTypeDefinitionsAsync(input.EntityType);
await CheckPolicyAsync(definition.DeletePolicy);
await _entityTagManager.RemoveTagFromEntityAsync(
await EntityTagManager.RemoveTagFromEntityAsync(
input.TagId,
input.EntityType,
input.EntityId,
CurrentTenant?.Id);
}
public async Task SetEntityTagsAsync(EntityTagSetDto input)
public virtual async Task SetEntityTagsAsync(EntityTagSetDto input)
{
var definition = await _tagDefinitionStore.GetTagEntityTypeDefinitionsAsync(input.EntityType);
var definition = await TagDefinitionStore.GetTagEntityTypeDefinitionsAsync(input.EntityType);
await CheckPolicyAsync(definition.UpdatePolicy);
var existingTags = await _tagRepository.GetAllRelatedTagsAsync(input.EntityType, input.EntityId, CurrentTenant?.Id);
var existingTags =
await TagRepository.GetAllRelatedTagsAsync(input.EntityType, input.EntityId, CurrentTenant?.Id);
var deletedTags = existingTags.Where(x => !input.Tags.Contains(x.Name)).ToList();
var addedTags = input.Tags.Where(x => !existingTags.Any(a => a.Name == x));
await _entityTagRepository.DeleteManyAsync(deletedTags.Select(s => s.Id).ToArray());
await EntityTagRepository.DeleteManyAsync(deletedTags.Select(s => s.Id).ToArray());
foreach (var addedTag in addedTags)
{
var tag = await _tagManager.GetOrAddAsync(input.EntityType, addedTag, CurrentTenant?.Id);
var tag = await TagManager.GetOrAddAsync(input.EntityType, addedTag, CurrentTenant?.Id);
await _entityTagManager.AddTagToEntityAsync(tag.Id, input.EntityType, input.EntityId, CurrentTenant?.Id);
}
await EntityTagManager.AddTagToEntityAsync(tag.Id, input.EntityType, input.EntityId, CurrentTenant?.Id);
}
}
}
}
}

2
modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/TagAdminAppService.cs

@ -72,7 +72,7 @@ namespace Volo.CmsKit.Admin.Tags
x.EntityType.ToLower().Contains(input.Filter));
}
public async Task<List<TagDefinitionDto>> GetTagDefinitionsAsync()
public virtual async Task<List<TagDefinitionDto>> GetTagDefinitionsAsync()
{
var definitions = await TagManager.GetTagDefinitionsAsync();

1
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/CmsKitTagOptions.cs

@ -1,5 +1,4 @@
using JetBrains.Annotations;
using Volo.CmsKit.Tags;
namespace Volo.CmsKit.Tags
{

15
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/DefaultTagDefinitionStore.cs

@ -11,11 +11,11 @@ namespace Volo.CmsKit.Tags
{
public class DefaultTagDefinitionStore : ITagDefinitionStore, ITransientDependency
{
private readonly CmsKitTagOptions options;
protected CmsKitTagOptions CmsKitTagOptions { get; }
public DefaultTagDefinitionStore(IOptions<CmsKitTagOptions> options)
{
this.options = options.Value;
CmsKitTagOptions = options.Value;
}
/// <summary>
@ -28,7 +28,8 @@ namespace Volo.CmsKit.Tags
{
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
var result = options.EntityTypes.SingleOrDefault(x => x.EntityType == entityType) ?? throw new EntityNotTaggableException(entityType);
var result = CmsKitTagOptions.EntityTypes.SingleOrDefault(x => x.EntityType == entityType) ??
throw new EntityNotTaggableException(entityType);
return Task.FromResult(result);
}
@ -38,7 +39,7 @@ namespace Volo.CmsKit.Tags
/// </summary>
public virtual Task<List<TagEntityTypeDefiniton>> GetTagEntityTypeDefinitionListAsync()
{
return Task.FromResult(options.EntityTypes.ToList());
return Task.FromResult(CmsKitTagOptions.EntityTypes.ToList());
}
/// <summary>
@ -50,9 +51,9 @@ namespace Volo.CmsKit.Tags
{
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
var definition = options.EntityTypes.SingleOrDefault(x => x.EntityType == entityType);
var isDefined = CmsKitTagOptions.EntityTypes.Any(x => x.EntityType == entityType);
return Task.FromResult(definition != null);
return Task.FromResult(isDefined);
}
}
}
}

6
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityTag.cs

@ -1,4 +1,6 @@
using System;
using JetBrains.Annotations;
using Volo.Abp;
using Volo.Abp.Domain.Entities;
using Volo.Abp.MultiTenancy;
@ -16,10 +18,10 @@ namespace Volo.CmsKit.Tags
{
}
internal EntityTag(Guid tagId, string entityId, Guid? tenantId = null)
internal EntityTag(Guid tagId, [NotNull] string entityId, Guid? tenantId = null)
{
TagId = tagId;
EntityId = entityId;
EntityId = Check.NotNullOrEmpty(entityId,nameof(entityId));
TenantId = tenantId;
}

25
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityTagManager.cs

@ -3,50 +3,47 @@ using System;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;
using Volo.CmsKit.Tags;
namespace Volo.CmsKit.Tags
{
public class EntityTagManager : DomainService, IEntityTagManager
{
protected readonly IEntityTagRepository _entityTagRepository;
protected readonly ITagDefinitionStore _tagDefinitionStore;
protected IEntityTagRepository EntityTagRepository { get; }
protected ITagDefinitionStore TagDefinitionStore { get; }
public EntityTagManager(
IEntityTagRepository entityTagRepository,
ITagDefinitionStore tagDefinitionStore)
{
_entityTagRepository = entityTagRepository;
_tagDefinitionStore = tagDefinitionStore;
EntityTagRepository = entityTagRepository;
TagDefinitionStore = tagDefinitionStore;
}
public async Task<EntityTag> AddTagToEntityAsync(
public virtual async Task<EntityTag> AddTagToEntityAsync(
[NotNull] Guid tagId,
[NotNull] string entityType,
[NotNull] string entityId,
[CanBeNull] Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
if (!await _tagDefinitionStore.IsDefinedAsync(entityType))
if (!await TagDefinitionStore.IsDefinedAsync(entityType))
{
throw new EntityNotTaggableException(entityType);
}
var entityTag = new EntityTag(tagId, entityId, tenantId);
return await _entityTagRepository.InsertAsync(entityTag, cancellationToken: cancellationToken);
return await EntityTagRepository.InsertAsync(entityTag, cancellationToken: cancellationToken);
}
public async Task RemoveTagFromEntityAsync(
public virtual async Task RemoveTagFromEntityAsync(
[NotNull] Guid tagId,
[NotNull] string entityType,
[NotNull] string entityId,
[CanBeNull] Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
var entityTag = await _entityTagRepository.FindAsync(tagId, entityId, tenantId, cancellationToken);
await _entityTagRepository.DeleteAsync(entityTag);
var entityTag = await EntityTagRepository.FindAsync(tagId, entityId, tenantId, cancellationToken);
await EntityTagRepository.DeleteAsync(entityTag, cancellationToken: cancellationToken);
}
}
}
}

21
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/Tag.cs

@ -8,11 +8,13 @@ namespace Volo.CmsKit.Tags
{
public class Tag : FullAuditedAggregateRoot<Guid>, IMultiTenant
{
public string EntityType { get; set; }
public virtual Guid? TenantId { get; set; }
public string Name { get; protected set; }
public Guid? TenantId { get; set; }
[NotNull]
public virtual string EntityType { get; protected set; }
[NotNull]
public virtual string Name { get; protected set; }
protected Tag()
{
@ -24,14 +26,19 @@ namespace Volo.CmsKit.Tags
[NotNull] string name,
Guid? tenantId = null) : base(id)
{
EntityType = Check.NotNullOrWhiteSpace(entityType, nameof(entityType), TagConsts.MaxEntityTypeLength);
SetName(name);
EntityType = Check.NotNullOrEmpty(entityType, nameof(entityType), TagConsts.MaxEntityTypeLength);
Name = Check.NotNullOrEmpty(name, nameof(name), TagConsts.MaxNameLength);
TenantId = tenantId;
}
public void SetName(string name)
public virtual void SetName(string name)
{
Name = Check.NotNullOrWhiteSpace(name, nameof(name), TagConsts.MaxNameLength);
}
public virtual void SetEntityType(string entityType)
{
EntityType = Check.NotNullOrEmpty(entityType, nameof(entityType), TagConsts.MaxEntityTypeLength);
}
}
}

3
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagEntityTypeDefiniton.cs

@ -25,13 +25,12 @@ namespace Volo.CmsKit.Tags
[CanBeNull] string deletePolicy = null) : base(createPolicy, updatePolicy, deletePolicy)
{
EntityType = Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
DisplayName = displayName;
}
public bool Equals(TagEntityTypeDefiniton other)
{
return EntityType == other.EntityType;
return EntityType == other?.EntityType;
}
}
}

62
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs

@ -1,35 +1,27 @@
using JetBrains.Annotations;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Services;
namespace Volo.CmsKit.Tags
{
public class TagManager : DomainService, ITagManager
{
private readonly ITagRepository _tagRepository;
private readonly ITagDefinitionStore _tagDefinitionStore;
protected ITagRepository TagRepository { get; }
protected ITagDefinitionStore TagDefinitionStore { get; }
public TagManager(
ITagRepository tagRepository,
ITagDefinitionStore tagDefinitionStore)
public TagManager(ITagRepository tagRepository, ITagDefinitionStore tagDefinitionStore)
{
_tagRepository = tagRepository;
_tagDefinitionStore = tagDefinitionStore;
TagRepository = tagRepository;
TagDefinitionStore = tagDefinitionStore;
}
public async Task<Tag> GetOrAddAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
public virtual async Task<Tag> GetOrAddAsync([NotNull] string entityType, [NotNull] string name,
Guid? tenantId = null, CancellationToken cancellationToken = default)
{
var entity = await _tagRepository.FindAsync(entityType, name, tenantId, cancellationToken);
var entity = await TagRepository.FindAsync(entityType, name, tenantId, cancellationToken);
if (entity == null)
{
@ -39,53 +31,43 @@ namespace Volo.CmsKit.Tags
return entity;
}
public async Task<Tag> InsertAsync(
Guid id,
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
public virtual async Task<Tag> InsertAsync(Guid id, [NotNull] string entityType, [NotNull] string name,
Guid? tenantId = null, CancellationToken cancellationToken = default)
{
if (await _tagRepository.AnyAsync(entityType, name, tenantId, cancellationToken))
if (!await TagDefinitionStore.IsDefinedAsync(entityType))
{
throw new TagAlreadyExistException(entityType, name);
throw new EntityNotTaggableException(entityType);
}
if (!await _tagDefinitionStore.IsDefinedAsync(entityType))
if (await TagRepository.AnyAsync(entityType, name, tenantId, cancellationToken))
{
throw new EntityNotTaggableException(entityType);
throw new TagAlreadyExistException(entityType, name);
}
return await _tagRepository.InsertAsync(
new Tag(
id,
entityType,
name,
tenantId),
return await TagRepository.InsertAsync(new Tag(id, entityType, name, tenantId),
cancellationToken: cancellationToken);
}
public async Task<Tag> UpdateAsync(
Guid id,
[NotNull] string name,
public virtual async Task<Tag> UpdateAsync(Guid id, [NotNull] string name,
CancellationToken cancellationToken = default)
{
var entity = await _tagRepository.GetAsync(id, cancellationToken: cancellationToken);
var entity = await TagRepository.GetAsync(id, cancellationToken: cancellationToken);
if (name != entity.Name &&
await _tagRepository.AnyAsync(entity.EntityType, name, entity.TenantId, cancellationToken))
await TagRepository.AnyAsync(entity.EntityType, name, entity.TenantId, cancellationToken))
{
throw new TagAlreadyExistException(entity.EntityType, name);
}
entity.SetName(name);
return await _tagRepository.UpdateAsync(entity, cancellationToken: cancellationToken);
return await TagRepository.UpdateAsync(entity, cancellationToken: cancellationToken);
}
public Task<List<TagEntityTypeDefiniton>> GetTagDefinitionsAsync(CancellationToken cancellationToken = default)
public virtual Task<List<TagEntityTypeDefiniton>> GetTagDefinitionsAsync(
CancellationToken cancellationToken = default)
{
return _tagDefinitionStore.GetTagEntityTypeDefinitionListAsync();
return TagDefinitionStore.GetTagEntityTypeDefinitionListAsync();
}
}
}

24
modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreEntityTagRepository.cs

@ -1,8 +1,6 @@
using JetBrains.Annotations;
using Nito.Disposables;
using System;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
@ -11,30 +9,32 @@ using Volo.CmsKit.EntityFrameworkCore;
namespace Volo.CmsKit.Tags
{
public class EfCoreEntityTagRepository: EfCoreRepository<ICmsKitDbContext, EntityTag>, IEntityTagRepository
public class EfCoreEntityTagRepository : EfCoreRepository<ICmsKitDbContext, EntityTag>, IEntityTagRepository
{
public EfCoreEntityTagRepository(IDbContextProvider<ICmsKitDbContext> dbContextProvider) : base(dbContextProvider)
public EfCoreEntityTagRepository(IDbContextProvider<ICmsKitDbContext> dbContextProvider) : base(
dbContextProvider)
{
}
public async Task DeleteManyAsync(Guid[] tagIds, CancellationToken cancellationToken = default)
public virtual async Task DeleteManyAsync(Guid[] tagIds, CancellationToken cancellationToken = default)
{
var dbSet = await GetDbSetAsync();
var dbContext = await GetDbContextAsync();
var dbSet = dbContext.Set<EntityTag>();
dbSet.RemoveRange(dbSet.Where(x => tagIds.Contains(x.TagId)));
await dbContext.SaveChangesAsync(GetCancellationToken(cancellationToken));
}
public Task<EntityTag> FindAsync(
public virtual Task<EntityTag> FindAsync(
[NotNull] Guid tagId,
[NotNull] string entityId,
[CanBeNull] Guid? tenantId,
CancellationToken cancellationToken = default)
{
return base.FindAsync(x =>
x.TagId == tagId &&
x.EntityId == entityId &&
x.TenantId == tenantId,
cancellationToken: cancellationToken);
x.TagId == tagId &&
x.EntityId == entityId &&
x.TenantId == tenantId,
cancellationToken: cancellationToken);
}
}
}

2
modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreTagRepository.cs

@ -5,10 +5,8 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.VirtualFileSystem;
using Volo.CmsKit.EntityFrameworkCore;
namespace Volo.CmsKit.Tags

Loading…
Cancel
Save