mercredi 14 mars 2018

Where should Mapper go in N-tier architecture?

I have a very simple question: Where (in which layer) a database Model/Entity (an object returned from database) should become a DTO? Where should the Mapper go?

Here's a little more background. The architecture looks like this:

Database -> Foo.Data -> Foo.Business -> WCF -> Intranet

I'm using EF to get data from the database. I don't use additional Repo/UoW. I have simple Data layer before the Service layer (called Business).

This is how my sample Data class looks like (simplified):

public class ItemData
{
    public IContextFactory ContextFactory { get; }
    internal ItemData(IContextFactory contextFactory)
    {
        ContextFactory = contextFactory;
    }

    public IEnumerable<ItemDto> GetItems()
    {
        using (var context = ContextFactory.CreateFniContext())
        {
            var items = context.Items.AsNoTracking()
                .Select(row => new ItemDto()
                {
                    ItemId = row.itemID,
                    Name = row.itemName
                })
                .OrderBy(row => row.Name)
                .ToList();
            return items;
        }
    }
}

So, I transfer from Entity to Dto in one call. I'm returning Dto to the service layer which will return Dto to WCF (final layer), which will send this Dto.. wherever.

I'm confused though. How about going back? WCF receives Dto in UpdateItem method. Passes this Dto to Service layer. What does Service layer do? It has to ask Data layer to update the database. What should be the paramter?

I have a few questions:

  1. Should I return Dto from Data layer? If "it depends", when I should/shoudn't? (I saw Julie Lerman course, and she sometimes does, sometimes not).

  2. What should be the parameter to Foo.Data.UpdateItem(...) method? Dto or Entity? Where should be the Mapper?

Thanks!

Aucun commentaire:

Enregistrer un commentaire