如何使用NHibernate组织数据库层 [英] How to organize database layer using NHibernate

查看:105
本文介绍了如何使用NHibernate组织数据库层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要注重的问题在这里,我们可以说,我有一个数据库层和一个应用层。因此,对于应用程序可以访问我不得不经历(当然)数据库层数据库。

To focus on the question here, we can say that I have one database layer and one application layer. So for the application to get access to the database I have to go through the database layer (of course).

现在的事情是,我想使用LINQ来写我的查询。我可以何去何从两个不同的路径。一种方法的 [A] 将在DBLayer创造约300个功能,并从应用程序调用它们(如 GetAllUsers()) 。

Now the thing is that I want to write my queries using LINQ. And I could go two different paths here. One way [A] would be to create about 300 individual functions in the DBLayer, and call them from the Application (eg. GetAllUsers()).

[B] 的DBLayer可能或多或少只是提供的DbContext,然后我可以运行在应用层的LINQ查询( DBContext.GetAll<用户>( )。凡(X =方式> X ...

OR [B] the DBLayer could more or less just offer a DBContext and then I can run the LINQ queries from the Application Layer (DBContext.GetAll<Users>().Where(x => x...).

我认为[B]路径将是更具吸引力,或者至少更讨人喜欢,因为它不会被塞满了大量的小功能。但是,你会说什么?

I think that the [B] path would be more appealing, or at least less annoying since it wouldn't be so filled up with small functions. But what would you say?

不过用[B]路径的问题是,如果我能避免它,我不希望在应用程序NHibernate的一个参考,但我似乎并没有得到访问的LINQ功能的NHibernate没有引用它。是否有一个很好的解决这个?

However the problem with the [B] path is that, if I can avoid it I don't want to have a reference to NHibernate in the Application, but I don't seem to get access to the LINQ for NHibernate functions without referencing it. Is there a good solution to this?

推荐答案

在提到你,你需要创建多达由应用需求所需要不需要创建300的功能选项A。而有些功能可以得到降低的功能数量相等一个标准。

Option A with the mention what you don't need to create 300 functions, you need to create as many as required by the app needs. And some functions can get a Criteria which reduces the nubmer of functions.

最重要的事情是,所有这些功能将返回应用程序对象和不持续性对象,从而使应用程序分离的形式,其余的持久性。当然,商业/ UI对象应该不知道NH无论是。

The most important things is that all those functions will return app objects and NOT persistence objects, thus keeping the rest of the app decoupled form the persistence. Of course, the business/ui objects shouldn't know about NH either.

这篇关于如何使用NHibernate组织数据库层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆