1

Im doing a .NET solution and i have to architect this. So my architecture is like below.

  1. DLL(data logic layer) (Where i have all my repositories to access data)
  2. Models(In a schema wise)
  3. ViewModels(Separately as class library )
  4. Services Layer (Where manipulate data and sends to controller)

Here DLL will sent the data by accessing DB. Then Service layer will use these Repositories (etc. UserRepository) and manipulate the data as i want to send it to controller. In this case controller will return the ViewModels to views. So what i wanna know is, when mapping data to ViewModels ,Should my service layer do mappings and return ViewModels to Controller ?? OR Service Layer return as Models and in controller we do the mapping and create view models ?

What i feels is it is not good to have so many operations in the controller. So my service should return ViewModels ,So the controller have less work.

I would like to hear best practices and ideas ??

Anushka Madushan
  • 577
  • 1
  • 4
  • 12
  • IMHO the controller controls the data and what it has to be done to it, so I find not problem having many operations in it. But asking for ideas and best practices could lead to the question being tagged as `too broad`. I would go to https://codereview.stackexchange.com/ – Cleptus Sep 20 '18 at 10:46
  • Are you using AutoMapper or are you mapping models manually? – Matt Sep 20 '18 at 11:16
  • @Matt no but i was thinking of using it. – Anushka Madushan Sep 20 '18 at 11:40
  • @bradbury9 ok. Then hope it will not make controller ugly and heavy – Anushka Madushan Sep 20 '18 at 11:45
  • 1
    You can add one or more ViewModelServices. They do belong to the presentation layer so they do not go into the services layer. Add a folder/namespace to the root of your MVC project or to the Views folder. – H H Sep 20 '18 at 11:51

2 Answers2

4

I would do mapping in the controller. Because there can be some instances we need to map the same service output to different view models. Otherwise, we have to write multiple service methods for each view model type. You can simplify mapping by using AutoMapper.

Sampath
  • 1,173
  • 18
  • 29
  • And then what if service layer want to return like DTO objects. I mean say i have a user service and my user model consists of Username,Passord only. And In controller im expecting a Object like this Username,Passord, List roles. So according to your solution we have to get User from UserService and get Roles from RoleService, then create a ViewModel as i want it then assign Roles to rolesList in the Controller. – Anushka Madushan Sep 20 '18 at 11:54
  • No, In the controller you don't want to call multiple services. In your UserService there can be a method GetUserWithRoles() which will return your DTO to the controller. – Sampath Sep 20 '18 at 12:12
0

Take a look at Where should I put automapper code? question.

It suggest using Automapper in service layer.

Also, configuring mappings is a static method, called only once, so it does not affect performance much: official getting started.

And for end, here is some more explanations for setup: SO answer

Matt
  • 1,245
  • 2
  • 17
  • 32