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?
You can get the current unit of work by using UnitOfWorkScope's CurrentUnitOfWork property.
using (var scope = new UnitOfWorkScope())
{
var session = scope.CurrentUnitOfWork<NHUnitOfwork>().GetSession<MyResultClass>();
sesion.GetNamedQuery("MyStoredProc").List<MyResultClass>();
}