title: MVVM - Using entities in ViewModels
body: <p>I am really just starting out with MVVM, IoC and Dependency Injection and I've hit a stumbling block that I don't know how to solve but I do understand why it's happening.</p>
  
  <p>I am using Castle Windsor for DI and IoC functionality and MVVM Light as my MVVM framework in a WPF application.  Using <a href="http://fir3pho3nixx.blogspot.co.uk/2011/08/silverlight-mvvm-light-toolkit-and.html" rel="nofollow noreferrer">this tutorial</a> I have managed to get Castle Windsor to create a <code>MainPageViewModel</code> that has a <code>IGroupRepository</code> injected into the constructor.  I have registered a mock implementation of this in Castle Windsor.</p>
  
  <p>The following is the only other code in the <code>MainPageViewModel</code> class besides the constructor.</p>
  
  <pre><code>public ObservableCollection&lt;GroupViewModel&gt; Groups
  {
      get
      {
          var groupVms = new ObservableCollection&lt;GroupViewModel&gt;();
          IEnumerable&lt;Group&gt; groups = _repository.GetAllGroups();
          foreach (Group g in groups)
          {
              var vm = new GroupViewModel(g);
              groupVms.Add(vm);
          }
  
          return groupVms;
      }
  }
  </code></pre>
  
  <p>The intention is to create a view model for each of the groups in the repository.  However, doing this causes Castle Windsor to give the following exception:</p>
  
  <blockquote>
    <p>Can't create component 'Planner.ViewModel.GroupViewModel' as it has dependencies 
        to be satisfied.
    'Planner.ViewModel.GroupViewModel' is waiting for the following dependencies:</p>
    
    <ul>
    <li>Service 'Planner.Models.Group' which was not registered.</li>
    </ul>
  </blockquote>
  
  <p>I understand this exception - Castle Windsor is responsible for constructing my view models but it has no way of handling my entity.</p>
  
  <p>I have done plenty of Googling but have found very few answers or suggestions to this problem which makes me think that what I am doing is wrong.  <a href="https://stackoverflow.com/questions/2720329/should-i-use-dtos-as-my-data-models-in-mvvm">This Stack Overflow question</a> has two answers which suggest that having an entity on the view model is ok but I'm beginning to wonder if that's true.  Other questions, <a href="https://stackoverflow.com/questions/4147250/ef4-mvvm-expose-entities-in-viewmodel">such as this one</a> suggest that the entity should be nowhere near the view model.</p>
  
  <p>What is the correct way to resolve this problem?</p>
  
  <p><strong>Update:</strong> As requested, this is the stacktrace for the exception:</p>
  
  <pre><code>at Castle.MicroKernel.Handlers.DefaultHandler.AssertNotWaitingForDependency()
  at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden&amp; burden)
  at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
  at Castle.MicroKernel.Handlers.AbstractHandler.Resolve(CreationContext context)
  at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
  at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
  at Castle.MicroKernel.DefaultKernel.Resolve(Type service, IDictionary arguments)
  at Castle.Windsor.WindsorContainer.Resolve(Type service)
  at Planner.ViewModel.ViewModelResolver.Resolve(String viewModelName) in D:\Planner\Planner\Planner\ViewModel\ViewModelResolver.cs:line 27
  at Planner.ViewModel.ViewModelLocator.get_Item(String viewModelName) in D:\Planner\Planner\Planner\ViewModel\ViewModelLocator.cs:line 21
  </code></pre>
  
  <p>I thought that this was the correct behaviour because of the following code which (I believe) intercepts any calls to the constructor of a view model and injects them as appropriate.</p>
  
  <pre><code>public class ViewModelResolver : IViewModelResolver
  {
      private IWindsorContainer _container;
  
      public object Resolve(string viewModelName)
      {
          if (_container == null)
          {
              _container = new WindsorContainer();
              _container.Install(new WindsorViewsInstaller());
              _container.Install(new WindsorRepositoriesInstaller());
          }
  
          var viewModelType =
              GetType()
              .Assembly
              .GetTypes()
              .Where(t =&gt; t.Name.Equals(viewModelName))
              .FirstOrDefault();
  
          return _container.Resolve(viewModelType);
      }
  }
  </code></pre>
  
  <p><strong>Update 2:</strong> I think this answers Ritch's query:</p>
  
  <pre><code>public class ViewModelLocator : DynamicObject
  {
      public IViewModelResolver Resolver { get; set; }
  
      public ViewModelLocator()
      {
          Resolver = new ViewModelResolver();
      }
  
      public object this[string viewModelName]
      {
          get
          {
              return Resolver.Resolve(viewModelName);
          }
      }
  
      public override bool TryGetMember(GetMemberBinder binder, out object result)
      {
          result = this[binder.Name];
          return true;
      }
  }
  </code></pre>
  
  <p>I think I understand this a little more now.  The problem isn't actually with the original code I posted.  The problem is occurring actually setting up Windsor isn't it?  I'm still not sure how I solve that problem though.</p>