-
Create DB from Classes and Mappings with NHibernate-Domain Driven Design
-
Here I post a simple class for create db schema from mappings and classes.This is the right method for develop real Domani Driven Design.You don't must project DB it will be create by NHibernate:
using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
using System.Threading;
using System.Configuration;
using NHibernateConfig.Common.Configuration;
using NHibernate.Tool.hbm2ddl;
namespace Gestio.DataLayer.Nhibernate.Provider
{
public sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernate.current_session";
private static readonly ISessionFactory sessionFactory;
private static NHibernate.Cfg.Configuration cfg;
static NHibernateHelper()
{
if(sessionFactory==null){
cfg = new NHibernate.Cfg.Configuration();
cfg.AddAssembly("Gestio.NHibernate.Mappings");
try
{
sessionFactory = cfg.BuildSessionFactory();
}
catch (Exception e)
{
throw new Exception(" Can not create session factory! " + e.Message, e.InnerException);
}
}
}
public static ISession GetCurrentSession()
{
ISession currentSession=sessionFactory.OpenSession();
return currentSession;
}
public static void CloseSessionHelper()
{
sessionFactory.Close() ;
}
public static void CreateDB()
{
NHibernate.Tool.hbm2ddl.SchemaExport obj=new SchemaExport(cfg,null);
System.Data.IDbConnection dbconn = new System.Data.SqlClient.SqlConnection("Data Source=ciuccio;Initial Catalog=test;User Id=sa;Password=pippo");
dbconn.Open();
obj.Execute(true,true,false,false,dbconn,null);
dbconn.Close();
}
}
}
With static method "CreateDB" you can create your db schema.Of course you must use a valid connection and if you want you can pass to method Execute a TextWriter object for save generated DDL.It is a great feature from hbm2ddl library. Of couse your mappings must be valid and without errors ;-)
Bye
Antonio
-
Resources about DDD
-
This is a nice list of really interesting resources about Domain Driven Design...
Bye
Antonio
-
My solution to pagination and count criteria in SQL Server
-
This is a trick about pagination and counting verified criteria with SQL Server features...
Bye
Antonio