存储库,服务或域对象-逻辑属于哪里? [英] Repository, Service or Domain object - where does logic belong?

查看:75
本文介绍了存储库,服务或域对象-逻辑属于哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以这个简单的,人为的例子为例:

Take this simple, contrived example:

UserRepository.GetAllUsers();UserRepository.GetUserById();

UserRepository.GetAllUsers(); UserRepository.GetUserById();

不可避免地,我会遇到更复杂的查询",例如:

Inevitably, I will have more complex "queries", such as:

//returns users where active=true, deleted=false, and confirmed = true
GetActiveUsers();

我无法确定存储库的责任在哪里结束.GetActiveUsers()代表一个简单的查询".它属于存储库吗?

I'm having trouble determining where the responsibility of the repository ends. GetActiveUsers() represents a simple "query". Does it belong in the repository?

涉及一些逻辑的事情如何处理,例如:

How about something that involves a bit of logic, such as:

//activate the user, set the activationCode to "used", etc.
ActivateUser(string activationCode);

推荐答案

存储库负责对象的集合的特定于应用程序的处理.这自然涵盖了查询以及集合修改(插入/删除).

Repositories are responsible for the application-specific handling of sets of objects. This naturally covers queries as well as set modifications (insert/delete).

ActivateUser 对单个对象进行操作.需要检索该对象,然后对其进行修改.存储库负责从集合中检索对象;另一个类将负责调用查询并使用该对象.

ActivateUser operates on a single object. That object needs to be retrieved, then modified. The repository is responsible for retrieving the object from the set; another class would be responsible for invoking the query and using the object.

这篇关于存储库,服务或域对象-逻辑属于哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆