我目前使用的是3层架构(DAL,BLL,Presentation Layer).
我想知道如何使用LINQ to SQL实现3层架构.我不知道LINQ应该是DAL还是BLL.LiNQ似乎是DAL和BLL的融合.
有没有人以前在3层架构中实现LINQ?
我使用Linq-to-SQL/XML,我认为我的应用程序是3层.pre-Linq应用程序之间的区别在于,现在数据访问层非常小而且更轻,这实际上是一件非常好的事情!
在我的旧DAL中,我会有以下方法:
public virtual int CountCustomersInCountry(string country) {
// Plug in raw SQL.
}
public virtual List<Customer> GetCustomersInCountry(string country) {
// Plug in raw SQL.
}
public virtual int CountCustomersForDepartment(string department) {
// Plug in raw SQL.
}
public virtual List<Customer> GetCustomersForDepartment(string department) {
// Plug in raw SQL.
}
etc. etc. ad-infinitum
Run Code Online (Sandbox Code Playgroud)
我现在有以下几种方法:
public virtual int Count(Expression<Func<T, bool>> where) {
// Plug in Linq-to-SQL DataContext here.
}
public virtual T Get(Expression<Func<T, bool>> where) {
// Plug in Linq-to-SQL DataContext here.
}
public virtual List<T> Get(Expression<Func<T, bool>> where, string orderByField, int offset, int count) {
// Plug in Linq-to-SQL DataContext here.
}
Run Code Online (Sandbox Code Playgroud)
要调用新的DAL方法并从DynamicLinq获得一点帮助,我使用:
int countryCount = Count(c => c.Country == country);
List<Customer> customers = Get(c => c.Country == country, "inserted", 0, 25);
int departmentCount = Count(c => c.Department == department);
List<Customer> customers = Get(c => c.Department == department, "inserted", 0, 25);
Run Code Online (Sandbox Code Playgroud)
在你进入使用Linq2SQL成为单行调用的Adds,Updates和Deletes之前的所有内容!我的DAL现在包含10种方法,而以前很容易为DAL照顾的每个对象获得20到30种方法!我强烈建议你试着试试它,因为它真的会为你节省很多代码.
归档时间: |
|
查看次数: |
3022 次 |
最近记录: |