如何在C#中的存储库中设置某些实体属性? [英] How to Set Some of Entity Properties in Repository in C#?

查看:89
本文介绍了如何在C#中的存储库中设置某些实体属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,其所有实体都有一些用于创建/修改/删除"的日志字段,并且我必须在我的所有CRUD操作中都使用当前用户ID",并出于安全目的设置这些字段....

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 ....

这是我的实体的示例:

 //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; }

我正在使用存储库,我想在IRepository接口中添加类似的内容:

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 ...

推荐答案

好的,因此您必须创建一个清晰的IRepository并使其尽可能简单(因为您要使用此泛型):

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();
    }
}

关于EntityBase的好处是,由于所有属性都有一个ID,因此您可以轻松地进行如下操作:

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; }
}

然后将其实现到您的模型:

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);
    }
}

这篇关于如何在C#中的存储库中设置某些实体属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆