Use of Dataannotations in DDD projects

Asked

Viewed 128 times

5

We are with a doubt that analyst raised, when I use Dataannotations in Viewmodel in projects that use DDD in the case have a Domain layer and in the case will not expand, this use would eliminate the domino layer by is duplicating the field validation, for example:

I am validating if the email was filled in this case would need to do this validation again in the domain?

  • 3

    I wouldn’t know until because I don’t know much about D.O.D. and because I don’t think it’s a good methodology in most cases. I would say it depends :) This idea that the domain is so static displeases me. Things are full of contexts. Because I adopt a very different strategy I can’t help much more, but there’s a hint that things can become more complex than the DDD does, which by the way adds complexity to the application just because of the mechanism and not because of the domain, what is an irony :)

1 answer

9


I am validating if the email was filled in this case would need to do this validation again in the domain?

A.:Yes, the validation you use in viewmodel is to be used for client validation (although it will also be validated on the server with model.isvalid). You should not use the business entity as Viewmodel. Always use a viewmodel to transfer only the necessary data to be displayed in the view. To automatically fill the viewmodel use the Automapper and to make the entity validation rules use the Fluentvalidation.

  • Why use Fluentvalidation?

  • 3

    Better separates class responsibilities, increasing cohesion and making your code more readable, as you put the whole validation part separately.

  • 1

    But from what I understand, it is not recommended to use Model or Business Entity, the problem that often both are equal, should not be?

  • In the case of basic registrations, the view model will be equal to the business entity. I use the same in these cases but I’ve read people defending not even use in these cases.

  • I really wanted to find out the reasons, because it must have, like attack something like this would be super interesting someone post the reasons for not using Viewmodel as a business rule, to even help those who have this doubt like me and my friends work.

  • The reasons are even performance. Using viewmodels you traffic only the necessary data to view and in many cases already formatted. Using the entity always goes over data. I usually put the viewmodels in separate projects for cases where the entity query is time consuming I create a view in the BD and map the viewmodel directly (using nhibernate).

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.