views:

101

answers:

2

Consider an ASP.NET MVC 2 web application project that uses EF4 POCO entities and the repository pattern to store information in a SQL Server database. So far there are 3 projects, 4 if you count the database:

1.) Domain.dll, has no dependencies, exposes POCO's and repository interfaces.

2.) Storage.dll, depends on Domain, implements repository interfaces (using EF 4).

3.) Mvc.dll, depends on both #1 and #2, provides UI layer.

4.) SQL Server database + connection, dll agnostic (no dependencies).

Let's say I add another web application to host a WCF Data Service, which provides an OData feed of the database using Domain.dll and Storage.dll:

5.) Provider.dll, depends on Domain & Storage, provides OData service layer.

Where does the domain validation logic belong in this solution? If the domain POCO classes are decorated with validation attributes, does the WCF data service need anything else to protect the data? Is it ever a good/bad idea to put validation logic into stored procedures, and why?

+1  A: 

I marked this as subjective and argumentative because even the DDD community can't make up its mind about this.

How many layers do you need to change if a new validation requirement for Customer.LastName comes in? Can the UI layer inform the user the domain, wcf, web, database layers have rejected their commands? How is a LastName restriction that a LastName can't exceed 50 characters part of the business domain?

You can see that this is really a discussion and not a answerable question.


Also if your using OData as a Database feed for your services you are not using DDD. You are using Persistance-As-Model or DDD-Lite. This is actually harmful and results in questions like this that try to resolve the problems with using two incompatible patterns together.

jfar
+1  A: 

I would say put DataAttributes on the Domain and use the validation in MVC for the MVC part and use Ent Lib to do the validation in the WCF Service. Alternatively you could use the Ent Live validation in both MVC and WCF, but thats not what this is about. I dont know that this needs to come down to DDD semantics. MVC does validation on the Models when they have DataAttributes, so follow that and do the same in WCF.

CrazyDart
Thanks, I actually just discovered the entlib validation block after asking this question yesterday. The MVC project is already using Unity 2.0, so entlib looks promising.
olivehour
I will plug my own blog for this one... if you want to put entlib in your MVC solution as a provider, check out this post: http://www.bitsinmyhead.com/2010/09/asp-net-mvc-2-0-and-its-validation%E2%80%A6-using-a-custom-provider/
CrazyDart
I also found this little tidbit for using entlib validation in MVC: http://codebetter.com/blogs/david.hayden/archive/2009/02/03/an-aha-moment-on-mvc-validation-extensibility-in-defaultmodelbinder-bye-to-idataerrorinfo.aspx
olivehour