用户输入验证和业务对象验证混淆
我正在尝试集中精力验证用户输入和验证业务对象。假设我正在使用一个 Customer 对象。它具有以下属性:CustomerId、FirstName 和 LastName。 FirstName 和 LastName 为必填项,且其长度不能超过 50 个字符。
我正在使用 ASP.NET MVC 3。我也在尝试 Fluent Validation(但不一定是这个验证框架)。
当我位于“创建客户”视图时,我向视图传递了一个 CustomerViewModel:
[Validator(typeof(CustomerViewModelValidator))]
public class CustomerViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
在 CustomerViewModelValidator 中,我设置了如上所述的验证所需的规则。这一切都在视图上得到了很好的验证。现在我有一个问题。在我的应用程序中,我有一个服务层,在这里我想应用所有应用程序逻辑。假设我想保存一个新客户,那么我将在 CustomerService 中有一个 Save customer 方法,它调用 CustomerRepository 的 Save 方法。
我可能有另一个应用程序(除了上述的 Web 应用程序)将使用我的服务层。因此,这意味着如果创建了 Customer 对象,我将必须验证该对象。出现以下问题:
- 我是否还需要验证 CustomerService 中的 Customer 对象以检查 FirstName 和 LastName?
- 创建一个新的验证器类来验证 Customer 类会更好吗?或者我应该分享它?
- 我还需要验证 CustomerId 吗?我的意思是它应该大于零,但是我如何验证 Id 为 0 的新客户?
如果有人可以分享一些对此的见解/文章,我们将不胜感激。
我也想添加一些业务规则,这会在哪里?在哪里以及如何实施业务规则?
I'm trying to wrap my head around validating user input and validating a business object. Let's say I am working with a Customer object. It is has the following properties: CustomerId, FirstName and LastName. FirstName and LastName are required, and their length cannot be more than 50 characters long.
I am using ASP.NET MVC 3. I am also experimenting with Fluent Validation (but does not have to be this validation framework).
When I am on the Create customer view, I pass the view a CustomerViewModel:
[Validator(typeof(CustomerViewModelValidator))]
public class CustomerViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
In CustomerViewModelValidator I set the required rules for the validation as described above. This all validates fine on the view. Now I have a question. In my application I have a service layer, here I want to apply all application logic. Lets say I want to Save a new customer then I will have a Save customer method in CustomerService which calls CustomerRepository's Save method.
I might have another application (other than the web app described above) that will make use of my service layer. So this is going to mean I am going to have to validate a Customer object if one is created. The following questions arise:
- Do I need to validate a Customer object in the CustomerService as well to check FirstName and LastName?
- Would it be better to create a new validator class to validate the Customer class? Or should I share it?
- Do I need to validate CustomerId as well? I mean it should be greater than zero, but how would I valid a new Customer where Id is 0?
If anyone can share some insight/articles into this it would appreciated.
I would love to add some business rules as well, where would this be? Where and how do I implement business rules?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于这个问题可能有不同的观点,我的观点是,如果您正在创建可重用的服务,那么在服务级别验证 Customer 对象会更正确。
您应该创建一个新的验证器。请记住,
CustomerViewModel
和Customer
是两个不同的类。视图模型特定于给定视图,并且可能包含模型类属性的子集或超集。服务层的验证应该仅验证业务规则。如果您使用 SQL Server,则甚至可以在数据库级别执行此验证(通过设置列的长度),并且如果其中一些业务规则被执行,则会引发异常违反了。There might be different opinions on this matter and mine is that if you are creating a reusable service then it would be more correct to validate the Customer object at the service level.
You should create a new validator. Remember that
CustomerViewModel
andCustomer
are two different classes. The view model is specific to a given view and might contain a subset or a superset of properties of the model class. The validation at the service layer should validate only business rules.If you are using SQL server this validation could even be performed at the database level (by setting the length of the columns) and an exception will be thrown if some of those business rules is violated.