Adam W. McKinley
Adam W. McKinley

Reputation: 775

Blendability / MVVM for NHibernate

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:

  1. Is there an easy way to implement an in-memory ISessionFactory?
  2. Should I try to use an in-memory SQLite database?
  3. Am I taking the wrong approach to the whole thing?

Upvotes: 1

Views: 853

Answers (1)

Marcote
Marcote

Reputation: 3095

The view shouldn't know anything about NH. Is not it's responsability. NH Session should be handled at Service level or Repository Level. There are some many variables and approches and truth is this: there is no silver bullet. All the depends of what the View is doing.

I suggest looking at some examples in unNHAddins (http://code.google.com/p/unhaddins/) dealing with NH and WPF.

I added more information in this answer yesterday: NHibernate in C# application, how to manage session

Good luck!

Upvotes: 2

Related Questions