NHibernate 2.0: Cfg.Configuration.SetProperties problema com IDictionary
-
20-08-2019 - |
Pergunta
Eu tenho um problema com:
NHibernate.Cfg.Configuration.SetProperties()
Não aceitar o IDictionary: NHibernateConfigHandler
Eu recebo as mensagens:
Erro 30 O melhor jogo método sobrecarregado para 'NHibernate.Cfg.Configuration.SetProperties (System.Collections.Generic.IDictionary)' tem alguns argumentos inválidos
e
Erro 31 Argumento '1': não podem converter de 'System.Collections.IDictionary' para 'System.Collections.Generic.IDictionary'
Por favor, informe?
O todo Método:
/// <param name="config">NHibernate configuration</param>
public ISessionFactory GetSessionFactoryFor(NHibernateConfigHandler config)
{
if (config == null)
throw new ArgumentNullException("config may not be null nor empty");
ISessionFactory sessionFactory = GetSessionFactoryFor(config.MappingAssembly);
// Failed to find a cached SessionFactory so make a new one.
if (sessionFactory == null)
{
NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
cfg.SetProperties(config.Properties); //THIS LINE
cfg.AddAssembly(config.MappingAssembly);
// Now that we have our Configuration object, create a new SessionFactory
sessionFactory = cfg.BuildSessionFactory();
if (sessionFactory == null)
{
throw new InvalidOperationException("cfg.BuildSessionFactory() returned null.");
}
HttpRuntime.Cache.Add(config.MappingAssembly, sessionFactory, null, DateTime.Now.AddDays(7),
TimeSpan.Zero, CacheItemPriority.High, null);
}
return sessionFactory;
}
Solução
Assinatura do SetProperties é
public NHibernate.Cfg.Configuration SetProperties(System.Collections.Generic.IDictionary<string,string> newProperties)
Member of NHibernate.Cfg.Configuration
SetProperties está tomando parâmetro do tipo IDictionary, ou seja dicionário genérico. E você tentando passar em IDictionary. Isso é a razão para o erro.
Ou você pode mudar o tipo de propriedades em NHibernateConfigHandler para System.Collections.Generic.IDictionary. OU Criar um System.Collections.Generic.IDictionary e copiar todos os valores de System.Collections.IDictionary.
É sempre eficiente usar dicionário genérico (que está em System.Collections.Generic) sobre IDictionary (que está em System.Collections).