持久層使用了LINQ TO SQL。
生成工具采用了T4 模板里面自帶的一個linq sql 生成工具。比自帶的那個好多了。
?
持久層用的最多的3個模式
ASP.NET2.0完全開發指南、dao,repository, dao+repository
它們之間的區別不說了。我用的是另外一種模式repository+extensions,這種方式比較適合查詢語句相對簡單的項目。
請看我寫的這篇文章【輕量級數據過濾方案】
http://www.cnblogs.com/yuanhuaming/archive/2010/02/05/1664564.html
?
javaweb開發基本三層架構。hibernate , entity framework 這類orm已經提供了很好的條件封裝機制,只需提供一個通用的數據訪問接口就行了,在社區甚至有人認為使用這些orm根本就不需要dal。
其實linq也可以只使用一個通用接口,只不過我對表達式樹不了解,使用方面有困難。
看一下項目中repository的接口
public interface IRepository<T>
{
T Get(object id);
IQueryable<T> FindAll();
?
bool IsExists(Expression<Func<T, bool>> predicate);
?
void Add(T entity);
void Add(List<T> entitys);
void Delete(T entity);
void Delete(List<T> entitys);
void Delete(Expression<Func<T, bool>> predicate);
?
void Save();
}
這里FindAll()方法沒有直接返回List集合。數據的加載我基本上都延遲到了Controller再加載的。
延遲到controller層有很多好處:
javaweb三層架構?1.可以減少DTO。 使用匿名類可以直接放入View中,而不需要寫一個DTO作中間傳輸。
2.數據需要時在加載,有效利用資源。
3.IQueryable<T> 的靈活性很高。
這些都會在介紹controller的。
?
Spring MVC。那么復雜查詢怎么處理?
我們來看一個簡單的例子
這是一個擴展question查詢的類
public static class QuestionExtensions
{
public static IQueryable<Question> GetQuestionByUser(this IQueryable<Question> query, string userId)
{
return query.Where(a => a.CreatedBy == userId);
}
}
我們在service層就可以這樣使用。
public class QuestionBiz : BaseBiz<Question>
{
private readonly IRepository<Question> _repository;
public QuestionBiz(IRepository<Question> repository)
: base(repository)
{
_repository = repository;
}
?
public IQueryable<Question> GetQuestionByUser(string userId)
{
return _repository.FindAll().GetQuestionByUser(userId);
}
}
數據處理全部放在了dal,service只負責調用。dal只開放了一個通用接口就可以實現dao+repository模式了。
javaweb頁面跳轉,?
?
?
附上souce了,https://iknowledge.svn.codeplex.com/svn? ,程序還未完成,理解就行。