NHibernate 2.0: problème Cfg.Configuration.SetProperties avec IDictionary
-
20-08-2019 - |
Question
J'ai un problème avec:
NHibernate.Cfg.Configuration.SetProperties()
N'accepte pas l'IDictionary: NHibernateConfigHandler
Je reçois les messages:
Erreur 30 La meilleure correspondance de méthode surchargée pour 'NHibernate.Cfg.Configuration.SetProperties (System.Collections.Generic.IDictionary)' contient des arguments non valides
et
Erreur 31 Argument '1': impossible de convertir de 'System.Collections.IDictionary' en 'System.Collections.Generic.IDictionary'
S'il vous plaît aviser?
La méthode entière:
/// <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;
}
La solution
La signature de SetProperties est
public NHibernate.Cfg.Configuration SetProperties(System.Collections.Generic.IDictionary<string,string> newProperties)
Member of NHibernate.Cfg.Configuration
Setproperties prend un paramètre de type IDictionary, i.e Generic Dictionary. Et vous essayez de passer en IDictionary. C’est la raison de l’erreur.
Vous pouvez également changer le type de propriétés dans NHibernateConfigHandler en System.Collections.Generic.IDictionary. OU Créez un System.Collections.Generic.IDictionary et copiez toutes les valeurs à partir de System.Collections.IDictionary.
Il est toujours efficace d'utiliser un dictionnaire générique (qui se trouve sur System.Collections.Generic) plutôt que sur IDictionary (qui se trouve sur System.Collections).