Currently I'm building an windows application using sqlite. In the data base there is a table say User
, and in my code there is a Repository<User>
and a UserManager
. I think it's a very common design. In the repository there is a List
method:
//Repository<User> class
public List<User> List(where, orderby, topN parameters and etc)
{
//query and return
}
This brings a problem, if I want to do something complex in UserManager.cs
:
//UserManager.cs
public List<User> ListUsersWithBankAccounts()
{
var userRep = new UserRepository();
var bankRep = new BankAccountRepository();
var result = //do something complex, say "I want the users live in NY
//and have at least two bank accounts in the system
}
You can see, returning List<User>
brings performance issue, becuase the query is executed earlier than expected. Now I need to change it to something like a IQueryable<T>
:
//Repository<User> class
public TableQuery<User> List(where, orderby, topN parameters and etc)
{
//query and return
}
TableQuery<T>
is part of the sqlite driver, which is almost equals to IQueryable<T>
in EF, which provides a query and won't execute it immediately. But now the problem is: in UserManager.cs
, it doesn't know what is a TableQuery<T>
, I need to add new reference and import namespaces like using SQLite.Query
in the business layer project. It really brings bad code feeling. Why should my business layer know the details of the database? why should the business layer know what's SQLite? What's the correct design then?
TableQuery<T>
is incorrect, norIQueryable<T>
is correct. Even I added a new transfer interface, likeIQuery<T>
, it is still base onTableQuery<T>
, and when I useIQuery<T>
in my business layer, everything is the same. It just looks unrelated with SQLite but in fact, it's wearing a mask. – ReservistGetTheStuffIWant()
to the DA layer, and not have to know anything about it's implementation (or am I missing something?). On the other hand, @DennisTraub may be right in recommending some loading strategy in the repository instead, or some kind of caching, if you don't absolutely need to call the DB every time. – Chaille