存储库和服务层交互问题
我有一个通用存储库接口,它具有从服务层保存、读取和查询的常用方法,如下所示:
public interface IRepository<T>
{
T GetById(int id);
void Save(T entity);
void Update(T entity);
void Delete(T entity);
IEnumerable<T> GetAll();
}
如果我有一个服务,例如使用 IRepository 的具体实现的用户服务> 以 User
作为其类型 (IRepository
),如果服务本身可能需要来自另一个 IRepository
的内容,请说 IRepository
Admin>
服务应该直接调用 IRepository
,还是应该调用关联的服务(即主要处理 IRepository
的服务) > 存储库)?
如果您直接从存储库中提取项目,如果您想在结果返回给客户端之前应用某些业务规则,但另一方面,服务可能希望处理原始结果,我个人会看到一个问题设置并将其自己的规则应用于结果,所以我对采取哪个方向有点困惑,任何建议将不胜感激。
I have a generic repository interface, that has the usual methods for saving, reading and querying from the service tier like so:
public interface IRepository<T>
{
T GetById(int id);
void Save(T entity);
void Update(T entity);
void Delete(T entity);
IEnumerable<T> GetAll();
}
If I have a service, for example a User service that uses a concrete implementation of the IRepository
with User
as its type (IRepository<User>
), if the service itself might need something from another IRepository
say IRepository<Admin>
should the service call IRepository<Admin>
directly, or should it call an associated service (i.e., the service that primarily deals with the IRepository<Admin>
repository)?
I personally can see an issue if you are pulling items directly from a repository, if say you wanted to apply certain business rules before the results are returned to a client, but on the other-hand a service might want to work on the raw result set and to apply its own rules to the results, so I am a bit confused about which direction to take, any suggestions would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果所有存储库的实现细节相同,您可能可以创建一个抽象的 BaseRepository,例如:
然后您可以创建一个专用的 AdminRepository,例如:
并从另一个存储库调用,您可以执行以下操作:
希望有所帮助。
If the implementation detail of all the repositories is same, you could probably create an abstract BaseRepository, for example:
then you can create a dedicated AdminRepository, for example:
and to call from another repository you can do the following:
hope that helps.