System.NullReferenceException after upgrade to EF 4.1 - asp.net-mvc-3

I have an MVC3 app which was using EF CTP5. After upgrading to EF 4.1 I get NullReferenceException thrown from here:
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
at System.Data.Entity.Internal.InternalContext.Initialize()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
I've got EF 4.1 bits from NuGet.
Database is initialized using custom initializer
public class RecreateDatabaseInitializer : IDatabaseInitializer<DatabaseContext>
{
public void InitializeDatabase(DatabaseContext context)
{
if (ConfigurationManager.ConnectionStrings["DatabaseContextSA"] == null)
{
EventLog.WriteEntry("RecreateDatabaseInitializer", "Connection string 'DatabaseContextSA' doesn't exist in config file.", EventLogEntryType.Warning);
return;
}
using (var ctx = new DatabaseContext("DatabaseContextSA"))
{
if (ctx.Database.Exists())
DropDatabase(ctx);
CreateDatabase(ctx);
InitializeDatabaseObjects(ctx);
ctx.SaveChanges();
}
PopulateDatabase(context);
context.SaveChanges();
}
}
The Exception is thrown from PopulateDatabase() method.
Any ideas?
UPDATE:
It seems that the problem stems from the instantiation of the second DatabaseContext to manually recreate the database. It must somehow interfere with the original context.

I have used Ladislav's decorator for closing opened connections but I had to modify it a bit. This was required (I believe) as I use sql authentication and have two connection strings (one with elevated permissions and one with only read/write).
public class ForceDeleteInitializer : IDatabaseInitializer<DatabaseContext>
{
private readonly IDatabaseInitializer<DatabaseContext> InnerInitializer;
public ForceDeleteInitializer(IDatabaseInitializer<DatabaseContext> innerInitializer)
{
this.InnerInitializer = innerInitializer;
}
public void InitializeDatabase(DatabaseContext context)
{
using (var ctx = new DatabaseContext("DatabaseContextSA"))
{
Database.SetInitializer<DatabaseContext>(null);
ctx.Database.ExecuteSqlCommand("ALTER DATABASE " + ctx.Database.Connection.Database + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
}
this.InnerInitializer.InitializeDatabase(context);
Database.SetInitializer<DatabaseContext>(this);
}
}

Related

AUTOFAC THROWIN EXCEPTION ON DBCONTEXT CONSTRUCTOR PARAMETER IN WINDOWS CONSOLE APP

I am using reverse poco to create entities from an sql database. The tt script I am using is from the on the plural website on the tutorial on EF. In a windows console app I am using Autofac to dependency injection of the the ddbcontext interface ISCDInnoPRDDataLayerDbContext:
public partial class SCDInnoPRDDataLayerDbContext : Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext, ISCDInnoPRDDataLayerDbContext
and of a class that will takes that interface as an input.
public class DLScdHopDataNarRepository :
DLScdBaseRepository<Read_Only_HopDataNar>,
IDLScdHopDataNarRepository
{
public DLScdHopDataNarRepository(ISCDInnoPRDDataLayerDbContext dataLayerDbContext)
: base(dataLayerDbContext)
{
}
Autofac is throwing an exception when I resolve the DLScdHopDataNarRepository class. The exception is on the constructor parameter to the SCDInnoPRDDataLayerDbContext class.
Exceptions:
An exception was thrown while invoking the constructor 'Void .ctor(System.String)' on type 'SCDInnoPRDDataLayerDbContext'.
at SCDInnoPRD.DataLayer.Context.SCDInnoPRDDataLayerDbContext..ctor(String connectionString)
at lambda_method(Closure , Object[] )
at Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate()
+ InnerException {"The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception."}
System.Exception {System.TypeInitializationException}
Below is the autofac registration and the code that resolves the DLScdHopDataNarRepository .
Registration code:
public class SCDInnoPRDModule :Module
{
public string ConnectionString { get; private set; }
public SCDInnoPRDModule(string connectionString)
{
ConnectionString = connectionString;
}
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<SCDInnoPRDDataLayerDbContext>()
.As<ISCDInnoPRDDataLayerDbContext>()
.Keyed<ISCDInnoPRDDataLayerDbContext>("scdb")
.WithParameter((pi, ctx) => pi.ParameterType == typeof(string)
&& pi.Name == "connectionString",
(pi, ctx) => getConnectionString(ConnectionString))
.InstancePerLifetimeScope();
builder.RegisterType<DLScdHopDataNarRepository>()
.As<IDLScdHopDataNarRepository>()
.Keyed<IDLScdHopDataNarRepository>("sdhopres")
.InstancePerLifetimeScope();
builder.RegisterType<DLScdNarsService>()
.As<IDLScdNarsService>()
.Keyed<IDLScdNarsService>("sddlser")
.InstancePerLifetimeScope();
}
private Func< string,string> getConnectionString = delegate (string connectionString)
{
string retVal = connectionString;
return retVal;
};
Resolve code:
using (var config= new ContainerConfig())
{
// register dependency injectopn
var container = config.Configure();
using (var scope = container.BeginLifetimeScope())
try
{ // exception occurs here
var dlscd = container.ResolveNamed("sdhopres");
I have to a point where I can solve this problem. Any suggestion to fix the problem would be appreciated.
Any example source code project that use autofac to do di of a dbcontext in a windows console application would be helpful. I can not find any such project on line.
I can post the entire source code project on GitHub or a website for download if anyone would like to view the code

Implementing different destinations in applications on the Tomcat server

Earlier this year I developed an implementation of the SAP JCO CustomDestinationProvider for one of my Spring MVC tomcat applications. In my application, I use this implementation to call a BAPI in my SAP R/3 system to retrieve data.
I am now working on a second Spring MVC tomcat application that I want to have call a BAPI in my SAP R/3 system to retrieve data. It will be a different BAPI that I will be calling, thus it will be different data that I will be retrieving. Since this is a different application calling a different BAPI, I want to use a different SAP system user in my configurations. This new application will be running on the same physical tomcat server as the first application.
My question is should I develop another implementation of the SAP JCO CustomDestinationProvider for this new application or should I somehow reuse the first implementation? If the answer is that I should develop another implementation for this new application, I would expect then that I would develop another implementation for each new Spring MVC tomcat application that I develop that needs to talk to SAP. Is this correct thinking?
If I do a different implementation for this new application of mine, should I be using the same destination name in the code, or should I use a different name?
Below is the code for my first implementation of CustomDestinationDataProvider:
public class CustomDestinationDataProvider {
public class MyDestinationDataProvider implements DestinationDataProvider {
private DestinationDataEventListener eL;
private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();
public Properties getDestinationProperties(String destinationName) {
try {
Properties p = secureDBStorage.get(destinationName);
if(p!=null) {
if(p.isEmpty())
throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION, "destination configuration is incorrect", null);
return p;
}
return null;
} catch(RuntimeException re) {
throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR, re);
}
}
public void setDestinationDataEventListener(DestinationDataEventListener eventListener) {
this.eL = eventListener;
}
public boolean supportsEvents() {
return true;
}
public void changeProperties(String destName, Properties properties) {
synchronized(secureDBStorage) {
if(properties==null) {
if(secureDBStorage.remove(destName)!=null)
eL.deleted(destName);
} else {
secureDBStorage.put(destName, properties);
eL.updated(destName); // create or updated
}
}
}
}
public ArrayList<String> executeSAPCall(Properties connectProperties, ArrayList<String> partnumbers) throws Exception {
String destName = "ABAP_AS";
SAPDAO sapDAO = new SAPDAO();
ArrayList<MaterialBean> searchResults = new ArrayList<MaterialBean>();
MyDestinationDataProvider myProvider = new MyDestinationDataProvider();
boolean destinationDataProviderRegistered = com.sap.conn.jco.ext.Environment.isDestinationDataProviderRegistered();
JCoDestination dest;
try {
if (!destinationDataProviderRegistered) {
com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);
myProvider.changeProperties(destName, connectProperties);
}
} catch(IllegalStateException providerAlreadyRegisteredException) {
logger.error("executeSAPCall: providerAlreadyRegisteredException!");
}
try {
dest = JCoDestinationManager.getDestination(destName);
searchResults = sapDAO.searchSAP(dest, partnumbers);
} catch(JCoException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return searchResults;
}
}
If the answer is that I should not need to implement another CustomDestinationDataProvider for my second application, what other considerations do I need to keep in mind?
You can only register one DestinationDataProvider so the one you set must be able to handle both (or more) different connections. In order to do this, you need unique names for each connection, i.e. destName can't be the fixed value ABAP_AS, you need to create one for each connection.
Your current implementation of the provider looks good for me, but your method when calling the RFC is mixing the creation of the connection and the actual RFC-calling too much in my eyes. IMHO you should separate the former into its own method, so you can call it from other parts of your application to e.g. do other things than RFC-calling.
I've figured it out! I discovered two different ways to implement CustomDestinationDataProvider so that I could use multiple destinations.
Something that I did that helped out with both of my different solutions was change out the method in CustomDestinationDataProvider that instantiates the MyDestinationDataProvider inner class so that instead of returning ArrayList, it returns JCoDestination. I changed the name of this method from executeSAPCall to getDestination.
The first way that I discovered that allowed me to use multiple destinations, successfully changing out destinations, was to introduce a class variable for MyDestinationDataProvider so that I could keep my instantiated version. Please note that for this solution, the CustomDestinationDataProvider class is still embedded within my java application code.
I found that this solution only worked for one application. I was not able to use this mechanism in multiple applications on the same tomcat server, but at least I was finally able to successfully switch destinations. Here is the code for CustomDestinationDataProvider.java for this first solution:
public class CustomDestinationDataProvider {
private MyDestinationDataProvider gProvider; // class version of MyDestinationDataProvider
public class MyDestinationDataProvider implements DestinationDataProvider {
private DestinationDataEventListener eL;
private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();
public Properties getDestinationProperties(String destinationName) {
try {
Properties p = secureDBStorage.get(destinationName);
if(p!=null) {
if(p.isEmpty())
throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION, "destination configuration is incorrect", null);
return p;
}
return null;
} catch(RuntimeException re) {
System.out.println("getDestinationProperties: Exception detected!!! message = " + re.getMessage());
throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR, re);
}
}
public void setDestinationDataEventListener(DestinationDataEventListener eventListener) {
this.eL = eventListener;
}
public boolean supportsEvents() {
return true;
}
public void changeProperties(String destName, Properties properties) {
synchronized(secureDBStorage) {
if(properties==null) {
if(secureDBStorage.remove(destName)!=null) {
eL.deleted(destName);
}
} else {
secureDBStorage.put(destName, properties);
eL.updated(destName); // create or updated
}
}
}
}
public JCoDestination getDestination(String destName, Properties connectProperties) {
MyDestinationDataProvider myProvider = new MyDestinationDataProvider();
boolean destinationDataProviderRegistered = com.sap.conn.jco.ext.Environment.isDestinationDataProviderRegistered();
if (!destinationDataProviderRegistered) {
try {
com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);
gProvider = myProvider; // save our destination data provider in the class var
} catch(IllegalStateException providerAlreadyRegisteredException) {
throw new Error(providerAlreadyRegisteredException);
}
} else {
myProvider = gProvider; // get the destination data provider from the class var.
}
myProvider.changeProperties(destName, connectProperties);
JCoDestination dest = null;
try {
dest = JCoDestinationManager.getDestination(destName);
} catch(JCoException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
}
return dest;
}
}
This is the code in my servlet class that I use to instantiate and call CustomDestinationDataProvider within my application code:
CustomDestinationDataProvider cddp = new CustomDestinationDataProvider();
SAPDAO sapDAO = new SAPDAO();
Properties p1 = getProperties("SAPSystem01");
Properties p2 = getProperties("SAPSystem02");
try {
JCoDestination dest = cddp.getDestination("SAP_R3_USERID_01", p1); // establish the first destination
sapDAO.searchEmployees(dest, searchCriteria); // call the first BAPI
dest = cddp.getDestination("SAP_R3_USERID_02", p2); // establish the second destination
sapDAO.searchAvailability(dest); // call the second BAPI
} catch (Exception e) {
e.printStackTrace();
}
Again, this solution only works within one application. If you implement this code directly into more than one application, the first app that calls this code gets the resource and the other one will error out.
The second solution that I came up with allows multiple java applications to use the CustomDestinationDataProvider class at the same time. I broke the CustomDestinationDataProvider class out of my application code and created a separate java spring application for it (not a web application) for the purpose of creating a jar. I then transformed the MyDestinationDataProvider inner class into a singleton. Here's the code for the singleton version of CustomDestinationDataProvider:
public class CustomDestinationDataProvider {
public static class MyDestinationDataProvider implements DestinationDataProvider {
////////////////////////////////////////////////////////////////////
// The following lines convert MyDestinationDataProvider into a singleton. Notice
// that the MyDestinationDataProvider class has now been declared as static.
private static MyDestinationDataProvider myDestinationDataProvider = null;
private MyDestinationDataProvider() {
}
public static MyDestinationDataProvider getInstance() {
if (myDestinationDataProvider == null) {
myDestinationDataProvider = new MyDestinationDataProvider();
}
return myDestinationDataProvider;
}
////////////////////////////////////////////////////////////////////
private DestinationDataEventListener eL;
private HashMap<String, Properties> secureDBStorage = new HashMap<String, Properties>();
public Properties getDestinationProperties(String destinationName) {
try {
Properties p = secureDBStorage.get(destinationName);
if(p!=null) {
if(p.isEmpty())
throw new DataProviderException(DataProviderException.Reason.INVALID_CONFIGURATION, "destination configuration is incorrect", null);
return p;
}
return null;
} catch(RuntimeException re) {
throw new DataProviderException(DataProviderException.Reason.INTERNAL_ERROR, re);
}
}
public void setDestinationDataEventListener(DestinationDataEventListener eventListener) {
this.eL = eventListener;
}
public boolean supportsEvents() {
return true;
}
public void changeProperties(String destName, Properties properties) {
synchronized(secureDBStorage) {
if(properties==null) {
if(secureDBStorage.remove(destName)!=null) {
eL.deleted(destName);
}
} else {
secureDBStorage.put(destName, properties);
eL.updated(destName); // create or updated
}
}
}
}
public JCoDestination getDestination(String destName, Properties connectProperties) throws Exception {
MyDestinationDataProvider myProvider = MyDestinationDataProvider.getInstance();
boolean destinationDataProviderRegistered = com.sap.conn.jco.ext.Environment.isDestinationDataProviderRegistered();
if (!destinationDataProviderRegistered) {
try {
com.sap.conn.jco.ext.Environment.registerDestinationDataProvider(myProvider);
} catch(IllegalStateException providerAlreadyRegisteredException) {
throw new Error(providerAlreadyRegisteredException);
}
}
myProvider.changeProperties(destName, connectProperties);
JCoDestination dest = null;
try {
dest = JCoDestinationManager.getDestination(destName);
} catch(JCoException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
return dest;
}
}
After putting this code into the jar file application and creating the jar file (I call it JCOConnector.jar), I put the jar file on the shared library classpath of my tomcat server and restarted the tomcat server. In my case, this was /opt/tomcat/shared/lib. Check your /opt/tomcat/conf/catalina.properties file for the shared.loader line for the location of your shared library classpath. Mine looks like this:
shared.loader=\
${catalina.home}/shared/lib\*.jar,${catalina.home}/shared/lib
I also put a copy of this jar file in the "C:\Users\userid\Documents\jars" folder on my workstation so that the test application code could see the code in the jar and compile. I then referenced this copy of the jar file in my pom.xml file in both of my test applications:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>jcoconnector</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>C:\Users\userid\Documents\jars\JCOConnector.jar</systemPath>
</dependency>
After adding this to the pom.xml file, I right clicked on each project, selected Maven -> Update Project..., and I then right clicked again on each project and selected 'Refresh'. Something very important that I learned was to not add a copy of JCOConnector.jar directly to either of my test projects. The reason for this is because I want the code from the jar file in /opt/tomcat/shared/lib/JCOConnector.jar to be used. I then built and deployed each of my test apps to the tomcat server.
The code that calls my JCOConnector.jar shared library in my first test application looks like this:
CustomDestinationDataProvider cddp = new CustomDestinationDataProvider();
JCoDestination dest = null;
SAPDAO sapDAO = new SAPDAO();
Properties p1 = getProperties("SAPSystem01");
try {
dest = cddp.getDestination("SAP_R3_USERID_01", p1);
sapDAO.searchEmployees(dest);
} catch (Exception ex) {
ex.printStackTrace();
}
The code in my second test application that calls my JCOConnector.jar shared library looks like this:
CustomDestinationDataProvider cddp = new CustomDestinationDataProvider();
JCoDestination dest = null;
SAPDAO sapDAO = new SAPDAO();
Properties p2 = getProperties("SAPSystem02");
try {
dest = cddp.getDestination("SAP_R3_USERID_02", p2);
sapDAO.searchAvailability(dest);
} catch (Exception ex) {
ex.printStackTrace();
}
I know that I've left out a lot of the steps involved in first getting the SAP JCO 3 library installed on your workstation and server. I do hope that this helps out at least one other person of getting over the hill of trying to get multiple spring mvc java spplications talking to SAP on the same server.

Create a DbContext that handle a DatabaseFactory to use DapperExtensions more easily

This days I try to create an abstract base repository using some basic CRUD functions proposed by DapperExtensions. But the code given as an exemple use a SqlConnection which is made to connect to a SQL Server database. I want to be able to connect to all kind of Database (SQL Server, MySql, etc...). Also their code sample is repeated for each CRUD function as the code below show
using (SqlConnection cn = new SqlConnection(_connectionString))
{
cn.Open();
//Code doing something here...
cn.Close();
}
So i was thinking about to create a DbContext that can handle the creation, the opening and closing of the connection and also can create the right connection object depending on the database type I want to use (a kind of database factory).
Is there somebody that already done it and could share his code?
Thank you guys !
You are using Dapper-Extensions; following code is with Dapper only. But it does not change the entire concept. Just instead of sql you need to pass poco.
Refer this answer for how I implemented IUnitOfWork and DalSession. In below code, BaseDal is just like BaseRepository.
public abstract class BaseDal
{
internal BaseDal(IUnitOfWork unitOfWork)
{
dapperHandler = new DapperHandler(unitOfWork);
}
DapperHandler dapperHandler = null;
protected T Get<T>(string sql, DynamicParameters param) where T : class
{
var result = dapperHandler.Query<T>(sql, param).FirstOrDefault();
return result;
}
protected List<T> GetList<T>(string sql, DynamicParameters param) where T : class
{
var result = dapperHandler.Query<T>(sql, param).ToList();
return result;
}
protected int Insert(string sql, DynamicParameters param)
{
var result = dapperHandler.Execute(sql, param);
return result;
}
}
Edit 1
For example code with Dapper-Extensions, refer this answer that I recently posted.
public abstract class ABaseRepository<M> : IBaseRepository<M>
where M : BaseModel
{
private static DbProviderFactory factory = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ProviderName);
protected static DbConnection connection;
public static IDbConnection CreateOpenConnection()
{
connection = factory.CreateConnection();
connection.Open();
return connection;
}
public dynamic Insert(M model)
{
dynamic multiKey;
using (IDbConnection con = CreateOpenConnection())
{
multiKey = con.Insert(model);
}
return multiKey;
}
}

Unable to setup MiniProfiler w/ Enity Framework 4.0 (Not code first)

I installed MiniProfiler and MiniProfiler.EF in my project via nuget.
Before using MiniProfiler I would open a connection using this in my model repository:
public class NotificationRepository
{
private CBNotificationModel.CB_NotificationEntities db;
public NotificationRepository()
{
db = new CB_NotificationEntities();
}
public NotificationContact GetNotificationContacts()
{
return db.NotificationContacts.ToList();
}
}
To use mini profiler I created:
public static class ConnectionHelper
{
public static CB_NotificationEntities GetEntityConnection()
{
var conn = new StackExchange.Profiling.Data.EFProfiledDbConnection(GetConnection(), MiniProfiler.Current);
return ObjectContextUtils.CreateObjectContext<CB_NotificationEntities>(conn); // resides in the MiniProfiler.EF nuget pack
}
public static EntityConnection GetConnection()
{
return new EntityConnection(ConfigurationManager.ConnectionStrings["CB_NotificationEntities"].ConnectionString);
}
}
The model repository now uses
db = ConnectionHelper.GetEntityConnection();
However this gives the error:
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
Am I missing a step? I tried adding MiniProfilerEF.Initialize() and MiniProfilerEF.Initialize_EF42() in Application_start() however that just changes the errors given.
There does not seem to be much information for setting up a entity framework project to use miniprofiler unless it is codefirst.
I was able to get this working by changing my ConnectionHelper class to the following:
public static class ConnectionHelper
{
public static CB_NotificationEntities GetEntityConnection()
{
var connectionString = ConfigurationManager.ConnectionStrings["CB_NotificationEntities"].ConnectionString;
var ecsb = new EntityConnectionStringBuilder(connectionString);
var sqlConn = new SqlConnection(ecsb.ProviderConnectionString);
var pConn = new StackExchange.Profiling.Data.EFProfiledDbConnection(sqlConn, MiniProfiler.Current);
var context = ObjectContextUtils.CreateObjectContext<CB_NotificationEntities>(pConn);
return context;
}
}

Nhibernate with windows form

I have a asp.net application with Nihbernate setup, now I want to convert it to Windows form application.
Here is the code which have been setup in the Global.asax.cs. Can anyone give me some code sample how to do this in Windows form?
protected void Application_BeginRequest(object sender, EventArgs e)
{
ManagedWebSessionContext.Bind(HttpContext.Current, SessionManager.SessionFactory.OpenSession());
}
protected void Application_EndRequest(object sender, EventArgs e)
{
ISession session = ManagedWebSessionContext.Unbind(HttpContext.Current, SessionManager.SessionFactory);
if (session != null)
{
try
{
if (session.Transaction != null && session.Transaction.IsActive)
{
session.Transaction.Rollback();
}
else
{
session.Flush();
}
}
finally
{
session.Close();
}
}
}
Well, there are several methods for accessing ISessionFactory in statefull application (and a desktop application is that kind of application), among them:
Singleton
You could build the session factory once during the startup of your program and access it through a static singleton class.
This would force the application to use only one instance of the session factory.
example:
public sealed class NHibernateHelper
{
private static ISessionFactory SessionFactory;
private static readonly Configuration NhibernateConfig;
// ....
static NHibernateHelper()
{
NhibernateConfig = new Configuration().Configure();
SessionFactory = NhibernateConfig.BuildSessionFactory();
}
public static ISessionFactory GetSessionFactory()
{
return SessionFactory;
}
// ....
}
... and access the session factory through GetSessionFactory method all over the application.
Context Object and/or Dependency Injection
You could build the session factory from configuration and pass it through a context object all over the application.
example:
during startup:
// here you configure NHibernate.
ISessionFactory _sessionFactory = BuildMySessionFactory();
// ...
ObjectFactory.Initialize(x =>
{
x.For<IPatientRepository>()
.Use<StandardPatientRepository>()
.Ctor<ISessionFactory>().Is(_sessionFactory);
// ... initialize the rest of your repositories...
});
then:
public class StandardPatientRepository : IPatientRepository
{
private readonly ISessionFactory _sessionFactory;
public StandardPatientRepository(ISessionFactory sessionFactory)
{
if (sessionFactory == null)
throw new ArgumentNullException("sessionFactory");
_sessionFactory = sessionFactory;
}
public virtual Patient Get(Guid id)
{
using (IStatelessSession session =
_sessionFactory.OpenStatelessSession())
{
return session.Get<Patient>(id);
}
}
// the rest of data-access methods.
}
then in your classes that will make use of the data (ie. use the repositories) you will use:
Patient = ObjectFactory.GetInstance<IPatientRepository>().Get(patient);
In my opinion the second method is better as I think that singleton in most cases is an anti-pattern. The second approach will give you more control over your data layer, you will know who and when is accessing it.
Here is a well done and extensive sample application using NHibernate in a desktop application:
Building a Desktop To-Do Application with NHibernate
Managing the NHibernate session in a desktop application tends to be a lot more involved than managing the NHibernate session in a web application.

Resources