Question

I'm using Fluent NHibernate. I have a few applications that use the same database (mainly the application itself, and configuration and reporting tools for it, that are separate .exe) They share the same mapping now, defined fluently in common .dll. The big difference between them is that they use some parts of the database very differently. There are some tables that are read-only for one tool, but read-and-write for another. I would like to ensure that the tool would never write data to the "wrong" table.

My idea is to make each mapping for each tool a bit different. The general base of the mapping will be the same, but I could add extension methods that will check the name of the tool that is running the mapping configuration, and add several mapping specifications differently for each tool:

  • I want some of the tables be .ReadOnly() (mutable="false") for some of the tools
  • I want switch off/on cascades differently for each tool.

But I am a bit unsure, whether I think right. Any pitfalls on that? Any better way of achieving this?

Was it helpful?

Solution

Option 1

you could write a method returning all Mappings which alters them depending on the caller and add the mappings manually to the context

public IEnumerable<IMappingProvider> GetAllMappings(MappingMode mode)
{
    yield return new UserMap();

    var fooMap = new FooMap();
    if (mode == MappingMode.Reporting)
        fooMap.ReadOnly();
    yield return fooMap
}

var model = new PersistenceModel();
foreach (var mapping in GetAllMappings(mappingMode)
{
    model.Add(mapping);
}

Fluently.Configure().Mappings(m => m.UsePeristenceModel(model))

Option 2

add custom conventions depending on the app which alter the mapping

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top