您将这个 CRUD 类命名为什么?
试图避免这里的 SomethingManager 陷阱......
假设我要编写一个用户编辑器,允许管理员在系统中创建用户。 非常基本的功能 - 查看现有用户列表、创建新用户、更新现有用户、删除用户。
我们还假设我决定编写一个“业务”类来处理这些基本的 CRUD 操作。 界面可能如下所示:
public interface ISomeUsefulName
{
IList<User> FetchUsers();
User FetchUser(int userId);
bool SaveUser(User user);
bool DeleteUser(int userId);
}
例如,在 SaveUser() 方法内,我将验证数据(使用不同的类),然后将数据实际保存到数据库(再次使用另一个类)。
我的问题是,我应该给这个类起什么名字? 这个类做得太多了,因此我应该将其分成多个类吗?
Trying to avoid the SomethingManager trap here...
Let's say I'm going to write a User editor which will allow administrators to create users in the system. Pretty basic functionality - view a list of existing users, create a new user, update an existing user, delete a user.
Let us also say that I decide to write a "business" class to handle these basic CRUD operations. This is probably what the interface would look like:
public interface ISomeUsefulName
{
IList<User> FetchUsers();
User FetchUser(int userId);
bool SaveUser(User user);
bool DeleteUser(int userId);
}
Inside the SaveUser() method, for example, I would validate the data (using a different class) and then actually save the data to the database (again using another class).
My question is, what should I name this class? Is this class doing too much and therefore I should split it into multiple classes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果不遵守 SRP,命名会很困难:)
但成员的命名经常被滥用。
在你的情况下,我会做这样的事情:
思考没有声音
- 为用户完成持久化,相关名称可以是 IUserRepository
- 方法不比CRUD多
- 因为 IUserRepository 是供用户使用的,所以没有必要有 UserSave、UserUpdate,因为它破坏了通用使用方式
魔法就在这里...只需这样做:
很难吗?
定制一件该怎么办?
在使用 IoC 容器的代码中,你可以轻松祝
你好运:)
Naming is difficult if is not respected SRP :)
But members naming is often misused.
In your case I'll do something like this:
Thinks without voice
- persistence is done for user and a relevant name can be IUserRepository
- methods are not more than for CRUD
- because of the fact that the IUserRepository is for user, is not necessary to have UserSave, UserUpdate because it brakes the generic usage manner
The Magic Is Here ... just do this:
Is it difficult ?
What to do with a custom one?
In the code using an IoC container you can do easily
good luck :)
我赞同 ChrisW 的呼吁,将其命名为“User”。
每当您发现自己将相同的字符串放在几乎每个方法的名称中时,都应该将其从方法名称中删除并放入类名称中。
I'm seconding ChrisW's call to just name it "User".
Any time you find yourself putting the same string in the name of nearly every method, it should be removed from the method names and put in the class name.
IUserRepository —— 如 Repository 模式中所示。
IUserRepository -- as in the Repository pattern.
IUserRepository 或 IUserServices。
IUserRepository or IUserServices.
我的偏好是 IUserStorage 或 IUserStore
My preference would be IUserStorage or IUserStore
事实上,你在命名时遇到困难,这应该是一个巨大的危险信号,表明它是错误的。
单一职责原则(和接口隔离原则)在这里适用。 将其分解为您需要的各种操作。
然后命名它们就变得更加简单,因为现在只有一个名称真正适用。 相信我,如果您是设计师,您的开发人员会喜欢您让事情变得易于理解和使用。
The fact that you are having trouble naming this should be a giant red flag that it is wrong.
Single Responsibility Principle (and Interface Segregation Principle) applies here. Break it up into the various operations you need.
and then it gets much simpler to name them because now only one name really applies. Trust me, if you are a designer your devs will love you for making things simple to understand and use.
它可以成为一个通用接口。
或者受到 IUserStore 的启发。
It could become a generic interface.
Or inspired by IUserStore.
为什么不只是 IUserCRUD?
相对于“管理”,CRUD 有 10 种含义。
Why not just IUserCRUD?
CRUD has, as oposed to 'manage' no 10 meanings.
将其称为“Users”(或“AuthorizedUsers”或“CollectionOfUsers”)怎么样?
How about calling it "Users" (or "AuthorizedUsers" or "CollectionOfUsers")?
我会选择
UserActions
。 这描述了您想要执行的功能集; 它避免了将其称为集合的陷阱(因为它实际上不收集任何东西,只是检索集合)。但我也会重新考虑以这种形式开设这门课。 看起来您正在尝试安装一个持久性管理器; 您是否希望以这种方式保留任何其他类型的对象? 您能否提取任何可以派生到基类的通用功能? 也许是“
PersistenceManager
”类或类似的类? 然后,如果绝对有必要(我不确定是否有必要),您可以派生一个“UserPersistenceManager
”,它可以单独对 User 对象进行操作。 (我认为这可能没有必要,因为您可能能够仅从 PersistenceManager 执行您需要的所有操作;不过,只有您的特定实现可以告诉您这一点。)I'd go with
UserActions
. This describes the set of functionality you want to do; it avoids the trap of calling it a collection (since it doesn't actually collect anything, simply retrieves a collection).But I'd also rethink having this class in this form in the first place. It looks like what you're trying to put in place is a persistence manager; are there any other types of objects that you're going to want to persist in this manner? Can you extract any common functionality that can then be derived to a base class? Perhaps a "
PersistenceManager
" class or somesuch? Then, if it's absolutely necessary (and I'm not certain it would be), you could derive a "UserPersistenceManager
" that would operate on User objects alone. (I believe it might not be necessary because you may be able to perform everything you need just from thePersistenceManager
; only your specific implementation can tell you that, though.)