ConfORM- Getting started

2 Set

ConfORM is a just another option to create the mapping for NHibernate. Out-of-the-box NHibernate mapping’s can be created only with XML mapping files (*.hbb.xml files). ConfORM uses code instead of XML. If you are thinking that there is already at least one tool that follow this approach you should read this post to understand which are the differences between the other tools and ConfORM.
After this brief introduction I want to show you a little example of ConfORM’s usage to demonstrate how easy could be life with this tool.  
Here below I posted a screenshot of my domain model. As you can see there is nothing special here: some Collections and some ManyToOne relationships. There is also a read-only property: Element.HasGroupedAnswer (sorry you can’t see it in the screenshot below) .

image

The very first step is to create a valid configuration object for NHibernate. Using NHibernate.Cfg.Loquacious feature from NHibernate version 3 this can be done with this lines of code.

            Configuration configure = new Configuration();
            configure.SessionFactoryName("Demo");
            configure.Proxy(p =>
            {
                p.Validation = false;
                p.ProxyFactoryFactory<ProxyFactoryFactory>();
            });
            configure.Properties["current_session_context_class"] = _context;
            configure.DataBaseIntegration(db =>
            {
                db.Dialect<MsSql2008Dialect>();
                db.Driver<SqlClientDriver>();
                db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
                db.IsolationLevel = IsolationLevel.ReadCommitted;
                db.LogFormatedSql = LogSqlInConsole;
                db.LogSqlInConsole = LogSqlInConsole;                
                db.ConnectionString = "yuor connection string goes here";
                db.Timeout = 10;
                db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'";
            });
 

Once configuration object is ready we must setup mapping for our domain. Now it’s time to use ConfORM. I decided to wrap all mapping stuff inside the class shown below.

    public class ConfOrmDomainMapper
    {
        public ConfOrmDomainMapper()
        {
            orm = new ObjectRelationalMapper();         
            orm.Patterns.PoidStrategies.Add(new NativePoidPattern());
            var patternsAppliers = new CoolPatternsAppliersHolder(orm);

            mapper = new Mapper(orm, patternsAppliers);

            DefineDomain();
            DefineMapping();
        }

        private void DefineMapping()
        {           
        }

        private void DefineDomain()
        {
            orm.TablePerClass(GetTypes());
        }

        public HbmMapping Mapping
        {
            get { return mapper.CompileMappingFor(GetTypes()); }
        }

        IEnumerable<Type> GetTypes()
        {
            var type = typeof(BaseEntity);
            return type.Assembly.GetTypes().Where(t => t.BaseType == type);
        }
        private readonly ObjectRelationalMapper orm;
        private readonly Mapper mapper;
    }

Since all the classes in my domain model inherit from a common class called BaseEntity I can use reflection to get all types with a single line of code. This is not mandatory for ConfORM but as you can the result is very nice. In this simple sample I don’t need to customize the mapper but if you need to do you can use the Mapper class instance. Now all the mapping is already done. Since I used CoolPatternsAppliersHolder ConfORM already create a complete mapping to start working with NHibernate (I’ll blog some details about it in the future)

To finish I need to join ConfORM mappings and NHibernate configuration. This task is done with this few lines of code:

ConfOrmDomainMapper domainMapper = new ConfOrmDomainMapper();
configure.AddDeserializedMapping(domainMapper.Mapping, "MyAppDomain");

After this I’m ready to create a SessionFactory with

configure.BuildSessionFactory();

and start using NHibernate a usual.

This was just the beginning. I hope to find some more time to write again about ConfORM.

I want also thank Fabio Maulo for sharing with community this another piece of great code!

7 Risposte to “ConfORM- Getting started”

  1. Luca Milan settembre 2, 2010 a 11:18 pm #

    Great! One question, ConfORM map components / value objects (even collection) inside my entity ? thx

    • makka settembre 3, 2010 a 7:16 am #

      Yes. Please wait some days and I’ll post about it

  2. Fabio Maulo settembre 3, 2010 a 5:09 am #

    configuration.CurrentSessionContext
    Thanks.😉

    • makka settembre 3, 2010 a 7:15 am #

      I can’t find a CurrentSessionContext property on NHibernate.Cfg.Configuration class

      I just found this alternatives
      configuration.Properties[Environment.CurrentSessionContextClass] = _context;
      configuration.SetProperty(Environment.CurrentSessionContextClass, _context);

      Am I wrong ?

      • Fabio Maulo settembre 3, 2010 a 3:03 pm #

        No you don’t. Was commited few hours ago😉

  3. Fabio Maulo settembre 3, 2010 a 5:11 am #

    WOW!!!
    The whole domain in just one line ?
    orm.TablePerClass(GetTypes());

    you are a lucky man!!

    Are you ConfORM ?

Trackbacks/Pingbacks

  1. ConfORM: understand core pieces « TDD developer - settembre 6, 2010

    […] core pieces Posted: September 6, 2010 by makka in Uncategorized 0 If you read the post ConfORM Gettins started you already know that NHibernate mapping task could be very easy with ConfORM. But unfortunately […]

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger cliccano Mi Piace per questo: