使用 nHibernate 和存储库模式,需要一些指导
好的,我刚刚进入 nhibernate(使用 fluid)。
我喜欢它的一件事是我可以使用存储库模式(从 nhibernate rhino 博客中了解它)。
基本上使用泛型,我可以创建适用于所有数据库表的方法。
public interface IRepository<T>
{
T GetById(int id);
ICollection<T> FindAll();
void Add(T entity);
void Remove(T entity);
}
public class Repository<T> : IRepository<T>
{
public ISession Session
{
get
{
return SessionProvider.GetSession();
}
}
public T GetById(int id)
{
return Session.Get<T>(id);
}
public ICollection<T> FindAll()
{
return Session.CreateCriteria(typeof(T)).List<T>();
}
public void Add(T t)
{
Session.Save(t);
}
public void Remove(T t)
{
Session.Delete(t);
}
}
然后,我继承 Repository 类,然后可以添加特定于该实体的方法。
当尝试添加 Update 方法时,有人提到存储库模式应该作用于集合?我在这里看问题的方式不对吗?为什么我无法创建更新方法?
我尝试添加更新方法,但我对如何处理会话和更新数据库感到困惑?
我希望每个实体的所有数据库访问都在一个地方,因此 UserRepository 将具有所有基本的 CRUD,然后可能还有一些其他方法,例如 GetUserByEmail() 等。
Ok so I 'm just getting into nhibernate (using fluent).
One thing that I love about it is that I can use the Repository pattern (read about it from the nhibernate rhino blog).
Basically using generics, I can create methods that will work accross ALL my database tables.
public interface IRepository<T>
{
T GetById(int id);
ICollection<T> FindAll();
void Add(T entity);
void Remove(T entity);
}
public class Repository<T> : IRepository<T>
{
public ISession Session
{
get
{
return SessionProvider.GetSession();
}
}
public T GetById(int id)
{
return Session.Get<T>(id);
}
public ICollection<T> FindAll()
{
return Session.CreateCriteria(typeof(T)).List<T>();
}
public void Add(T t)
{
Session.Save(t);
}
public void Remove(T t)
{
Session.Delete(t);
}
}
I then inherit the Repository class and I can then add methods that are specific to that entity.
When trying to add an Update method, someone mentioned that the Repository pattern is suppose to act on collections? Am I looking at things incorrectly here? Why can't I create an update method?
I tried adding a update method, but I'm confused as to how I will handle the session and update the database?
I want a single place for all my database access for each entity, so UserRepository will have all basic CRUD and then maybe some other methods like GetUserByEmail() etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用存储库模式 - 使用 UnitOfWork 模式,并将定义的查询 ICriteria 传递给 ISession。本质上,Repo 模式是包装一些不需要用 NH 包装的东西。
请参阅http://ayende.com /Blog/archive/2009/04/17/repository-is-the-new-singleton.aspx 了解更多信息
Don't use the repository pattern - use the UnitOfWork pattern instead, and pass defined query ICriteria to the ISession. Essentially the Repo pattern is wrapping something that doesn't need to be wrapped with NH.
see http://ayende.com/Blog/archive/2009/04/17/repository-is-the-new-singleton.aspx for more info
也许你听错了或者有人说错了——存储库模式应该公开类似集合的行为,而不是对集合进行操作。就像您可以添加、删除和搜索集合中的项目一样,存储库提供针对数据库的保存、删除和搜索操作。
我建议您下载 S#arp Architecture 的代码。它包括一个可以轻松重用的存储库实现。如果您不想依赖它,至少您可以花一些时间研究它们的实现,以便您更好地了解如何自己处理它。
Perhaps you misheard or someone mispoke - the Repository pattern is supposed to expose collection like behavior, not operate on collections. Just like you can add, remove and search for items in a collection, your repository offers save, delete and search operations that work against your database.
I suggest you download the code for S#arp Architecture. It includes a repository implementation that you can reuse quite easily. If you don't want to take the dependency, at the very least you can spend some time studying their implementation to give you a better idea of how to approach it yourself.