开发者

Using loquacious class mappings in NHibernate

开发者 https://www.devze.com 2023-03-17 15:55 出处:网络
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 Ma

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.DataBas开发者_如何学GoeIntegration(x => GetDatabaseConfig(x));

        ModelMapper mapper = new ModelMapper();

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

        configuration.AddMapping(compiledMapping);


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().


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消