Nhibernate中类内部的Null和Empty值属性 [英] Null and empty values property from inside class in Nhibernate

查看:92
本文介绍了Nhibernate中类内部的Null和Empty值属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用nHibernate从类内部映射中获取了空值,请参见以下内容:

I get null values from inside class mapping with nHibernate, please see below:

public class IndicadorRepository : Repository<IndicadorRepository>
{
    ...
    public Indicador FindById(int indicadorId)
    {
        return _session.Get<Indicador>(indicadorId);
    }
    ...
}

Repository.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映射.

Indicador.cs this class is mapped with 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,而为什么在类外部进行加载?我在做什么错了?

Why the property values inside the class Indicador are null and outside the class are loaded? What am I doing wrong?

推荐答案

也许我误解了您的问题,但这似乎只是一个计时问题.

Maybe I misinterpreted your question, but it just seems like a timing issue.

Console.WriteLine("Property from Inside: " + Nombre);

您正在尝试访问和显示构造函数中的属性值,该对象当时没有绑定到数据库.为什么要为此属性指定一个特定的值?

you are trying to access and display a property value in the constructor, for an object that is not even bound to the database at that time. Why would you want to have a specific value for this property ?

 Console.WriteLine("Property from Outside: {0}", IndicadorRepository.Instance.FindById(1).Nombre); 

您正在显示刚从数据库中加载的对象的值. (希望)有一个值

you are displaying the value of an object which has just been loaded from the database. It (hopefully) has a value

这篇关于Nhibernate中类内部的Null和Empty值属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆