存储库模式和更改日志实现

问题描述 投票:1回答:1

我具有使用.net core 2构建的API,并且我正在尝试实现更改日志功能。

我已经完成了基本的部分,但是我不确定这是否是执行此操作的最佳方法。

这是我的EntityBaseRepository

public class EntityBaseRepository<T> : IEntityBaseRepository<T> where T : class, IFullAuditedEntity, new()
{
    private readonly ApplicationContext context;

    public EntityBaseRepository(ApplicationContext context)
    {
        this.context = context;
    }

    public virtual IEnumerable<T> items => context.Set<T>().AsEnumerable().OrderByDescending(m => m.Id);

    public virtual T this[int id] => context.Set<T>().FirstOrDefault(m => m.Id == id);

    public virtual T GetSingle(int id) => context.Set<T>().FirstOrDefault(x => x.Id == id);

    public virtual T Add(T entity) => Operations(entity: entity, state: EntityState.Added);

    public virtual T Update(T entity) => Operations(entity: entity, state: EntityState.Modified);

    public virtual T Delete(T entity) => Operations(entity: entity, state: EntityState.Deleted);

    public virtual T Operations(T entity, EntityState state)
    {
        EntityEntry dbEntityEntry = context.Entry<T>(entity);

        if (state == EntityState.Added)
        {
            entity.CreationDateTime = DateTime.UtcNow;
            entity.CreationUserId = 1;

            context.Set<T>().Add(entity);
            dbEntityEntry.State = EntityState.Added;
        }
        else if (state == EntityState.Modified)
        {
            entity.LastModificationDateTime = DateTime.UtcNow;
            entity.LastModificationUserId = 1;

            var local = context.Set<T>().Local.FirstOrDefault(entry => entry.Id.Equals(entity.Id));
            if (local != null)
            {
                context.Entry(local).State = EntityState.Detached;
            }

            dbEntityEntry.State = EntityState.Modified;
        }
        else if (state == EntityState.Deleted)
        {
            entity.DeletionFlag = true;
            entity.DeletionUserId = 1;
            entity.DeletionDateTime = DateTime.UtcNow;

            dbEntityEntry.State = EntityState.Modified;
        }

        return entity;
    }

这里是我的控制人之一。

[Produces("application/json")]
[Route("api/Item")]
public class ItemController : Controller
{
    private readonly IItemRepository repository;
    private readonly IChangeLogRepository changeLogRepository;
    private readonly IMapper mapper;

    public ItemController(IItemRepository repository, IChangeLogRepository _changeLogRepository, IMapper mapper)
    {
        this.repository = repository;
        this.changeLogRepository = _changeLogRepository;
        this.mapper = mapper;
    }

    [HttpPost]
    public IActionResult Post([FromBody]ItemDto transactionItemDto)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var item = repository.Add(mapper.Map<ItemDto, Item>(source: transactionItemDto));
        repository.Commit();

        ChangeLog log = new ChangeLog()
        {
            Log = "New Item Added"
        };

        changeLogRepository.Add(log);
        changeLogRepository.Commit();

        return new OkObjectResult(mapper.Map<Item, ItemDto>(source: item));
    }
}

如果您在控制器中看到,我已经添加了一项,并提交了它,然后为该插入准备了日志,添加并提交了。

现在,我有几个问题,例如

  1. 我必须两次提交事务,有什么方法可以优化它?我不知道是否可以在EntityBaseRepository上处理它。
  2. 我还想检查每个属性,是否更改。 如果更改它,我想记录到它。处理它的最佳方法是什么?

如果有人可以帮助我,那将非常好。万分感激。谢谢。

c# asp.net-core repository-pattern
1个回答
0
投票

您可以将操作筛选器用于变更日志,请参阅

Action Filter Attributes in .NET core

© www.soinside.com 2019 - 2024. All rights reserved.