views:

30

answers:

2

Hi All

Is it possible to configure the autopersistence model to read mapping and mapping overrides from many assemblies.

At the moment I use

public AutoPersistenceModel Generate()
        {
            var model =
                new AutoPersistenceModel()
                    .AddEntityAssembly(Assembly.GetAssembly(typeof(User)))
                    .Where(
                    this.GetAutoMappingFilter).Conventions.Setup(this.GetConventions()).Setup(this.GetSetup()).

                    UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();

            return model;
        }

        public AutoPersistenceModel Generate(params Assembly[] assemblies)
        {
            var model = this.Generate();
            assemblies.ToList().ForEach(x => model.AddEntityAssembly(x));

            return model;
        }

The problem is with

  UseOverridesFromAssemblyOf<AutoPersistenceModelGenerator>();

which takes only one assembly. BUt I would like to collect mapping and their otherrides from different assemblies.

Thanks

A: 

Did you find the solution to this problem? I'm having the same exact one.

Łukasz Podolak
A: 

So the problem with .UseOverridesFromAssemblyOf is that they do not duplicate type.

Code snipped from FluentNHibernate:

public AutoPersistenceModel UseOverridesFromAssembly(Assembly assembly)
    {
      this.alterations.Add(new AutoMappingOverrideAlteration(assembly));
      return this;
    }
public AutoMappingAlterationCollection Add(IAutoMappingAlteration alteration)
    {
      if (!this.alterations.Exists(a => a.GetType() == alteration.GetType()))
      {
        this.alterations.Add(alteration);
      }
      return this;
    }

You can see that adding duplicate type is not allowed.

After investigation FNH code I found that they finally alter the AutoMappingOverrideAlteration and I just write an extension to apply this altering

 public static class AutoPersistenceModelExtension
    {
        public static AutoPersistenceModel AddMappingAssembly(this AutoPersistenceModel model, Assembly assembly)
        {
            new AutoMappingOverrideAlteration(assembly).Alter(model);
            return model;
        }

        public static AutoPersistenceModel AddMappingFromAssemblyOf<T>(this AutoPersistenceModel model)
        {
            return model.AddEntityAssembly(typeof (T).Assembly);
        }


    }

The result is: Our application is build based on components, each components provide own Entities and Mappings for those entities. Each component should be derived from a BaseComponent class which has an abstract method to Alter the model.

Ex of FulfillmentModuleRegistration:

public override void AddToModel(AutoPersistenceModel autoPersistenceModel)
        {
            autoPersistenceModel.AddEntityAssembly(typeof(SupplierListItem).Assembly);
            autoPersistenceModel.AddMappingFromAssemblyOf<SupplierListItemMappingOverride>();
        }

and our global.asax.cs:

 var autoPersistenceModel = new AutoPersistenceModelGenerator().Generate();
            //TODO: Refactor this, instead of instantiate each module we have to go thought a list of loaded module and apply their model alteration
            new QuestionBankModuleRegistration().AddToModel(autoPersistenceModel);
            new FulfilmentServiceAreaRegistration().AddToModel(autoPersistenceModel);

            var cfg = NHibernateSession.Init(
                this.webSessionStorage,
                new[] { this.Server.MapPath("~/bin/baseMappings.dll") },
                autoPersistenceModel,
                this.Server.MapPath("~/Hibernate.cfg.xml"));

baseMappings.dll contains our base entities like : Users, Lookups, etc. So it could be any assembly which contains base, default mappings, conventions, etc.

isuruceanu

related questions