Wiki
Clone wikiDataAccess.Core / NHibernate
Quick Start
Overview
DataAccess.Core.Nh is the implementation of DataAccess.Core for NHibernate.
Features
This implementation is compatible with all frameworks available for DataAccess.Core
Principal implementations
- SessionContextProvider implements IContextProvider<ISession>
- StatelessSessionContextProvider implements IContextProvider<ISession>
- NhTransactionProvider implements ITransactionProvider<ISession>
- NhQueryableDataFacadeService implements IQueryableDataFacadeService
- NhQueryDefinitionDataFacade implements IQueryDataFacade
Transaction and context providers depends of ISessionFactory instance (Nhibernate interface).
Getting Started
The easiest way to get started is by installing the available NuGet packages.
Quick examples
Let's see how can be used the principals implementation.
At the first place, you need to build your ISessionFactory as you prefer, but maybe It could be convenient to use extension methods of Configuration instance, offered by this library.
#!c# // creating a nhibernate configuration var config = new NHibernate.Cfg.Configuration { .Dialect<NHibernate.Dialect.MsSql2012Dialect>() //.Dialect<NHibernate.Dialect.MsSql2008Dialect>() .ConnectionString(connectionStr) .GenerateStatistics() .ShowSql() .UseProxyValidator() //.AddFluentMappings() //you can indicate the assembly where are defined all fluent mappings .AddFluentMappings(typeof(UserMap), typeof(MessageMap), typeof(EntityWithComposedKeyMap), typeof(EntityWithStringKeyMap), typeof(EmployeeMap), typeof(StateEmployeeMap), typeof(PrivateEmployeeMap), typeof(EntityPriceMap), typeof(JobMap)); // builds configuration in order to get ISessionFactory instance. var mySessionfactory = config.BuildSessionFactory(); // Notice that all fluent mappings are Conformist, not defined for FluentNHibernate library.
Once you create your ISessionFactory, you can follow the next steps:
create a IContextProvider<ISession> instance, using the session factory resolved previously as dependency.
#!c# IContextProvider<ISession> sessionProvider = new SessionContextProvider(mySessionfactory);
or create a IContextProvider<ISession> instance, but taking advantage of session stateless.
#!c# IContextProvider<ISession> sessionProvider = new StatelessSessionContextProvider(mySessionfactory);
create a ITransactionProvider<ISession> instance, injecting IContextProvider<ISession> instance as dependency.
#!c# ITransactionProvider<ISession> transactionProvider = new SessionContextProvider(sessionProvider);
create your IQueryableDataFacadeService instance, injecting ITransactionProvider<ISession> as dependencies
#!c# IQueryableDataFacadeService facade = new NhQueryableDataFacadeService(transactionProvider);
Use cases - using Dependency Injection
In the real world, all the above initialization of providers and facades must be delegated to a Dependency Injection engine.. It's trivial the choice.
SimpleInjector
see (doc) for details.
#!c# var container = new Container(); container.Register<IQueryableDataFacadeService, NhQueryableDataFacadeService>(); container.Register<ITransactionProvider<ISession>, NhTransactionProvider>(); container.Register<IContextProvider<ISession>, SessionContextProvider>(); container.Register<ISessionFactory>(() => CreateMySessionFactory(), LifetimeScope.Singleton);
or maybe you want to take advantage of stateless session, so in this case:
#!c# container.Register<IContextProvider<ISession>, StatelessSessionContextProvider>();
DryIoc
see (doc) for details.
#!c# var container = new Container(); container.Register<IQueryableDataFacadeService, NhQueryableDataFacadeService>(); container.Register<ITransactionProvider<ISession>, NhTransactionProvider>(); container.Register<IContextProvider<ISession>, SessionContextProvider>(); container.RegisterDelegate<ISessionFactory>(resolver => CreateMySessionFactory(), Reuse.Singleton);
LightInject
see (doc) for details.
#!c# var container = new Container(); container.Register<IQueryableDataFacadeService, NhQueryableDataFacadeService>(); container.Register<ITransactionProvider<ISession>, NhTransactionProvider>(); container.Register<IContextProvider<ISession>, SessionContextProvider>(); container.Register<ISessionFactory>(factory => CreateMySessionFactory(), new PerContainerLifetime());
Updated