适用于中小型项目的 ASP.NET MVC、Nhibernate 和存储库
我目前正在开发一个小型 ASP.NET MVC 项目。
我正在尝试实现 Nhibernate 以持久保存在 MS Sql Server 数据库上。 在花了很长时间研究 DDD 和在 Internet 上找到的其他项目后,我决定采用存储库模式。 现在我面临着两难的境地。
使用 Nhinbernate 时我真的需要存储库吗?
拥有一个与 Nhinbernate 交互的服务层(我目前没有服务层),避免多次编写类似的内容不是更好吗:
public Domain.Reminder GetById(Guid Code)
{
return (_session.Get<Domain.Reminder>(Code));
}
public Domain.Reminder LoadById(Guid Code)
{
return (_session.Load<Domain.Reminder>(Code));
}
public bool Save(Domain.Reminder Reminder)
{
_session.SaveOrUpdate(Reminder);
return (true);
}
public bool Delete(Domain.Reminder Reminder)
{
_session.Delete(Reminder);
return (true);
}
我发现了一个旧的 Ayende 的 POST,它是针对存储库的。
我知道围绕这些主题存在着巨大的争论,答案总是......取决于,但在我看来,由于抽象层太多,事情会变得更加复杂且难以理解。
我错了吗?
I am working on a small ASP.NET MVC project at the moment.
I am trying to implement Nhibernate to persist on a MS Sql Server database.
Having spent long hours studying DDD and other projects found on the Internet I have decided to go for the repository pattern.
Now I ma facing a dilemma.
Do I really need a repository when using Nhinbernate?
Wouldn't it be better to have a Service Layer (I don't have a Service Layer at the moment) which interacts with Nhinbernate avoiding to write many times something like that:
public Domain.Reminder GetById(Guid Code)
{
return (_session.Get<Domain.Reminder>(Code));
}
public Domain.Reminder LoadById(Guid Code)
{
return (_session.Load<Domain.Reminder>(Code));
}
public bool Save(Domain.Reminder Reminder)
{
_session.SaveOrUpdate(Reminder);
return (true);
}
public bool Delete(Domain.Reminder Reminder)
{
_session.Delete(Reminder);
return (true);
}
I found an old Ayende's POST which is against repositories.
I know there's a huge debate around these topics and the answer is always ... depends, but it seems to me that with too many layers of abstractions things get more complicated and hard to follow.
Am I wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ayende 反对以您的方式编写存储库,因为您提出这个问题的原因是,它是重复的代码,而 NH 无论如何都可以处理所有这些代码。他主张像调用存储库一样直接调用 NH,而不必担心它。
我非常同意他的观点。除了付出更多的努力之外,确实没有什么收获。
Ayende was against writing a repository the way you did because of the reasons you asked this question, it is repetitive code and NH can handle all of it anyways. He advocates just calling NH directly as you would a repository, and stop worrying about it.
I pretty much agree with him. There really isn't much to gain except for more work.
请改用通用存储库。每个类一个存储库很容易被过度杀伤。
我使用一个包含 Get、Load、Save 方法和各种匹配方法的存储库(一种用于 Linq,一种用于我的域查询)。
最后一个方法接受 ICreateCitiera 实现。下面是接口及其一种实现。
这让我可以将查询分解到他们自己的类中,并轻松地在我的项目中重用它们。
Use a generic Repository instead. One repository per class can easily be overkill.
I use one repository with Get, Load, Save methods and various Matching-methods (one for Linq and one for my domain queries).
The last method accepts a ICreateCritiera implementation. Below is the interface and one implementation of it.
This lets me break out the queries to their own classes and reuse them all over my project easily.
我发现了一些你应该使用 Repository/DAO/Whatever 的原因。
知道的就这些了,其他的想不起来了。
I found some reasons why you should use Repository/DAO/Whatever.
That's it for know, can't remember anything else.