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

2
  • 1
    I'd avoid word "best", because this leads question to close as opinion-based.
    – Dennis
    Jun 19, 2015 at 9:04
  • removed best :) i just want to know how to solve this problem Jun 19, 2015 at 9:06

2 Answers 2

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.
1
  • 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. Jun 19, 2015 at 9: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.