diff --git a/Samples/Stylet.Samples.OverridingViewManager/CustomViewManager.cs b/Samples/Stylet.Samples.OverridingViewManager/CustomViewManager.cs index 2e13cfb..e0b64ec 100644 --- a/Samples/Stylet.Samples.OverridingViewManager/CustomViewManager.cs +++ b/Samples/Stylet.Samples.OverridingViewManager/CustomViewManager.cs @@ -27,8 +27,8 @@ namespace Stylet.Samples.OverridingViewManager // Dictionary of ViewModel type -> View type private readonly Dictionary viewModelToViewMapping; - public CustomViewManager(Func viewFactory, List viewAssemblies) - : base(viewFactory, viewAssemblies) + public CustomViewManager(ViewManagerConfig config) + : base(config) { var mappings = from type in this.ViewAssemblies.SelectMany(x => x.GetExportedTypes()) let attribute = type.GetCustomAttribute() diff --git a/Stylet/Bootstrapper.cs b/Stylet/Bootstrapper.cs index 9a2b757..178f0f8 100644 --- a/Stylet/Bootstrapper.cs +++ b/Stylet/Bootstrapper.cs @@ -48,10 +48,15 @@ namespace Stylet protected virtual void DefaultConfigureIoC(StyletIoCBuilder builder) { // Mark these as weak-bindings, so the user can replace them if they want + var viewManagerConfig = new ViewManagerConfig() + { + ViewFactory = this.GetInstance, + ViewAssemblies = new List() { this.GetType().Assembly } + }; + builder.Bind().ToInstance(viewManagerConfig).AsWeakBinding(); - var viewManager = new ViewManager(this.GetInstance, new List() { this.GetType().Assembly }); // Bind it to both IViewManager and to itself, so that people can get it with Container.Get() - builder.Bind().And().ToInstance(viewManager).AsWeakBinding(); + builder.Bind().And().To().AsWeakBinding(); builder.Bind().ToInstance(this).AsWeakBinding(); builder.Bind().To().InSingletonScope().AsWeakBinding(); diff --git a/Stylet/ViewManager.cs b/Stylet/ViewManager.cs index 7658929..07529b6 100644 --- a/Stylet/ViewManager.cs +++ b/Stylet/ViewManager.cs @@ -44,6 +44,22 @@ namespace Stylet UIElement CreateAndBindViewForModelIfNecessary(object model); } + /// + /// Configuration object consumed by + /// + public class ViewManagerConfig + { + /// + /// Gets or sets the ViewFactory to use + /// + public Func ViewFactory { get; set; } + + /// + /// Gets or sets the Assembles to search for views in + /// + public List ViewAssemblies { get; set; } + } + /// /// Default implementation of ViewManager. Responsible for locating, creating, and settings up Views. Also owns the View.Model and View.ActionTarget attached properties /// @@ -134,18 +150,17 @@ namespace Stylet /// /// Initialises a new instance of the class, with the given viewFactory /// - /// ViewFactory to use - /// Assembles to search for views in - public ViewManager(Func viewFactory, List viewAssemblies) + /// Configuration object + public ViewManager(ViewManagerConfig config) { // Config.ViewAssemblies cannot be null - ViewManagerConfig ensures this - if (viewFactory == null) - throw new ArgumentNullException("viewFactoryy"); - if (viewAssemblies == null) + if (config.ViewFactory == null) + throw new ArgumentNullException("viewFactory"); + if (config.ViewAssemblies == null) throw new ArgumentNullException("viewAssemblies"); - this.ViewFactory = viewFactory; - this.ViewAssemblies = viewAssemblies; + this.ViewFactory = config.ViewFactory; + this.ViewAssemblies = config.ViewAssemblies; } /// diff --git a/StyletUnitTests/ViewManagerTests.cs b/StyletUnitTests/ViewManagerTests.cs index 58152f0..6a62531 100644 --- a/StyletUnitTests/ViewManagerTests.cs +++ b/StyletUnitTests/ViewManagerTests.cs @@ -27,8 +27,8 @@ namespace StyletUnitTests private class AccessibleViewManager : ViewManager { - public AccessibleViewManager(Func viewFactory, List viewAssemblies) - : base(viewFactory, viewAssemblies) { } + public AccessibleViewManager(ViewManagerConfig config) + : base(config) { } public new UIElement CreateViewForModel(object model) { @@ -61,8 +61,8 @@ namespace StyletUnitTests public UIElement View; public object RequestedModel; - public CreatingAndBindingViewManager(Func viewFactory, List viewAssemblies) - : base(viewFactory, viewAssemblies) { } + public CreatingAndBindingViewManager(ViewManagerConfig config) + : base(config) { } public override UIElement CreateViewForModel(object model) { @@ -81,8 +81,8 @@ namespace StyletUnitTests private class LocatingViewManager : ViewManager { - public LocatingViewManager(Func viewFactory, List viewAssemblies) - : base(viewFactory, viewAssemblies) { } + public LocatingViewManager(ViewManagerConfig config) + : base(config) { } public Type LocatedViewType; protected override Type LocateViewForModel(Type modelType) @@ -93,8 +93,8 @@ namespace StyletUnitTests private class ResolvingViewManager : ViewManager { - public ResolvingViewManager(Func viewFactory, List viewAssemblies) - : base(viewFactory, viewAssemblies) { } + public ResolvingViewManager(ViewManagerConfig config) + : base(config) { } public Type ViewType; protected override Type ViewTypeForViewName(string viewName) @@ -123,18 +123,20 @@ namespace StyletUnitTests } } + private ViewManagerConfig config; private AccessibleViewManager viewManager; [SetUp] public void SetUp() { - this.viewManager = new AccessibleViewManager(type => null, new List()); + this.config = new ViewManagerConfig() { ViewFactory = type => null, ViewAssemblies = new List() }; + this.viewManager = new AccessibleViewManager(this.config); } [Test] public void ViewManagerRejectsNullViewAssemblies() { - Assert.Throws(() => new ViewManager(type => null, null)); + Assert.Throws(() => new ViewManager(new ViewManagerConfig() { ViewFactory = type => null, ViewAssemblies = null })); Assert.Throws(() => this.viewManager.ViewAssemblies = null); } @@ -159,7 +161,7 @@ namespace StyletUnitTests [Test] public void ViewManagerRejectsNullViewFactory() { - Assert.Throws(() => new ViewManager(null, new List())); + Assert.Throws(() => new ViewManager(new ViewManagerConfig() { ViewFactory = null, ViewAssemblies = new List() })); Assert.Throws(() => this.viewManager.ViewFactory = null); } @@ -197,7 +199,11 @@ namespace StyletUnitTests var target = new ContentControl(); var model = new object(); var view = new UIElement(); - var viewManager = new CreatingAndBindingViewManager(type => null, new List()); + var viewManager = new CreatingAndBindingViewManager(new ViewManagerConfig() + { + ViewFactory = type => view, + ViewAssemblies = new List(), + }); viewManager.View = view; @@ -215,7 +221,7 @@ namespace StyletUnitTests var target = new ContentControl(); var model = new object(); var view = new Window(); - var viewManager = new CreatingAndBindingViewManager(type => null, new List()); + var viewManager = new CreatingAndBindingViewManager(this.config); viewManager.View = view; @@ -225,7 +231,11 @@ namespace StyletUnitTests [Test] public void CreateViewForModelReturnsNullIfViewNotFound() { - var viewManager = new AccessibleViewManager(type => null, new List() { typeof(BootstrapperBase).Assembly, Assembly.GetExecutingAssembly() }); + var viewManager = new AccessibleViewManager(new ViewManagerConfig() + { + ViewFactory = type => null, + ViewAssemblies = new List() { typeof(BootstrapperBase).Assembly, Assembly.GetExecutingAssembly() } + }); Assert.IsNull(viewManager.ViewTypeForViewName("Test")); } @@ -238,7 +248,7 @@ namespace StyletUnitTests [Test] public void LocateViewForModelThrowsIfTypeLocationDoesntWork() { - var viewManager = new ResolvingViewManager(type => null, new List()); + var viewManager = new ResolvingViewManager(this.config); viewManager.ViewType = null; Assert.Throws(() => viewManager.LocateViewForModel(typeof(C1))); } @@ -246,7 +256,7 @@ namespace StyletUnitTests [Test] public void LocateViewForModelFindsViewForModel() { - var viewManager = new AccessibleViewManager(type => null, new List() { Assembly.GetExecutingAssembly() }); + var viewManager = new AccessibleViewManager(new ViewManagerConfig() { ViewFactory = type => null, ViewAssemblies = new List() { Assembly.GetExecutingAssembly() } }); var viewType = viewManager.LocateViewForModel(typeof(ViewManagerTestsViewModel)); Assert.AreEqual(typeof(ViewManagerTestsView), viewType); } @@ -254,7 +264,7 @@ namespace StyletUnitTests [Test] public void CreateViewForModelIfNecessaryThrowsIfViewIsNotConcreteUIElement() { - var viewManager = new LocatingViewManager(type => null, new List()); + var viewManager = new LocatingViewManager(this.config); viewManager.LocatedViewType = typeof(I1); Assert.Throws(() => viewManager.CreateAndBindViewForModelIfNecessary(new object())); @@ -270,7 +280,12 @@ namespace StyletUnitTests public void CreateAndBindViewForModelIfNecessaryCallsFetchesViewAndCallsInitializeComponent() { var view = new TestView(); - var viewManager = new LocatingViewManager(type => view, new List()); + var viewManager = new LocatingViewManager(new ViewManagerConfig() + { + ViewFactory = type => view, + ViewAssemblies = new List(), + }); + viewManager.LocatedViewType = typeof(TestView); var returnedView = viewManager.CreateAndBindViewForModelIfNecessary(new object()); @@ -295,7 +310,11 @@ namespace StyletUnitTests public void CreateViewForModelDoesNotComplainIfNoInitializeComponentMethod() { var view = new UIElement(); - var viewManager = new LocatingViewManager(type => view, new List()); + var viewManager = new LocatingViewManager(new ViewManagerConfig() + { + ViewFactory = type => view, + ViewAssemblies = new List(), + }); viewManager.LocatedViewType = typeof(UIElement); var returnedView = viewManager.CreateAndBindViewForModelIfNecessary(new object()); @@ -308,7 +327,7 @@ namespace StyletUnitTests { var view = new UIElement(); var model = new object(); - var viewManager = new AccessibleViewManager(type => null, new List()); + var viewManager = new AccessibleViewManager(this.config); viewManager.BindViewToModel(view, model); @@ -320,7 +339,7 @@ namespace StyletUnitTests { var view = new FrameworkElement(); var model = new object(); - var viewManager = new AccessibleViewManager(type => null, new List()); + var viewManager = new AccessibleViewManager(this.config); viewManager.BindViewToModel(view, model); Assert.AreEqual(model, view.DataContext); @@ -331,7 +350,7 @@ namespace StyletUnitTests { var view = new UIElement(); var model = new Mock(); - var viewManager = new AccessibleViewManager(type => null, new List()); + var viewManager = new AccessibleViewManager(this.config); viewManager.BindViewToModel(view, model.Object); model.Verify(x => x.AttachView(view));