Ayende @ Rahien

Unnatural acts on source code

Pluggable Domain Model

Rhino Security is a good example of a pluggable domain model, in which we can plug some functionality into different and varied domains.

Here is an interesting demo to show how you can use it.

public static void DemoUsingCustomerCareModule<TCustomer>(string schema)
    where TCustomer : ICustomer, new()
{
    Configuration cfg = new Configuration()
        .Configure("hibernate.cfg.xml")
        .AddAssembly(typeof(Lead).Assembly)
        .AddAssembly(typeof(TCustomer).Assembly);

    cfg.MapManyToOne<ICustomer, TCustomer>();
    cfg.SetSchema(schema);

    new SchemaExport(cfg).Execute(true, true, false, true);

    ISessionFactory factory = cfg.BuildSessionFactory();

    object accountCustomerId;
    using (var session = factory.OpenSession())
    using (var tx = session.BeginTransaction())
    {
        var customer = new TCustomer { Name = "ayende"};
        session.Save(customer);
        var customerCareService = new CustomerCareService(session);
        customerCareService.GenerateLeadFor(customer, "phone call");
        customerCareService.GenerateLeadFor(customer, "email ");
        tx.Commit();

        accountCustomerId = session.GetIdentifier(customer);
    }

    using (var session = factory.OpenSession())
    using (var tx = session.BeginTransaction())
    {
        var customer = session.Get<TCustomer>(accountCustomerId);
        var customerCareService = new CustomerCareService(session);

        Console.WriteLine("Leads for: " + customer.Name);
        foreach (var lead in customerCareService.GetLeadsFor(customer))
        {
            Console.WriteLine("\t" + lead.Note);
        }

        tx.Commit();
    }
}

This can be used with:

DemoUsingCustomerCareModule<AccountingCustomer>("Accounting");

DemoUsingCustomerCareModule<HelpDeskCustomer>("HelpDesk");

Comments

Martin
05/06/2008 02:51 PM by
Martin

Ayende,

where are MapManyToOne and SetSchema? Looked both in NHibernate and Rhino Security but with no success.

Ayende Rahien
05/06/2008 03:51 PM by
Ayende Rahien

Martin,

It is not public yet, but you can check my post about dynamic mapping

Code Beast
05/07/2008 07:43 AM by
Code Beast

var, var, var hell is there ;)

Comments have been closed on this topic.