You never pass a view model to the service. A service doesn't even know about the existence of a view model that you might have defined in your presentation tier. A service works with domain models Use Auto mapper to map between view model and domain model and vice versa Personally, I've never heard of service models in DDD (view models for services).
You never pass a view model to the service. A service doesn't even know about the existence of a view model that you might have defined in your presentation tier. A service works with domain models.
Use Auto mapper to map between view model and domain model and vice versa. Personally, I've never heard of service models in DDD (view models for services).
My terminology is likely inaccurate. So, in DDD, should I not be taking this approach? Or, are you just saying in DDD, they're not called "Service Models"?
– Jerad Rose Feb 25 at 16:22 the only problem in this particular case is that the Domain object does not have a plaintext password property, so there is nothing to map it against. – Yakimych Feb 25 at 16:44 @Jerad, yep, don't take that approach. – Å¡ljaker Feb 25 at 17:56 1 @Yakimych, you map what you have... Then you call another service in order to get password hash and password salt, and then you send the modified domain object to the registration service.
Auto mapper is not a must. You can do a manual mapping if that's easier in this particular case. – Å¡ljaker Feb 25 at 18:04.
Use 3rd option, for sure. As Å¡ljaker said, Service should be unaware of presentation part of application (which your ViewModel is a part of). Sure, as well, don't overcomplicate things around by including tons of transition models like RegistrationServiceModel or - even worse - IRegistrationModel (last one will lead to "interface explosion" one day).
So: Have a Domain entity (POCO entity that is persisted with Entity Framework or Nbernate or NoRM or whatever). Have a ViewModel that represents your domain model in given context. Don't hesitate to make a ViewModel per Controller Action if necessary.
The side-effect benefit of strict ViewModels (those which are 1:1 with your View) is complete absence of over-posting and under-posting problems. It depends on your concrete situation/taste though. Use DataAnnotation attributes with your ViewModels to provide basic validation (remember to validate business rules too but it should sit behind the wire - inside Services/Repositories layer).
Don't let App Service ever know about ViewModels. Create a domain entity instance and feed it to the Service instead (to validate/persist). Use AutoMapper as an option to quicky map from your domain entities to ViewModels.
Map from incoming ViewModel or FormCollection to your entity in either Controller action or custom IModelBinder. (Optionally) I'd recommend to follow the Thunderdome Principle. It's a really really convenient usage of ViewModels.
Vasilio - Same comment as above. In this particular case the entity does not contain a plaintext password, so there is nothing for AutoMapper (or whatever tool or manual code) to map against. – Yakimych Feb 25 at 16:46 1 @Yakimych, I didn't see it until I have written my comment.
About AutoMapper - you can make custom projections with AutoMapper. Also, as I understood the question - it is essentially about "best practices" of using ViewModels in common. So Account and UserService are just an example.
And in most cases AutoMapper is the thing to avoid boilerplate code. – Vasilio Ruzanni Feb 25 at 16:55 @Vasilio - I mostly agree with you, and yes you can tweak automapper to project onto a different property. But again in this particular case, there is no property for a plaintext password at all.
You actually need to run business logic in order to generate salt and hash the password, so you need to make space for the plaintext password somewhere at the service layer. And IMO in this case a DTO works best. Regarding the rest, as I mentioned, I mostly agree.
– Yakimych Feb 25 at 17:11 @Yakimych, sure that definitely is approach too, with usage of special Dto for trasfer across the wire and I have to state that it is very dependent on context and service isolation requirements. In MVC I'd prefer to feed domain entities directly to app service but I like the DTO approach too (except the fact that it is another object to maintain along with already existing domain entity and a couple of ViewModels/EditModels). – Vasilio Ruzanni Feb 25 at 17:28 1 @Yakimych, MembershipService.
RegisterUser(user, password) – Vasilio Ruzanni Feb 25 at 19:16.
In this case it makes perfect sense to use a DTO (Data Transfer Object). You can create an AccountDto class at the service layer and use it to pass the registration data down to the service. It might be similar to the ViewModel in some cases, but generally you can show much more in your View than is required to create a user.To further illustrate the point, your ViewModel will probably at least look something like this: public class RegistrationViewModel { Required public string Email { get; set; } Required public string Password { get; set; } Required Compare("Password") public string RepeatPassword { get; set; } } While your DTO will only require the Email and Password properties.
Public class AccountDto { public string Email { get; set; } public string Password { get; set; } } So as you see, the ViewModel only contains data needed for the View. The email validation and password comparison logic happens in your Web layer. You use the DTO to get get only email and password to the Service.
And then at the service layer you hash the password, populate your Entity object and persist the values to the database.
Usually, after registration, user is redirected to the profile page. This means you would have to include the UserID in AccountDto and to do the following: RegistrationViewModel -> AcountDto -> Acount -> persist values to the db -> Acount -> AcountDto -> Back to the controller. Seams like an overkill.
– Å¡ljaker Feb 25 at 18:30 @Å¡ljaker - It's not necessary, but it is perfectly reasonable in cases when business logic should be run to populate part of the domain object from the ViewModel. Regarding the other statement, I don't see a reason to use the Dto to get the Account back. Account account = accountService.
RegisterAccount(accountDto); - and you get your domain object back. It contains all you need. – Yakimych Feb 26 at 12:07 @Yakimych So you need to transform the Registration view mode to AccountDTO?
And in the service layer, you must transform account DTO into Account domain model? Is that correct? – Adi Sembiring Aug 25 at 13:20 Yes, that's correct.
– Yakimych Aug 26 at 10:39.
Use 3rd option, for sure. As Å¡ljaker said, Service should be unaware of presentation part of application (which your ViewModel is a part of). Sure, as well, don't overcomplicate things around by including tons of transition models like RegistrationServiceModel or - even worse - IRegistrationModel (last one will lead to "interface explosion" one day).
Have a Domain entity (POCO entity that is persisted with Entity Framework or NHibernate or NoRM or whatever). Have a ViewModel that represents your domain model in given context. Don't hesitate to make a ViewModel per Controller Action if necessary.
The side-effect benefit of strict ViewModels (those which are 1:1 with your View) is complete absence of over-posting and under-posting problems. It depends on your concrete situation/taste though. Use DataAnnotation attributes with your ViewModels to provide basic validation (remember to validate business rules too but it should sit behind the wire - inside Services/Repositories layer).
Don't let App Service ever know about ViewModels. Create a domain entity instance and feed it to the Service instead (to validate/persist). Use AutoMapper as an option to quicky map from your domain entities to ViewModels.
Map from incoming ViewModel or FormCollection to your entity in either Controller action or custom IModelBinder. (Optionally) I'd recommend to follow the Thunderdome Principle. It's a really really convenient usage of ViewModels.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.