samedi 24 mars 2018

Designing model in REST API [duplicate]

This question already has an answer here:

I am working on a web project (joined to a team) where there is one model used to persisting data and exchanging data with clients (both receiving and sending) (15-20 different types). Due to this, in single model class we have properties ignored by ORM (like collection of many-to-many connected resources) and ignored by response serialization (like implementation of many-to-many with linking class). Also we have some other problems emerging from this like model validation.

So I tried to decouple this to DTOs used in receiving data from clients and data model used in ORM and sending data back. But I encourage some design problems.

  1. How should I rewrite data from DTOs to data model. Is it responsibility of data model or DTO or something third (like some kind of rewrite service which received both DTO and data model instance?

First:

class DTO 
{
    void Update(Model model) {}
}

class Model
{
}

Second:

class DTO 
{
}

class Model
{
    void Update(DTO dto) {}
}

Third:

class DTO 
{
}

class Model
{
}

class RewriteService
{
    Update(Model model, DTO dto) {}
}

  1. Should I separate domain model from data model and then domain model should base on data model?

Example:

class Data
{
}

class Model
{
    private Data _data;

    Model(Data data)
    {
        _data = data;
    }
}

  1. Should I use DTOs also to sending data to user? If so where is best place to put invocation of rewrite logic? In controller action? Or during response processing (like JsonConverter)?

I am trying to implement this as good as possible and learn how generally this should be designed.

Aucun commentaire:

Enregistrer un commentaire