jeudi 7 mars 2019

Where and how to map T entity to multiple different entities?

This post contains two different but cohesive questions:

I'm creating an application with a multitier architecture. I decided to transfer multiple types of objects between layers, see image 1 architecture.

The main reason to use different objects is for extra security, security by design. So the Dto objects are objects from the database where the data is filtered (for example: hashed userId's)
Architecture Image 1. Architecture

In the picture I transfer Dto's from datalayer to busines, reason: so the business developers can't misuse the objects and send harmfull database data.

Question 1: Where should I map the objects?

I'm not sure yet where to convert the objects, I'm thinking of doing it in the Repository class now. I've read (see link 1) the Repository isn't ment for it, but i have no clear explanation for it when using the data transfer like I do in this architecture. Link 1: Misuse of repository pattern mapping objects.

I'm not using Automapper for this, because there aren't that many objects to map and to keep grip on the code. I've also read that Automapper comes with some complications, if you'd like to see them see link 2. Link 2: Horrors of Automapper.

Question 2: How can I still use the generic class and map objects by type?

For example:

Generic repository

    public abstract class Repository<T> : IRepository<T> where T : class
    {
       private readonly PlatformContext _platformContext;

       protected Repository(PlatformContext context) => _platformContext = context;
    }

Specific Repository

    public class RoomRepository : Repository<Room>, IRoomRepository
    {
       private readonly PlatformContext _platformContext;

       public RoomRepository(PlatformContext platformContext)
           : base(platformContext) => _platformContext = platformContext;
    }

I can override all the generic methods, but I've the feeling of redundancy with that option. Anyone familiar with a way to make a configuration of mappings and return a certain entity by the return value of the method.

Simpler example of the question: RoomRepository inherits the Repository function:

bool Create(T entity);

Where in this case T is a Room object, so is there a way to decide according to the parameter in the function?

Or maybe anyone has an easier way, that I'm thinking too difficult.

Aucun commentaire:

Enregistrer un commentaire