我想回答#3 ...
我认为这是少“臭”,更“懒”。下面是一个典型的“资源库”,我已经看到周围的互联网络...
public interface IRepository {
// Query operations.
IQueryable<T> All<T>();
IQueryable<T> Find<T>(Expression<Func<T, bool>> expression);
T Single<T>(Expression<Func<T, bool>> expression);
// Save operations.
T Add<T>(T objectToAdd);
void Delete<T>(T objectToDelete);
T Update<T>(T objectToUpdate);
}
据我所知,这是少了仓库,并多了一个“会议”或“工作单位” 。这是一种方便的方法,可以将您正在使用的任何数据库技术抽象出来,而只是转而使用非常通用的接口。所以让我们将其重命名为ISession
。这是我最近一直在做的模式。
public class PeopleRepository {
private readonly ISession session;
public PeopleRepository(ISession session) {
this.session = session;
}
public virtual IEnumerable<Person> Active() {
return session.Find<Person>(p => p.Active).OrderBy(p => p.LastName).ThenBy(p => p.FirstName);
}
public virtual IEnumerable<Person> ByLastName(string name) {
return session.Find<Person>(p => p.Active && p.LastName.StartsWith(lastName)).OrderBy(p => p.LastName).ThenBy(p => p.FirstName);
}
public virtual void DeletePerson(int personId) {
// We don't really delete people; we mark them as inactive.
var person = session.Single<Person>(p => p.Id == personId);
person.Active = false;
session.Update(person);
}
}
在此设置中,ISession
是数据存储的通用链接。但是,PersonRepository
对于在Person
对象上执行的查询和操作的类型非常具体。
希望这会有所帮助。
我现在也是这种方法的粉丝。感谢您的比较和解释。 – 2010-12-28 15:56:22