6

I am using WPF with NHibernate currently using single session with mutiple thread that is causing so many error same as this link. So now i have to start and close the session as soon as i am done. But the problem will be be lazy data. I am confuse how will i fetch them with MVVM as below :-

class Product
{

public virtual string name{get;set;}
public virtual Session session{get;set;} // Lazy
public virtual Categories cate{get;set;} //Lazy
public virtual Warehose warehouse{get;set;} //Lazy

  public virtual string NAME
        {
            get { return Name; }
            set
            {
                Name = value;
                OnPropertyChanged("NAME");
            }
        }

 public virtual string TerminalName
        {
            get { return session.terminal.terminalName; }
            set
            {
                session.terminal.terminalName= value;
                OnPropertyChanged("TerminalName");
            }
        }

 public virtual string CateName
        {
            get { return session.cate.catename; }
            set
            {
                session.cate.catename= value;
                OnPropertyChanged("CateName");
            }
        }

 public virtual string UserName
        {
            get { return session.user.username; }
            set
            {
                session.user.username= value;
                OnPropertyChanged("UserName");
            }
        }

}

class Categories {
public virtual string catename{set;get;}
}

class Warehose {
public virtual string warename{set;get;}
}

class Session{
public virtual People user{get;set;} //Lazy
public virtual Terminal terminal{get;set;} //Lazy
//... other properties
} 

Class People{
public virtual string username{set;get;}
}

Class Terminal{
public virtual string terminalName{set;get;}
}

I will fetch the data from database in a list and giving it to DataGrid as itemsource as below

<DataGrid Name="saleDataGrid"
<DataGrid.Columns >
 <DataGridTextColumn Binding="{Binding NAME}" FontSize="12" 
  CanUserResize="False" Width="70" Header="Code"/>

 <DataGridTextColumn Binding="{Binding TerminalName}" FontSize="12" 
 Header="Desciption" Width="120" CanUserResize="False"/>

 <DataGridTextColumn Binding="{Binding CateName}" FontSize="12" 
  Header="Price" Width="50" CanUserResize="False"/>

 <DataGridTextColumn Binding="{Binding UserName}" FontSize="12" 
 CanUserResize="False" Width="45" Header="QTY"/>
</DataGrid.Columns>
</DataGrid>

So when i will close the connection and when DataGrid display that time i am getting lazy exception because session is closed. I don`t know what should i do. Can any one please recommend to get lazy data witth MVVM if session is closed. I don't want use eager loading as a solution for this.

Please let me know you need more information.

Thanks Anchit

Community
  • 1
  • 1
Anchit Pancholi
  • 1,174
  • 4
  • 14
  • 40

2 Answers2

0

For the most part, you have three options, and they are independent from ORM you use, because ORMs behave very similar:

  • let the session/context object live as long, as view model lives;
  • build model ("M" in MVVM) in the manner, which allows you to fetch data lazily, when you need them (with creation of new session/context object every time). This turns off ORM's lazy loading, since model is managing lazy loading by itself;
  • throw away lazy loading and load everything you need for the view model.
Dennis
  • 37,026
  • 10
  • 82
  • 150
  • 1
    in my current code i am working with only single session and I have used `Lock` on all the `DAO` but still getting `There was a problem converting an IDataReader to NDataReader` that is because some other thread is reading the data. So that's why i cant to start the new session as Unit of Work. i didnt understand your second point can you please explain more. – Anchit Pancholi Jun 19 '15 at 09:27
0

One way to deal with this is to set your session context status to 'thread_static' when you create your session factory,

If Fluent NHibernate

public class NHibernateConfig
{
    public static Configuration Configure()
    {
        var cfg = Fluently.Configure()
            .Database(...)
            .Mappings(...)
            .ExposeConfiguration(x => x.SetProperty("current_session_context_class", "thread_static"))
            .BuildConfiguration();
        return cfg;  
     }
}

If XML file

<props>
...
  <prop key="hibernate.current_session_context_class ">thread</prop>
...
</props>

which will keep the session per thread and you won't have to deal with the issues sharing same session across multiple threads.

However keeping a session open for a very long time is not a great idea, so may be when you initiate a new View, you could create a new session and close it when exiting the view, where it would not affect lazy loading.

Low Flying Pelican
  • 5,974
  • 1
  • 32
  • 43