1

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
  • first of all SingletonSession is not Singleton if used in multi threaded situation Commented Mar 12, 2013 at 5:16

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

When you say "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." indicador already exits with id = 1 and has a value for property Nombre that is "This is the name of indicador 1" but I can't get it from inside the class (inside the class the value for Name is null for the same object in the same runtime). I need to get this value in order to perform some calculations when the objects is initialized.
Why you say that the object is not even bound to the database at that time?
@Overflow012 because you are in the constructor. Almost nothing has been done on your object, unless the instanciation has been done in a subclass which would have called the base constructor AFTER having done its own constructor processing, which is doubtfull. You may try to add logging in your property setter to see when setting happens (not sure it would work though, but worth a try).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.