这里的第一个问题,我希望我做对了。
我正在使用具有洋葱架构(data / repo / service / mvc)的Entity Framework Core 5.0(代码优先),因此我为每个表(几乎)都提供了服务。它工作正常,但是现在我需要管理(获取,插入,更新,删除)大约150个表,这些表都具有相同的结构(ID,名称,顺序)。
我在DbContext中也将它们分别添加为Entity类和DbSet,但是我不想提供150个服务,我想拥有一个通用的服务。
如何将其绑定到我的通用存储库?
public class Repository<T> : IRepository<T> where T : BaseEntity
{
private readonly ApplicationContext context;
private DbSet<T> entities;
private readonly RepositorySequence repoSequence;
private string typeName { get; set; }
public Repository(ApplicationContext context)
{
this.context = context;
entities = context.Set<T>();
this.repoSequence = new RepositorySequence(context);
this.typeName = typeof(T).Name;
}
public T Get(long plng_Id)
{
return entities.SingleOrDefault(s => s.Id == plng_Id);
}
[...]
}
在理想的世界中,想拥有这样的东西:
public async Task Insert(dynamic pdyn_Entity)
{
Type DynamicType = Type.GetType(pdyn_Entity);
Repository<DynamicType> vobj_Repo = new Repository<DynamicType>(mobj_AppContext);
long Id = await vobj_Repo.InsertAsync(pdyn_Entity);
}
但是我也可以尝试从DbSet字符串Name中获取类型,我只是设法检索了一些数据:
public IEnumerable<object> GetAll(string pstr_DbSetName)
{
return ((IEnumerable<BaseEntity>)typeof(ApplicationContext).GetProperty(pstr_DbSetName).GetValue(mobj_AppContext, null));
}
我已经尝试了以下方法(显然与2.0兼容)来获得良好的DbSet,但都不起作用(没有查询):https ://stackoverflow.com/a/48042166/10359024
我想念什么?
非常感谢你的帮助
不确定为什么需要输入文字?
您可以使用类似这样的东西。
public class Repository<T> : IRepository<T> where T : BaseEntity
{
private readonly ApplicationContext context;
private DbSet<T> entities;
public Repository(ApplicationContext context)
{
this.context = context;
entities = context.Set<T>();
}
public List<T> Get()
=> entities.ToList();
public T Get(long plng_Id)
=> entities.Find(plng_Id);
public long Save(T obj)
{
if (obj.ID > 0)
entities.Update(obj);
else
entities.Add(obj);
return obj.ID;
}
public void Delete(T obj)
=> entities.Remove(obj);
}
然后,您可以使用以下两个选项之一
public class UserRepository : Repository<User> : IUserRepository
{
private readonly ApplicationContext context;
public UserRepository(ApplicationContext context)
{
this.context = context;
}
}
public class BaseService : IBaseService
{
private readonly ApplicationContext context;
private IUserRepository user;
private IRoleRepository role;
public IUserRepository User { get => user ??= new UserRepository(context); }
public IRoleRepository Role { get => user ??= new RoleRepository(context); }
public BaseService(ApplicationContext context)
{
this.context = context;
}
}
public class BaseService : IBaseService
{
private readonly ApplicationContext context;
private IRepository<User> user;
private IRepository<Role> role;
public IRepository<User> User { get => user ??= new Repository<User>(context); }
public IRepository<Role> Role { get => role ??= new Repository<Role>(context); }
public BaseService(ApplicationContext context)
{
this.context = context;
}
}
最后,您可以像这样致电服务。如果需要,可以使用多个服务而不是BaseService。
public class HomeController : Controller
{
private readonly IBaseService service;
public HomeController(IBaseService service)
{
this.service = service;
}
public IActionResult Index()
{
var user = service.User.Get();
return View(user);
}
public IActionResult Add(User user)
{
var id = service.User.Save(user);
return View();
}
}
我建议使用第一个选项(多个存储库),因为将来可能需要在自己的存储库中自定义函数。然后根据您的控制器名称创建服务类。例如,您有HomeController,UserController等。创建HomeService,UserService并将它们与BaseService链接,以便可以在它们自己的服务类中创建自定义函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句