使用 Autofac、NCommon 和 Fluent NHibernate 调用存储过程
我在项目中尝试使用这三个库时遇到问题。据我了解,因此根据其常见问题解答,Fluent NHibernate 不直接支持调用存储过程。因此,我定义了一个简单的 hbl.xml 文件,其中包含存储过程的映射:
<?xml version='1.0' encoding='utf-8'?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="My.Data.Mappings" assembly="My.Data.Mappings">
<sql-query name="MyStoredProc" callable="true">
<query-param name="paramA" type="date" />
<query-param name="paramB" type="int" />
<return alias="MyResultClass" class="My.Data.Mappings.MyResultClass, EP.Core.Data.Mappings" />
exec myStoredProc @paramA = :paramA, @paramB = :paramB
</sql-query>
</hibernate-mapping>
现在,在我的服务代码中,使用 NCommon 库,我有:
using (var scope = new UnitOfWorkScope())
{
...
DontKnowWhereToGetSessionManager.Instance.Session.GetNamedQuery("MyStoredProc").List<MyResultClass();
...
}
因此,我可以看到获取 DontKnowWhereToGetSessionManager 的唯一方法是将 Autofac 注入到我的服务。但这似乎是错误的方式。有没有办法从 UnitOfWorkScope 获取它?或者我应该用 Autofac 注入它?
I'm having a problem trying to use these three libraries in a project. I understand and therefore calling stored procedures isn't directly supported in Fluent NHibernate according to their FAQ. So I defined a simple hbl.xml file with a mapping for my stored procedure:
<?xml version='1.0' encoding='utf-8'?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="My.Data.Mappings" assembly="My.Data.Mappings">
<sql-query name="MyStoredProc" callable="true">
<query-param name="paramA" type="date" />
<query-param name="paramB" type="int" />
<return alias="MyResultClass" class="My.Data.Mappings.MyResultClass, EP.Core.Data.Mappings" />
exec myStoredProc @paramA = :paramA, @paramB = :paramB
</sql-query>
</hibernate-mapping>
So now, in my service code, using the NCommon library, I have:
using (var scope = new UnitOfWorkScope())
{
...
DontKnowWhereToGetSessionManager.Instance.Session.GetNamedQuery("MyStoredProc").List<MyResultClass();
...
}
So the only way I can see of getting DontKnowWhereToGetSessionManager is to have Autofac inject it into my service. But that seems like the wrong way. Is there a way to get it from the UnitOfWorkScope? Or should I just inject it with Autofac?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 UnitOfWorkScope 的 CurrentUnitOfWork 属性获取当前工作单元。
You can get the current unit of work by using UnitOfWorkScope's CurrentUnitOfWork property.