March 2008 - Posts

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 

Posted 09 March 2008 04:08 PM by antrad | no comments
Filed under:
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

Some interesting posts about CSS

These 2 really interesting posts about css...

http://www.smashingmagazine.com/2008/02/21/powerful-css-techniques-for-effective-coding/

 

http://www.smashingmagazine.com/2007/01/19/53-css-techniques-you-couldnt-live-without/

 

Bye

Posted 04 March 2008 12:49 PM by antrad | 1 comment(s)
Filed under:

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP
  • Breaking News
  • Find a Job
  • Social Bookmarking
    Tidebuy Reviews
    Online Shopping
    asp.net hosting
    UK online local dating