基于外部因素验证对象(即数据存储的唯一性) [英] Validate object based on external factors (ie. data store uniqueness)

查看:159
本文介绍了基于外部因素验证对象(即数据存储的唯一性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

描述

我的解决方案具有以下项目:


  • DAL =修改实体框架

  • DTO =数据传输对象能够验证自己

  • BL =业务层服务

  • 网络 = presentation Asp.net MVC应用程序

DAL,BL和WEB所有参考DTO这是伟大的。结果
这个过程通常执行这样的:


  1. 一个网络请求到WEB制作

  2. WEB发布得到的DTO

    • 的DTO获得通过自定义ActionFilter自动的验证

    • 验证错误是自动收集


  3. (验证是OK)WEB调用到BL提供的DTO

  4. BL使用DTO的调用到DAL(可以通过他们通过或只是使用它们)

DTO验证问题,那么......

我的DTO都能够根据自己的状态(属性的值),以验证自己。但现在我有一个问题psented $ P $当这种情况并非如此。我需要他们使用BL(因此DAL)来验证。

我的现实生活中的例子的:用户注册和WEB获取得到确认用户DTO。有问题的部分是用户名验证。它的独特性应该对数据存储进行检查。结果
我应该怎么做呢?

有额外的信息,所有的DTO实现一个接口(即用户 DTO工具 IUSER )的IoC的目的, TDD。两者都是的 DTO项目的一部分

不可能尝试


  1. 我不能引用BL的DTO,因为我会得到循环引用。结果
    编译错误

  2. 我无法创建,将基准分DTO类和有实现其验证(他们会引用BL + DTO)的额外DTO.Val项目。结果
    部分类不能跨程序集。

可能尝试


  1. 创建一个特殊的 ActionFilter ,将验证对外部条件的对象。这其中会内的 WEB项目的这样看DTO和BL将用在这里被创建。

  2. 将在的DTO BL和保持DTO接口,通过其他项目引用实际的DTO和重构所有code使用的接口,而不是具体的类。

  3. 请不要处理外部依赖的验证,并让外部依赖抛出一个异常 - 可能的最坏解决这个问题

您有什么建议?


解决方案

所得溶液

我最终使用的控制器操作的过滤器,能够验证对象,针对不能从对象本身获得的外部因素。

我创建了采取的操作参数进行检查和验证类型将验证该特定参数的名称过滤器。当然,这验证必须实现一定的接口,使这一切可重复使用的。

  [ValidateExternalFactors(用户的typeof(UserExternalValidator))]
公众的ActionResult创建(用户用户)

验证器需要实现这个简单的界面

 公共接口IExternalValidator< T>
{
    布尔的IsValid(T实例);
}

这是一个简单而有效的解决了看似复杂的问题。

Description

My solution has these projects:

  • DAL = Modified Entity Framework
  • DTO = Data Transfer objects that are able to validate themselves
  • BL = Business Layer Services
  • WEB = presentation Asp.net MVC application

DAL, BL and WEB all reference DTO which is great.
The process usually executes this way:

  1. A web request is made to the WEB
  2. WEB gets DTOs posted
    • DTOs get automagically validated via custom ActionFilter
    • validation errors are auto-collected
  3. (Validation is OK) WEB calls into BL providing DTOs
  4. BL calls into DAL by using DTOs (can either pass them through or just use them)

DTO Validation problem then...

My DTOs are able to validate themselves based on their own state (properties' values). But right now I'm presented with a problem when this is not the case. I need them to validate using BL (and consequently DAL).

My real-life example: User registers and WEB gets a User DTO that gets validated. The problematic part is username validation. Its uniqueness should be checked against data store.
How am I supposed to do this?

There's additional info that all DTOs implement an interface (ie. User DTO implements IUser) for IoC purposes and TDD. Both are part of the DTO project.

Impossible tries

  1. I can't reference BL in DTO because I'll get circular reference.
    Compilation error
  2. I can't create an additional DTO.Val project that would reference partial DTO classes and implement their validation there (they'd reference BL + DTO).
    Partial classes can't span assemblies.

Possible tries

  1. Create a special ActionFilter that would validate object against external conditions. This one would be created within WEB project thus seeing DTO and BL that would be used here.
  2. Put DTOs in BL and keep DTO interfaces as actual DTOs referenced by other projects and refactor all code to use interfaces instead of concrete classes.
  3. Don't handle external dependant validation and let external dependencies throw an exception - probably the worst solution to this issue

What would you suggest?

解决方案

Resulting solution

I ended up using controller action filter that was able to validate object against external factors that can't be obtained from the object itself.

I created the filter that takes the name of the action parameter to check and validator type that will validate that particular parameter. Of course this validator has to implement certain interface to make it all reusable.

[ValidateExternalFactors("user", typeof(UserExternalValidator))]
public ActionResult Create(User user)

validator needs to implement this simple interface

public interface IExternalValidator<T>
{
    bool IsValid(T instance);
}

It's a simple and effective solution to a seemingly complex problem.

这篇关于基于外部因素验证对象(即数据存储的唯一性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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