How to Set Some of Entity Properties in Repository

2019-02-19 00:07发布

I have a DB that all of its entities have some log fields for Create/Modify/Delete and I have to take Current User Id in all of my CRUD Actions and set these fields for security purpose ....

this is an example of my Entities :

 //log properties
    public byte RecordStatus { get; set; }
    public string RecordStatusDescription { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedDateTime { get; set; }
    public string CreatorIPAddress { get; set; }
    public string ModifiedBy { get; set; }
    public DateTime ModifiedDateTime { get; set; }
    public string ModifierIPAddress { get; set; }
    public string RemovedBy { get; set; }
    public string RemovedDateTime { get; set; }
    public string RemoverIPAddress { get; set; }
    public bool IsRemoved { get; set; }

I'm using Repository and I want to add something like this to my IRepository interface :

public interface IBaseRepository<TEntity> where TEntity : class 
{
    void Createdby(string UserId , string userIP);
    void ModifiedBy(string UserId ,  string userIP);
    void RemovedBy(string UserID , string userIP);
}

so how can I implement this in my Repository and then use it in my actions !?

I can set this fields in traditional way but I want to have more cleaner Actions ...

1条回答
劫难
2楼-- · 2019-02-19 00:37

Alright so you have to make a clear IRepository and make it as simple as possible like this(since you want this Generic):

IRepository:

public interface IRepository<T>
{
    void Add(T entity);
    void Delete(T entity);
    void Delete(int id);
    T GetById(int id);
    IEnumerable<T> GetAll();
    void Update(T entity);
    void save();
}

And Create One Generic Repository like below:

public class Repository<T> : IRepository<T>
    where T : EntityBase
{

    internal MyDbContext context;
    internal DbSet<T> dbSet;
    public Repository()
    {

        context = new MyDbContext();
        this.dbSet = context.Set<T>();

    }

    public void Add(T entity)
    {
        dbSet.Add(entity);
    }

    public void Delete(T entity)
    {
        dbSet.Remove(entity);
    }

    public void Delete(int id)
    {
        dbSet.Remove(dbSet.Find(id));
    }

    public T GetById(int id)
    {
        return dbSet.Find(id);
    }

    public IEnumerable<T> GetAll()
    {
       return dbSet.AsEnumerable();
    }

    public void Update(T entity)
    {
        dbSet.Attach(entity);
        context.Entry(entity).State = EntityState.Modified;
    }

    public void save()
    {
        context.SaveChanges();
    }
}

Good thing about EntityBase is since all of your properties have an id, you can easily go like this:

public class EntityBase
{
    public int id { get; set; }
}

And then implement this to your Models :

public class Example : EntityBase
{

public byte RecordStatus { get; set; }
public string RecordStatusDescription { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDateTime { get; set; }
public string CreatorIPAddress { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedDateTime { get; set; }
public string ModifierIPAddress { get; set; }
public string RemovedBy { get; set; }
public string RemovedDateTime { get; set; }
public string RemoverIPAddress { get; set; }
public bool IsRemoved { get; set; }

}

Advantage of using this simple Repository is you can easily do anything with it e.g. :

public class HomeController : Controller
{

    Repository<Example> _repository = new Repository<Example>();


    public ActionResult Index()
    {
        vm.Example = _repository.GetAll()
            .Where(x => x.RecordStatusDescription == "1").ToList(); 
        return View("index",vm);
    }
}
查看更多
登录 后发表回答