I have a web app with following design.
View -> Controller -> Service || Repository -> DB
For example :
Entity Model
public class Customer
{
[Key]
public int CustomerID { get; set; }
public char Gender { get; set; }
//rest of property here..
}
Context
public class TestContext : DbContext
{
public DbSet<Customer> Customers { get; set; } //register entity class as DbSet
...
Repository
public IEnumerable<Customer> List()
{
return _customer.AsEnumerable();
}
public void Insert()
{
//...
}
public void Update()
{
//...
}
Service
public IEnumerable<Customer> MaleCustomerList()
{
return _repo.Customer.List().Where(w => w.Gender == "M");
}
public IEnumerable<Customer> FemaleCustomerList()
{
return _repo.Customer.List().Where(w => w.Gender == "F");
}
So far we have no problem because service is returning entity model which is mapped to an actual database table. But what if I need to do some complex queries that return a completely different object, for example I need to group certain customer and calculate their percentage. I will need to return something like IEnumerable<CustomerGroup>
where CustomerGroup
is completely different with Customer
, but it is not an actual table (because I don't need it) in the database.
So my question is, is it okay to make a new Entity model
public class CustomerGroup
{
public int GroupName { get; set; }
public decimal Percentage { get; set; }
}
but the different is that it's not registered in the Database Context DbSet
so my service can return this type?
Aucun commentaire:
Enregistrer un commentaire