0
votes

I have an MVC layer with controllers that use the Service layer to get and store data. In my service layer I have methods like this:

public interface ISetServices
    {
        List<Set> GetBreadcrumbs(int? parentSetId);

        List<Set> GetAllSets();
        List<Set> GetAllTopLevelArchivedSets();
        List<Set> GetTopLevelSets();
        Set GetSet(int? setId);
        Set CreateSet(string name, string details, int? parentSetId);
        void DeleteSet(int? setId);
        void ArchiveSet(int? setId);
        void UnArchiveSet(int? setId);
  / etc

My MVC layer uses viewModels to display data. This has been fine up until implementing the Edit services - What I'm trying to do now is create an UpdateSet method that takes a viewModel from the controller and updates the database accordingly.

Since the MVC layer is dependent on the Service layer, the service layer has no knowledge of viewModels. Is this where dependency injection comes in? Or is there a better way to approach this that I'm overlooking?

1
Is Set a view model or a DTO/Domain Model?Martin
You need to map the viewModel properties to the domain model properties. Your service layer will work with domain models, your UI with view models. You can do this mapping manually or use a plugin like AutoMapperForty-Two
Set is a domain model MartinRobVious
What I'm asking is - my service definition will be something like "Set UpdateSet(setViewModel model) - but this project doesn't know about setViewModel... right?RobVious
@RobVious Remember M-V-VM. Your View uses the ViewModel, but your service must use the Model. There are libraries to map Model and ViewModel, such as AutoMapper.Andre Calil

1 Answers

1
votes

You will need to convert your ViewModel into Set and then pass that to the Service Layer.

public class SetController : Controller
{
    public Action Edit(SetViewModel viewModel)
    {
        var set = _setService.GetSet(viewModel.SetId);
        set.Prop1 = viewModel.Prop1;
        _setService.Edit(set);
    }
}