Thiago Rosa
Thiago Rosa

Reputation: 341

Is it correct to map a domain entity from within the controller (presentation layer)?

I have a solution in Visual Studio 2015 where I separate the DDD layers as projects. When I need to send data from the presentation layer (MVC 5) to the application layer (class library) I usually use:

Application Service

public interface IFooAppService
{
    void AddNew(string name, DateTime birthday);
}

Controller

[HttpPost]
public JsonResult AddNew(FooViewModel viewModel)
{
    FooAppService.AddNew(viewModel.Name, viewModel.birthday);
}

When I have a domain entity class with many properties and child entities, the application service method signature becomes too long. Considering DDD and separation of concerns, is it correct to map the FooViewModel class to the Foo domain entity right from the MVC Controller in this case? Such implementation would be:

Application Service

public interface IFooAppService
{
    void AddNew(Foo foo);
}

Controller

[HttpPost]
public JsonResult AddNew(FooViewModel viewModel)
{
    Foo foo = FooMapper.Map(viewModel);
    FooAppService.AddNew(Foo);
}

Upvotes: 2

Views: 1082

Answers (2)

guillaume31
guillaume31

Reputation: 14064

is it correct to map the FooViewModel class to the Foo domain entity right from the MVC Controller in this case?

If you have an Application Service, I would say no. You bothered creating an additional Application layer so that the presentation layer can concentrate on doing UI stuff and not deal with the Domain directly. It seems weird to want to short circuit that just because the "method signature is too long".

Besides, the concept of Command from CQ(R)S can help you solve that parameter problem. Controller can call the application service passing it just a Command.

Upvotes: 5

theDmi
theDmi

Reputation: 18034

If what you have is a simple mapping from DTOs to entities, you're probably building an anemic domain model.

You should either try to build a full-blown domain model using DDD or resort to a CRUD-style application. Both of them are useful depending on the nature of the application. DDD usually only makes sense for complex problem domains.

Upvotes: 0

Related Questions