How to take a Domain Entity method to higher layers

Asked

Viewed 338 times

1

Hello,
I have the following domain entity.

public class Cliente{
    [Key]
    public string CPF{get; set;}
    public string Nome{get; set;}

    //Outras propriedades

    public void Sacar(Conta conta, decimal valor){
        conta.Saldo -=valor;
    }
    public void Depositar(Conta conta, decimal valor){
        conta.Saldo+=valor;
    }
}

I’m working on an architecture . How am I going to map this to my layer and use on the layer ?

I thank everyone who can collaborate,
hug.

1 answer

1


In the DDD, when we need to create a complex entity it is necessary to use a factory (what the DDD calls Factory) of objects so that we can reuse it throughout the project.

Thus, you can create an abstract class containing methods that return consistent objects (in this case a Client object), with a single maintenance point.

Created from the factory, you can use it in the Application layer. But if you want to leave your code even more uncoupled, you can create an interface with the signatures of your factory methods and map your factory to the other layers using Ioc/DI.


Editing:

You can continue using the Automapper normally to perform the mapping between Client and Clientviewmodel, because the goal of the Automapper is to transform/map objects, avoiding manual work.

However, to call your domain object methods - like the Sacar() method - you can implement what the DDD calls a Service. You create in the Domain layer a Iclienteservice interface and its respective Clienteservice that implements the methods to be consumed by the other layers (I even suggest removing the methods of the Customer entity and create a service delegating this responsibility of withdrawal/ deposit to Icontaservice/ Contaservice, because, imagine if the account has no balance. You will have to deal with a number of situations like this that are not the responsibility of the Customer entity, but rather of Account).

In your Presentation layer (as I noticed Asp.net MVC) you can receive this interface in your Controller builder via Ioc/DI as I mentioned earlier.

Below some considerations that I found pertinent reinforce, because after reading your comment I was doubtful if it is already clear to you:

  • Your domain entity must not have any dependency on a external framework. Logo, remove [key] and other references to external frameworks of your domain entity.

  • There are no problems in your Presentation layer knowing your objects Domain. Would have problems the reverse (Domain layer know the presentation layer).

  • I created the Icliente interface. But my Presentation layer doesn’t know the Domain, so I’ve chosen domain entities for application using Automapper. So in Application I have Clienteviewmodel. So independent of this mapping I should call draw() Icliente?

  • As you mentioned about Automapper in your comment, I will edit the response considering its usage.

Browser other questions tagged

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