Implementing a mapping service
This section explores another way to organize objects. We will end up with a similar interface to the previous example but a completely different implementation.
This example aims to simplify the implementation of the mapper façade with a universal interface. To achieve this, we are implementing the diagram shown in Figure 13.3. Here’s a reminder:
Figure 15.5: Object mapping using a single IMapper interface
Instead of naming the interface IMapper
, we use the name IMappingService
. This name is more suitable because it is not mapping anything; it is a dispatcher servicing the mapping request to the right mapper. Let’s take a look:
namespace Core.Mappers;
public interface IMappingService
{
TDestination Map<TSource, TDestination>(TSource entity);
}
That interface is self-explanatory; it maps any TSource
to any TDestination
.
On the implementation side, we are leveraging the Service Locator pattern...