jeudi 11 août 2016

Service layer shouldn't return ViewModels, but what if I need to return something that isn't Dbset. Do I create another Entities?

So I'm using this pattern on my ASP.NET-MVC5 application.

Controller <-> Service Layer <-> Data Layer <-> Database

I use AutoMapper to map between my DbSet result from Service Layer and my ViewModels in controller.

But what if I need to return custom entities from my service layer that isn't DbSet? Do I need to create a new entities to keep my pattern clean?

For example :

DbSet

public class Customer
{
    public int CustomerID { get; set; }
    public string Name { get; set; }
    public string Phone { get; set; }
    public string Address { get; set; }
}

public class Order
{
    public int OrderID { get; set; }
    public dec Amount { get; set; }
    public int CustomerID { get; set; }
    public Customer Customer { get; set; }
}

Now if I have something like CalculateCustomerTax(int ID) in my service layer and this method will return 3 values which is CustomerName, TotalTax, TotalAmount.

What am I suppose to do to keep this pattern clean since returning ViewModels on service layer defeat the purpose.

Any help will be appreciated

Aucun commentaire:

Enregistrer un commentaire