我应该如何扩展通用存储库
How should I extend a generic repository
试图了解我是否可以扩展通用存储库接口。
抱歉,我是新手,google 搜索了这个,但找不到任何这样的实现,这让我觉得我在试图违背通用存储库逻辑。
我正在使用 EF Core ORM。
我有 IRepository<T>
:
IEnumerable<T> GetAll();
T Get(long id);
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
然后我实现一个通用存储库:
public class Repository<T> : IRepository<T> where T : class
但是我只需要为一个实体名称 Conference
实现一种特殊方法,例如:
IList<T> GetIncludedItems(Expression<Func<T, bool>> predicate, params string[] navigationProperties);
我如何扩展我的通用存储库来处理那个异常方法,而无需其他实体实现它。此外,我的 service class
将通过构造函数注入实现这些 Repositories
。
public ConferenceService(IRepository<Conference> _conferenceRepository )
我还需要通过 ConfigurServices
.
维护依赖绑定
services.AddScoped<IConferenceService, ConferenceService>();
对我哪里出错或如何完成这个有什么建议吗?
这是我的问题的解决方案,如果我想为特定实体添加自定义方法,我需要继承基础存储库。
click here to see how to solve the above problem
试图了解我是否可以扩展通用存储库接口。
抱歉,我是新手,google 搜索了这个,但找不到任何这样的实现,这让我觉得我在试图违背通用存储库逻辑。
我正在使用 EF Core ORM。
我有 IRepository<T>
:
IEnumerable<T> GetAll();
T Get(long id);
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
然后我实现一个通用存储库:
public class Repository<T> : IRepository<T> where T : class
但是我只需要为一个实体名称 Conference
实现一种特殊方法,例如:
IList<T> GetIncludedItems(Expression<Func<T, bool>> predicate, params string[] navigationProperties);
我如何扩展我的通用存储库来处理那个异常方法,而无需其他实体实现它。此外,我的 service class
将通过构造函数注入实现这些 Repositories
。
public ConferenceService(IRepository<Conference> _conferenceRepository )
我还需要通过 ConfigurServices
.
services.AddScoped<IConferenceService, ConferenceService>();
对我哪里出错或如何完成这个有什么建议吗?
这是我的问题的解决方案,如果我想为特定实体添加自定义方法,我需要继承基础存储库。 click here to see how to solve the above problem