10 repositories? Wow...
Luis
You say :" As discussed in this post, I don't think its a good practice to
let the controller talk directly with the repository. The controllers are
specific to the MVC pattern and the MVC library, which means that if we put
validation rules within the controllers, it will be pretty hard to migrate
our project to be used on winforms for example."
This absolutely correct, but I don't agree that having the controller talk
directly to the repositories means that you have to put your validation
rules on the controller. Here's a quick example of some code that could go
on a controller's action method:
var user = GetUserThatIsBeingUpdatedFromSomewhere();
UpdateUserWithFormInfo(user);
var brokenRules = user.GetBrokenRules();
if( brokenRules.Count() > 0 ){
//fill error info
//probably interact with modelstate, etc, etc
}
var repository = Repositories.Get<User>();
var success = repository.Save(user);
//check for success and do other stuff
Ok, this is just a snippet I've written here and you could really improve it
by adding exception handling, etc. As you can see, the controller is using
the domain objects directly without any problems or validation concerns.
Luis
And why is that? I’m really interested in hearing more on why this is a bad idea because I’ve tries all the approaches that have been commented here and this is the one that works best. We can go into a discussion on the advantages/disadvantages of using this option, but my main point with my previous mail is show that it is possible to have validation without a service and that is not also in a controller.
Luis
From: sharp-arc...@googlegroups.com
[mailto:sharp-arc...@googlegroups.com] On Behalf Of Simone Busoli
Sent: sábado, 21 de Fevereiro de 2009 14:44
To: sharp-arc...@googlegroups.com
Subject: Re: Introducing a service layer (+ how DTOs/Validation fits in)
that's because you put the validation on the domain entity, which might or not be a good thing.
I'm not an expert either but had to play with several options in my last
project.
I think the best way to ask this sort of question is either the alt.net or
ddd mailing lists.
Agreed. I've had some interesting discussions there on this subject a few
months ago...
But here's my take on it. In the beginning having the validation on the
entities sounded right, but then you might have rules which need a
repository to be validated (two users with same username), so how do you
deal with it? Do you inject the repository in the entity? Do you inject it
on the Validate() method? I'm not sure.
That is correct, but things start to look better when you don't think about
repositories and think about validations as services which are injected into
your aggregates. In practice, the classes that implement these services
might end up using repositories internally, but the entity interacts with an
existing validation service, not with the repository. I generally do this
because it makes validation more expressive. Again, in the repetition
scenario, you probably don't need to use a repository since you only need to
perform a check against your db (if you're using them).
On the other hand, as I've seen doing, and I think the sample northwind
project does as well and I'm sure CodeCampServer does, the validation is at
controller boundary, you validate the dtos, which I don't like very much,
since I think that validation should be a domain concern.
It's not really correct to validate DTOs (beyond those typical simple tests
where you test those basic rules) because your domain logic should be on
your domain layer, not scattered through controllers. On the other hand, I
guess this is OK if you're developing a simple app and your domain is only
going to be used by that app...
Luis
That is correct, but things start to look better when you don't think about
repositories and think about validations as services which are injected into
your aggregates. In practice, the classes that implement these services
might end up using repositories internally, but the entity interacts with an
existing validation service, not with the repository. I generally do this
because it makes validation more expressive. Again, in the repetition
scenario, you probably don't need to use a repository since you only need to
perform a check against your db (if you're using them).
My main problem with this is "forcing" a user to know the sequence of calls
it should perform when using my DDD library. By putting it on the entity,
he'll know that he can check for validity by calling a specific method.
Injecting it there is generally hidden form theclient if he's using the
factories and repositories for creating and saving aggregates.
they to be wrapped into a domain service which ensures everything is valid
when persisting data? I heard Greg Young on InfoQ speaking about having
always valid entities. I'm pretty sure he knows what he's saying, but I have
a hard time figuring out how you would accomplish that when the validity of
your entity depends on something external to your entity.
I'd say that you've got two levels of validation: "individual validation",
where all that matters is that the aggregate's data is valid and "collective
validation", where you also need to ensure that you don't have duplicate
aggregates, etc...
This might start to make sense once you start using commands to make changes
to your entities, as Greg advertises, but I don't think I'm going thus far.
Btw, one of the things I see commands doing is improving scalability...I've
run some tests with it but I need more time and more reading material for
understanding everything he's saying :)
Luis