FreeSql with warehousing soft-deleted

The individual pieces of content from the blog click to jump synchronize updates! Please indicate the source!

Some time ago using FreeSql as ORM, write a simple CMS, which summarize here the use of experience.

Warehousing with global filters

1. Unified deletion flag

Such as: database fields bool IsDeleted, on behalf of the delete flag.

Storage may be implemented with the required, uniform deletion flag, no need to add where (r => r.deleted == false), data fetch, the data is automatically filtered.

Interface ISoftDeleteAduitEntity.cs

    public interface ISoftDeleteAduitEntity
    {
        bool IsDeleted { get; set; }
        long? DeleteUserId { get; set; }
        DateTime? DeleteTime { get; set; }
    }

ConfigureServices by configuring services and increase global filters, the second parameter, you can specify the assembly warehouse where (a dll, a project is an assembly (generally a solution to a number of projects under the program, if written in multiple places warehousing we can set the program to scan written in the second parameter.))

services.AddFreeRepository(filter =>
{
    filter.Apply<ISoftDeleteAduitEntity>("SoftDelete", a => a.IsDeleted == false);
}, GetType().Assembly, typeof(AuditBaseRepository<>).Assembly);

GetType (). Assembly project where the current assembly.
typeof (AuditBaseRepository <>). Assembly , as AuditBaseRepository Where the assemblies, (LinCms.Zero.dll)

2. Unified deletion time, deletion man

Above, the deletion flag on the configuration, we inject it provides good storage, it will filter out isdeleted property value to true.

  public class UserService : IUserSevice
    {
        private readonly BaseRepository<LinUser> _userRepository;
        public UserService(BaseRepository<LinUser> userRepository)
        {
            _userRepository = userRepository;
        }

        public List<LinUser> GetUserList()
        {
            List<LinUser> users = _userRepository.Select.ToList();
            return users;
        }
}

So, when we delete a user, how to increase the time of deletion, deleting people do.

public void Delete(int id)
{
    _userRepository.Delete(r => r.Id == id);
}

This can be deleted, but people delete, delete, add time and did not go.

Here we inject AuditBaseRepository Rewrite parent Delete method.

private readonly AuditBaseRepository<LinUser> _userRepository;
public UserService(AuditBaseRepository<LinUser> userRepository)
{
    _userRepository = userRepository;
}

public void Delete(int id)
{
    _userRepository.Delete(r => r.Id == id);
}

Since the parent is not the type of Virtual herein override int Delete (Expression <Func <T, bool >> predicate) method by the new keyword

public new int Delete(Expression<Func<T, bool>> predicate)
{
    if (typeof(ISoftDeleteAduitEntity).IsAssignableFrom(typeof(T)))
    {
        List<T> items = Orm.Select<T>().Where(predicate).ToList();
        return Orm.Update<T>(items)
            .Set(a => (a as ISoftDeleteAduitEntity).IsDeleted, true)
            .Set(a => (a as ISoftDeleteAduitEntity).DeleteUserId, _currentUser.Id)
            .Set(a => (a as ISoftDeleteAduitEntity).DeleteTime, DateTime.Now)
            .ExecuteAffrows();
    }

    return base.Delete(predicate);
}

All rewrite the delete operation, please refer https://github.com/luoyunchong/lin-cms-dotnetcore/blob/master/src/LinCms.Zero/Repositories/AuditBaseRepository.cs
this course can achieve, but it seems to contain a lot as, IsAssignableFrom (T determine whether inherited ISoftDeleteAduitEntity).

Guess you like

Origin www.cnblogs.com/igeekfan/p/11689624.html