gpt4 book ai didi

c# - 使用泛型并在接口(interface)中实现可选字段或可选地实现接口(interface)?

转载 作者:行者123 更新时间:2023-11-30 23:21:10 24 4
gpt4 key购买 nike

我正在使用 C# 6.NET 4.6.2

我使用通用存储库和通用ASP.NET MVC Controller ,因为我有很多查找表并且不想为每个创建单独的 Controller 和存储库

这是我简化的代码:

模型:

public interface IEntity
{
int Id { get; set; }

}

public class Lookup1:IEntity
{

public int Id { get; set; }

public string Lookup1Name { get; set; }

public string CreatedBy { get; set; }

public DateTime CreatedDate { get; set; }
}
public class Lookup2:IEntity
{

public int Id { get; set; }

public string Lookup2Name { get; set; }

}

存储库:

public interface IGenericRepository<TEntity> where TEntity : class, IEntity
{
void Update(TEntity obj);
}


public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class, IEntity
{
public void Update(TEntity obj)
{
table.Attach(obj);
_db.Entry(obj).State = EntityState.Modified;
}
}

通用 Controller :

 public abstract class GenericController<TModel> : Controller
where TModel : class,IEntity
{

private IGenericRepository<TModel> _repository;

[HttpPost]
public async Task<IActionResult> Edit(TModel model)
{
try
{
if (ModelState.IsValid) {
_repository.Update(model);
await _repository.Save();
}
}
catch (Exception)
{
ModelState.AddModelError(string.Empty, "Unable to save changes.");
}
return View(model);
}

Controller :

public class Lookup1Controller : GenericController<Lookup1>
{
private IGenericRepository<Lookup1> _repository;

public Lookup1Controller (IGenericRepository<Lookup1> repository) : base(repository)
{
_repository = repository;
}
}

public class Lookup2Controller : GenericController<Lookup2>
{
private IGenericRepository<Lookup2> _repository;

public Lookup2Controller (IGenericRepository<Lookup2> repository) : base(repository)
{
_repository = repository;
}
}

以上工作并更新了从我的 MVC View .cshtml 文件传递​​的所有 Lookup1Lookup2 模型字段。

然而,只有我的一些模型具有 DateCreatedCreatedBy 属性,我想在我的通用中的 Edit 方法中更新这些属性 Controller 也是如此。

像这样的

 model.DateCreated = DateTime.Now;
model.CreatedBy = _myService.Username;

然而,要执行此操作,我必须将这两个属性添加到接口(interface) IEntity 中,但这些属性仅属于某些模型,例如 Lookup2 没有这两个属性。

我能想到的唯一解决方案是将 nullable DateCreatedCreatedBy 属性添加到我所有的模型类中,这样我就可以将这些属性添加到IEntity 接口(interface)并更新我的通用 Controller 中的那些字段。但是我不认为它是理想的,因为我没有兴趣为 Lookup2

设置这些属性

我的问题:

  1. 是否可以在一个接口(interface)中拥有条件属性或可选地继承一个单独的条件接口(interface)?

  2. 或者其他更简洁的解决方案来解决我的问题?

最佳答案

使用另一个接口(interface):

public interface ITraceableEntity : IEntity
{
string CreatedBy { get; set; }
DateTime CreatedDate { get; set; }
}

然后在你想要的具体类上实现它:

public class Lookup1 : ITraceableEntity
{
public int Id { get; set; }

public string Lookup1Name { get; set; }

public string CreatedBy { get; set; }

public DateTime CreatedDate { get; set; }
}

在通用 Controller 上,您可以检查实例是否正在实现此接口(interface),然后对其进行转换:

try
{
if (ModelState.IsValid) {
if (model is ITraceableEntity)
{
((ITraceableEntity)model).CreatedBy = _myService.Username; // <---
((ITraceableEntity)model).CreatedDate = DateTime.Now; // <---
}
_repository.Update(model);
await _repository.Save();
}
}
catch (Exception)
{
ModelState.AddModelError(string.Empty, "Unable to save changes.");
}
return View(model);

关于c# - 使用泛型并在接口(interface)中实现可选字段或可选地实现接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39349033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com