Wombat
Wombat

Reputation: 172

Using loquacious class mappings in NHibernate

I am trying to use the new loquacious class mapping feature that we used to get out of Fluent NHibernate. But I am getting stuck on how to get the class mappings to the configuration. I have read F Maulo's blog ( http://fabiomaulo.blogspot.com/2011/04/me-on-fluent-nhibernate.html ) on it but I can't seem to get it to work. Do any of you have any information on how to add this to the NH configuration?

Regards,

Found the answer based on the reaction

  foreach (string mappingAssembly in MappingsAssembly.Split(','))
        {
            if (string.IsNullOrEmpty(mappingAssembly)) continue;
            Assembly assembly = Assembly.Load(mappingAssembly);
            types.AddRange(assembly.GetTypes());
            //  configuration.AddAssembly(assembly);
        }
        configuration.DataBaseIntegration(x => GetDatabaseConfig(x));

        ModelMapper mapper = new ModelMapper();

        mapper.AddMappings(types);
        var compiledMapping = mapper.CompileMappingForAllExplicitAddedEntities();

        configuration.AddMapping(compiledMapping);

Upvotes: 3

Views: 3582

Answers (2)

Fran
Fran

Reputation: 6530

If you are already doing Fluent, the easiest would be to keep you separate mapping files and load them all in at startup. So I have an initialization class that defines a class level variable

    private ModelMapper _mapper = new ModelMapper();

then I call Initialize()

    public void Initialize()
    {
        Configure = new Configuration();
        Configure.SessionFactoryName(System.Configuration.ConfigurationManager.AppSettings["SessionFactoryName"]);
        Configure.DataBaseIntegration(db =>
                                      {
                                        db.Dialect<MsSql2008Dialect>();
                                        db.Driver<SqlClientDriver>();
                                        db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
                                        db.IsolationLevel = IsolationLevel.ReadCommitted;
                                        db.ConnectionStringName = ConnectionStringName;
                                        db.BatchSize = 20;
                                        db.Timeout = 10;
                                        db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'";
                                      });
        Configure.SessionFactory().GenerateStatistics();

        Map();
    }

then my Map() method is this

    private void Map()
    {
        _mapper.AddMappings(MappingAssembly.GetExportedTypes());
        Configure.AddDeserializedMapping(_mapper.CompileMappingForAllExplicitlyAddedEntities(), "MyWholeDomain"); 
    }

I like to pass my mapping assembly name in through my appSettings.

Upvotes: 1

Iulian Margarintescu
Iulian Margarintescu

Reputation: 2666

You can try something along these lines:

ModelMapper mapper = new ModelMapper();
// your mappings by code
Type[] types = new Type[] {}; // your mapped types here
// compile mappings
HbmMapping mapping = mapper.CompileMappingFor(types);
// create configuration
Configuration cfg = new Configuration();
// add mappings to config
cfg.AddDeserializedMapping(hbm,"documentname");

Just remember that doing the mappings by code gives you quite a few new possibilities: eg: you can get the list of mapped types with Assembly.GetTypes().

Upvotes: 1

Related Questions