如何在C#中设置存储库中的某些实体属性?
我有一个数据库,它的所有实体都有一些用于创建/修改/删除的日志字段,我必须在所有CRUD操作中使用当前用户Id,并为安全目的设置这些字段 这是我的实体的一个示例:如何在C#中设置存储库中的某些实体属性?,c#,asp.net-mvc,entity-framework,repository,C#,Asp.net Mvc,Entity Framework,Repository,我有一个数据库,它的所有实体都有一些用于创建/修改/删除的日志字段,我必须在所有CRUD操作中使用当前用户Id,并为安全目的设置这些字段 这是我的实体的一个示例: //log properties public byte RecordStatus { get; set; } public string RecordStatusDescription { get; set; } public string CreatedBy { get; set; } publi
//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界面中添加如下内容:
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);
}
公共接口IBaseRepository,其中tenty:class
{
void Createdby(字符串UserId,字符串userIP);
void ModifiedBy(字符串UserId,字符串userIP);
void RemovedBy(字符串UserID,字符串userIP);
}
那么,我如何在我的存储库中实现它,然后在我的操作中使用它呢
我可以用传统的方式设置这个字段,但我希望有更干净的操作 好的,所以你必须做出一个明确的假设,并尽可能简单地像这样(因为你想要这个通用的): i假设:
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();
}
然后将其应用到您的机型上:
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);
}
}
公共类HomeController:控制器
{
存储库_Repository=新存储库();
公共行动结果索引()
{
vm.Example=\u repository.GetAll()
.其中(x=>x.RecordStatusDescription==“1”).ToList();
返回视图(“索引”,vm);
}
}
我应该在我的[Entity]存储库中定义这些吗!?我只是不想在超过20个[实体]存储库中复制这些方法!!我知道存储库是用于单独直接与DB交互的,但是!?您是否建议我为这些操作定义单独的类,并在其中定义这些方法!?或者只是使用传统的方法来设置操作中的属性!?你会选择什么方法来解决这个问题!?查看答案,如果您有任何问题,请告诉我:)@RezaPouya乐于帮助=)@RezaPouya不客气!这是最佳实践(顺便说一句:)
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);
}
}