Null and empty values property from inside class in Nhibernate

匆匆过客 提交于 2020-01-15 09:45:12

问题


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;
        }
    }
}

Here begin the problems

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); 
}
...

Output result:

Property from Inside:

Property from Outside: This is the name of indicador 1

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


回答1:


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

In

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 ?

In

 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



来源:https://stackoverflow.com/questions/15353789/null-and-empty-values-property-from-inside-class-in-nhibernate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!