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

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

问题描述

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

My solution has these projects:

  • DAL = 修改后的实体框架
  • DTO = 能够自我验证的数据传输对象
  • BL = 业务层服务
  • WEB = 演示 Asp.net MVC 应用程序
  • 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 和 WEB 都参考了 DTO,这很棒.
流程通常是这样执行的:

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

  1. 向 WEB 发出 Web 请求
  2. WEB 发布 DTO
    • 通过自定义 ActionFilter 自动验证 DTO
    • 自动收集验证错误

DTO 验证问题然后...

我的 DTO 能够根据自己的状态(属性值)验证自己.但是现在我遇到了一个问题,而事实并非如此.我需要他们使用 BL(以及 DAL)进行验证.

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).

我的真实例子:用户注册,WEB 获得一个经过验证的用户 DTO.有问题的部分是 username 验证.应根据数据存储检查其唯一性.
我该怎么做?

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?

有额外的信息表明所有 DTO 都实现了一个接口(即 User DTO 实现了 IUser)用于 IoC 目的和 TDD.两者都是 DTO 项目的一部分.

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.

  1. 我无法在 DTO 中引用 BL,因为我会得到循环引用.
    编译错误
  2. 我无法创建一个额外的 DTO.Val 项目来引用部分 DTO 类并在那里实现它们的验证(他们会引用 BL + DTO).
    部分类不能跨越程序集.

可能的尝试

  1. 创建一个特殊的 ActionFilter 来根据外部条件验证对象.这将在 WEB 项目 中创建,从而看到将在此处使用的 DTO 和 BL.
  2. 将 DTO 放在 BL 中,并将 DTO 接口保留为其他项目引用的实际 DTO,并重构所有代码以使用接口而不是具体类.
  3. 不要处理外部依赖验证并让外部依赖抛出异常 - 可能是此问题的最糟糕的解决方案
  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

你有什么建议?

推荐答案

结果解决方案

我最终使用了控制器动作过滤器,该过滤器能够根据无法从对象本身获得的外部因素来验证对象.

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 需要实现这个简单的接口

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天全站免登陆