I have my entities such as Customer, Order etc. defined in my Domain Model.
Now I want to define an interface called IRepository to represent my persistence layer, i will further have SQLRepository, CacheRepository which implement IRepository.
Now i’m wondering if I should define IRepository in the Domain Model or the Data Access Layer? I guess SQLRepository and CacheRepository needs to go in the DAL, but does IRepository go in there too?
Further, for example my Repository returns a list of Customers from the Customer table, i’m a bit confused about how to design this, it seems like i end up repeating types in DAL and Domain Model. See example below:
In the application i want to do something like this :
var repository = new SQLRepository();
//Below repository.customers represents customer table
List<Customer> customers = repository.Customers.list();
So In my Domain:
class Customer
{
public int id;
public string name;
}
In my DAL:
class SqlRepository:IRepository
{
public CustomerTable Customers;
}
class CustomerTable
{
public List<Customer> list();
}
I wanted to know if there’s a better way to design these layers?
* UPDATE
I already have the DAL and the Domain defined in different class libraries/assemblies. Initially i thought i will have POCO entities like Customer which represent one record in the database table, but then where do declare Customer.Add(customer) ? does it go in DAL ? I don’t want business rules in DAL, if i start adding methods in my Entities they become complex having persistance logic as well as business logic in them.
The following information is for another problem it shows you a simple project structure: Placing Model in separate assembly
My opinion is that the IRepository interface should be public in your DataLayer project. The implementations can be private (use a Factory or Dependency Injection to load them in other projects) and the Domain Model you create can be shared by your Business Layer and Data Layer.