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:
- A web request is made to the WEB
- WEB gets DTOs posted
- DTOs get automagically validated via custom ActionFilter
- validation errors are auto-collected
- (Validation is OK) WEB calls into BL providing DTOs
- 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
- I can't reference BL in DTO because I'll get circular reference.
Compilation error
- 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
- 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. - 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.
- Don't handle external dependant validation and let external dependencies throw an exception – probably the worst solution to this issue
Best Solution
I would suggest an experiment that i have only been trialling for the last week or so.
Based on this inspiration i am creating DTOs that validate a little differently to that of the
DataAnnotations
approach. Sample DTO:This might look more work than
DataAnnotations
and well, that's coz it is, but it's not huge. I think it's more presentable in the class (i have some really ugly DTO classes now withDataAnnotations
attributes - you can't even see the properties any more). And the power of anonymous delegates in this application is almost book-worthy (so i'm discovering).Base class:
ValidationRule
and my validation functions:If my code looks messy well that's because i've only been working on this validation approach for the last few days. I need this idea to meet a few requirements:
IDataErrorInfo
interface so my MVC layer validates automaticallyThis approach i think will get me what i want, and maybe you as well.
I'd imagine if you jump on board with me on this that we'd be pretty 'by ourselves' but it might be worth it. I was reading about the new validation capabilities in MVC 2 but it still doesn't meet the above wish list without custom modification.
Hope this helps.