Try fast search NHibernate

06 September 2010

ConfORM: Usage Examples

I have promised myself to no post often about ConfORM, instead I have started a new project, available with ConfORM’s sources, where I’ll show some usage examples with some explication.

So far there are few examples created under users requests. If you want see the code without download, it is available here.

Taking the opportunity of this post I would show you another success story:
It is about an “Inventory management and stock control” system composed by 65 entities/components where the persistence is managed using NHibernate 2.1.2. Because ConfORM uses a NHibernate 3 specific feature they can’t use ConfORM directly in the application, instead they are using ConfORM to generate XML mappings (soon, probably, they will use it directly with NHibernate 3). They can trust in ConfORM because they have, so far, 83 persistence integration-tests testing CRUD and expected cascades operations.

I can’t show you the domain-model but I can show you the whole mapping:

IEnumerable<Type> domainEntities = typeof (BaseEntity)
    .Assembly.GetTypes()
    .Where(t => typeof (BaseEntity).IsAssignableFrom(t) && !t.IsGenericType)
    .ToList();

var tablePerClassHierarchy = new[] {typeof (Movement), typeof (MovementDetail)};
IEnumerable<Type> tablePerClassEntities = typeof (BaseEntity)
    .Assembly.GetTypes()
    .Where(t => typeof (BaseEntity) == t.BaseType && !tablePerClassHierarchy.Contains(t))
    .ToList();

var orm = new ObjectRelationalMapper();

orm.TablePerClass(tablePerClassEntities);
orm.TablePerClassHierarchy(tablePerClassHierarchy);

orm.Exclude(typeof (Movement<>));
orm.Exclude(typeof (MovementDetail<>));

orm.ManyToMany<Product, Tag>();

var patternsAppliers =
    (new SafePropertyAccessorPack())
        .Merge(new OneToOneRelationPack(orm))
        .Merge(new BidirectionalManyToManyRelationPack(orm))
        .Merge(new BidirectionalOneToManyRelationPack(orm))
        .Merge(new DiscriminatorValueAsClassNamePack(orm))
        .Merge(new CoolTablesAndColumnsNamingPack(orm))
        .Merge(new TablePerClassPack())
        .Merge(new DatePropertyByNameApplier())
        .Merge(new MsSQL2008DateTimeApplier());

var mapper = new Mapper(orm, patternsAppliers);

mapper.Customize<Product>(
    cm =>
        {
            cm.Property(product => product.Code, pm =>
                                                     {
                                                         pm.NotNullable(true);
                                                         pm.Unique(true);
                                                     });
            cm.Collection(product => product.Tags, tagsm => tagsm.Cascade(Cascade.Persist));
        });

mapper.Customize<Role>(
    cm => cm.Collection(role => role.Functionalities, com => com.Cascade(Cascade.Persist)));

mapper.Customize<User>(cm => cm.Property(user => user.Nick, n => n.Unique(true)));

In this mapping you can see how compose ConfOrm.Shop.Packs and how map a whole domain in few lines ;-)

Are you ConfORM ?
They are!!!