Pluggable Domain Model

time to read 2 min | 390 words

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");