在我的mvc3应用程序中,我的设置如下:
1. repositories for each entity
2. service class for each entity that wraps the bare nhibernate db calls with business logic
现在,例如,一个注册用户的类,如果用户可以成功注册,我希望服务类返回的不仅仅是布尔或用户对象。
这是好习惯吗?
原因是,某人可能因为以下原因而无法正确注册:
1. duplicate email address in the system
2. duplicate username
3. etc.
所以我的方法可能如下:
public User Register(User newUser)
{
// check for a user with the same email
// check for a user with the same username
// validation checks etc.
return user;
}
我正在考虑创建一个UserRegistrationResponse对象,这样我就可以返回更丰富的返回值。
类似于:
public UserRegistrationResponse Register(User user)
{
..
return userRegistrationResponse;
}
通过这种方式,我可以返回用户可以传播到UI层的反应,并且仍然可以获取用户对象和其他信息等。
对此方法的评论?
我想唯一的另一种方法是抛出异常,但这真的是一个好主意吗?我的想法是能够重用这些服务类,比如说在Restful中我将来需要的服务层。
答案 0 :(得分:3)
这很常见。在过去的3年里,我参与过的10个WCF项目中有10个使用了这种模式。这包括三个不同公司的遗留项目,绿色领域开发和mvc / webforms项目。