I am using the Castle Windsor V3.2.1 in my WPF MVVM Application.
This is my Installer:
public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
{
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<IAbstructFactory>().AsFactory());
container
.Register(Component.For<IShell>().ImplementedBy<Shell>().LifestyleTransient())
.Register(Types
.FromAssemblyInDirectory(new AssemblyFilter(AssemblyDirectory + "\\Map"))
.Pick()
.If(x => x.IsPublic)
.If(x => x.GetInterfaces().Length > 0)
.WithService
.FirstInterface()
.LifestyleTransient())
.Register(Component.For<MainWindow>().LifestyleTransient());
}
NOTE: I am registering FromDirectory named Map.
This is my Map Project:
MapViewModel
public class MapViewModel : IMapViewModel
{
#region IMapViewModel Members
IMapView _theMapView;
IMapModel _theMapModel;
/// <summary>
/// Gets or sets the view.
/// </summary>
/// <value>
/// The view.
/// </value>
public IMapView TheView
{
get
{
return _theMapView;
}
set
{
_theMapView = value;
_theMapView.TheMapViewModel = this;
}
}
/// <summary>
/// Gets or sets the model.
/// </summary>
/// <value>
/// The model.
/// </value>
public IMapModel TheModel
{
get
{
return _theMapModel;
}
set
{
_theMapModel = value;
CreateView();
}
}
/// <summary>
/// Creates the view.
/// </summary>
private void CreateView()
{
TheView = new MapView();
}
#endregion
#region IViewModel Members
/// <summary>
/// Gets the help.
/// </summary>
/// <value>
/// The help.
/// </value>
public IHelpManager Help
{
get
{
return HelpManager.Instance;
}
}
#endregion
}
MapView
public partial class MapView : IMapView
{
private IMapViewModel _theMapViewModel;
/// <summary>
/// The View Model.
/// </summary>
public IMapViewModel TheMapViewModel
{
get
{
return _theMapViewModel;
}
set
{
_theMapViewModel = value;
DataContext = _theMapViewModel.TheModel;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="MapView"/> class.
/// </summary>
public MapView()
{
InitializeComponent();
}
}
This is my MainWindow Class:
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow"/> class.
/// </summary>
/// <param name="context">The context.</param>
public MainWindow(IAbstructFactory context)
{
InitializeComponent();
IMapViewModel theMapViewModel = context.Create<IMapViewModel>();
context.Release(theMapViewModel);
}
My Question Is:
I would like to think there is a better way of creating the view than I am currently using.
- I am creating the MapViewModel in the MainWindow.
- As you can see in the MapViewModel - When it is created, the MapModel is injected by the Windsor - which is working good.
When I tried to do the same with the MapView meaning - I am adding a call in the main like so:
IMapView theMapView = context.Create<IMapView>()
I am getting nothing and application is crashed.
Why isn't the view being injected as the ViewModel do though they are both in the same assembly ?