如何在C#中设置存储库中的一些实体属性?

2024-01-20

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

这是我的实体的示例:

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

我正在使用 Repository,我想将类似的内容添加到我的 IRepository 界面中:

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

那么我如何在我的存储库中实现它,然后在我的操作中使用它!?

我可以用传统方式设置这个字段,但我想要更干净的操作......


好吧,所以你必须创建一个清晰的 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();
}

并创造One通用存储库如下:

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,所以您可以轻松地像这样:

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

然后将其实施到您的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; }

}

使用这个简单存储库的优点是您可以轻松地用它做任何事情,例如:

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);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在C#中设置存储库中的一些实体属性? 的相关文章

随机推荐