1

I have the following Solution structure.

Solution 
 ProductServiceApi
 Business
 DataAccess
 DTO
 Contract

I am using EntityFramework in DataAccess layer. It has the .edmx file and entity classes(e.g. Product). I have my domain classes in DTO layer(ProductDto). I am creating the map in WebApiConfig.cs file. To map the DTO and DataAccess layer entities I have to add a reference to both DTO layer and DataAccess layer in ProductServiceApi.

e.g.  Mapper.CreateMap<ProductDto, Product>();

But I think its a bad idea to add a reference to dataAccess layer in my ProductServiceApi.

What should I do to avoid this? Should I add a reference automapper in DTO layer and map the dto and entity there? What is the ideal solution. I have gone through some online tutorials but cannot find a proper solution.

subs
  • 2,189
  • 12
  • 35
  • 59

1 Answers1

0

I had prepared quite a lengthy answer to this question but as I re-read it it dawned on me that the answer I had would only mislead you. So here's a shorter answer :)

But I think its a bad idea to add a reference to dataAccess layer in my ProductServiceApi.

  • If as I suspect ProductServiceApi is a WebAPI Project, then you can't really (and shouldn't anyway) do anything to avoid this; in this case is it's effectively acting as Automapper's Composition Root*, and in order for it to do that, it must have a reference to all of these things - otherwise it cannot compose object mappings for you.

  • That's perfectly OK and not a problem at all from a design point of view ; you could "kick the can down the road" and do your Mapping configuration in a separate assembly, but then ProductServiceApi will have to reference that assembly meaning by association it is still directly referencing the lower layers.

So whilst there are things that I suspect need attention in your current design and can be discussed further, I don't believe you have a problem with the scenario outlined in this specific question :)

Examples of things that might need looking at

  • You have a single Contract Layer that I presume contains abstractions...this should be split out into multiple layers each of which contains a cohesive set of abstractions related to one part of the system.
  • Also, having your Domain objects defined in your DTO Layer sounds a bit odd.

But these are things for another question...

*(I know that Composition Root is more often than not used in the context of IoC libraries however in this case you are composing a Mapping between types which is a kind of IoC so I'm taking the view that the principle is the same).

Community
  • 1
  • 1
Stephen Byrne
  • 7,400
  • 1
  • 31
  • 51