I am currently working on a WPF application using NHibernate as my persistence layer. I am trying to use MVVM, partially so that I can use Blend to help design my controls.
I am trying to follow Ayende Rahien's example in Effectus of having one ISession per presenter (except in my case it's a view model instead of a presenter). My view model looks something like this:
public abstract ViewModelBase : INotifyPropertyChanged
{
private readonly ISessionFactory _sessionFactory;
protected ViewModelBase(ISessionFactory sessionFactory)
{
_sessionFactory = sessionFactory;
}
private ISession Session
{
get
{
if (_session == null)
{
_session = _sessionFactory.OpenSession();
}
return _session;
}
}
private ISession _session;
// INotifyPropertyChanged implementation here...
}
public class MainWindowViewModel : ViewModelBase
{
public MainWindowViewModel(ISessionFactory sessionFactory)
: base(sessionFactory)
{
var rep = new ProductRepository(this.Session);
this.Products = new ObservableCollection<Product>(rep.GetAll());
}
public ObservableCollection<Product> Products
{
get
{
return _products;
}
set
{
if (_products != value)
{
_products = value;
RaisePropertyChanged("Products");
}
}
}
private ObservableCollection<Product> _products;
}
public class DesignMainWindowViewModel : MainWindowViewModel
{
public DesignMainWindowViewModel(ISessionFactory sessionFactory)
: base(sessionFactory)
{
}
public new ObservableCollection<Product> Products
{
get
{
List<Product> products = new List<Product>();
// fill in design-time products...
return products;
}
set
{
}
}
}
What I would like to achieve is to have a ViewModelLocator that works with Unity to instantiate a design-time view model while working in Blend (and the regular view model when running the software normally). However, I need a design-time ISessionFactory in order to instantiate the DesignMainWindowViewModel.
Three questions:
- Is there an easy way to implement an in-memory ISessionFactory?
- Should I try to use an in-memory SQLite database?
- Am I taking the wrong approach to the whole thing?