我从使用 nHibernate 的类映射中获取空值,请参见下文:
public class IndicadorRepository : Repository<IndicadorRepository>
{
...
public Indicador FindById(int indicadorId)
{
return _session.Get<Indicador>(indicadorId);
}
...
}
存储库.cs
public class Repository<T> where T : Repository<T>, new()
{
/* Properties */
protected static T instance;
public ISession _session;
public static T Instance
{
get
{
if (instance == null) instance = new T();
return instance;
}
}
/* Constructor */
protected Repository()
{
this._session = SingletonSession.Session;
}
}
SingletonSession.cs
class SingletonSession
{
protected static ISession _session;
public static ISession Session
{
get
{
if (_session == null)
{
try
{
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Objetivo).Assembly);
var schema = new SchemaUpdate(cfg);
schema.Execute(true, true);
// Get ourselves an NHibernate Session
var sessions = cfg.BuildSessionFactory();
_session = sessions.OpenSession();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
return _session;
}
}
}
从这里开始问题
Indicador.cs这个类是用 nhibernate 映射的。
public class Indicador : Modelo<Indicador>
{
public virtual string Nombre { get; set;}
/************* Constructor *************/
public Indicador()
{
// Pay attention to line below
Console.WriteLine("Property from Inside: " + Nombre);
}
}
SomeForm.cs
...
private void ConfigurarIndicadoresDataGrid()
{
// Pay attention to line below
Console.WriteLine("Property from Outside: {0}", IndicadorRepository.Instance.FindById(1).Nombre);
}
...
输出结果:
Property from Inside:
Property from Outside: This is the name of indicador 1
为什么类内的属性值为Indicador
null,而类外的属性值是加载的?我究竟做错了什么?