From 8a6f010fbb138e60555851054e1b4f9facaf39a8 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Fri, 25 Sep 2015 10:57:46 +0100 Subject: [PATCH] Add support for namespace transformations to ViewManager --- Stylet/ViewManager.cs | 38 +++++++++++++++++++++---- StyletUnitTests/ViewManagerTests.cs | 43 +++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/Stylet/ViewManager.cs b/Stylet/ViewManager.cs index 71e3f0e..39383b1 100644 --- a/Stylet/ViewManager.cs +++ b/Stylet/ViewManager.cs @@ -49,7 +49,7 @@ namespace Stylet /// public class ViewManagerConfig { - private List _viewAssemblies; + private List _viewAssemblies = new List(); /// /// Gets and sets the assemblies which are used for IoC container auto-binding and searching for Views. @@ -70,12 +70,21 @@ namespace Stylet /// public Func ViewFactory { get; set; } + + private Dictionary _namespaceTransformations = new Dictionary(); + /// - /// Instantiates a new instance of the class + /// Gets and sets a set of transformations to be applied to the ViewModel's namespace: string to find -> string to replace it with /// - public ViewManagerConfig() + public Dictionary NamespaceTransformations { - this.ViewAssemblies = new List(); + get { return this._namespaceTransformations; } + set + { + if (value == null) + throw new ArgumentNullException(); + this._namespaceTransformations = value; + } } } @@ -96,6 +105,11 @@ namespace Stylet /// protected Func ViewFactory { get; set; } + /// + /// Gets and sets a set of transformations to be applied to the ViewModel's namespace: string to find -> string to replace it with + /// + protected Dictionary NamespaceTransformations { get; set; } + /// /// Initialises a new instance of the class, with the given viewFactory /// @@ -105,8 +119,10 @@ namespace Stylet // Config.ViewAssemblies cannot be null - ViewManagerConfig ensures this if (config.ViewFactory == null) throw new ArgumentNullException("config.ViewFactory"); + this.ViewAssemblies = config.ViewAssemblies; this.ViewFactory = config.ViewFactory; + this.NamespaceTransformations = config.NamespaceTransformations; } /// @@ -193,7 +209,19 @@ namespace Stylet /// View type name protected virtual string ViewTypeNameForModelTypeName(string modelTypeName) { - return Regex.Replace(modelTypeName, @"(?<=.)ViewModel(?=s?\.)|ViewModel$", "View"); + string transformed = modelTypeName; + + foreach (var transformation in this.NamespaceTransformations) + { + if (transformed.StartsWith(transformation.Key + ".")) + { + transformed = transformation.Value + transformed.Substring(transformation.Key.Length); + break; + } + } + + transformed = Regex.Replace(transformed, @"(?<=.)ViewModel(?=s?\.)|ViewModel$", "View"); + return transformed; } /// diff --git a/StyletUnitTests/ViewManagerTests.cs b/StyletUnitTests/ViewManagerTests.cs index bd53ace..ef7378a 100644 --- a/StyletUnitTests/ViewManagerTests.cs +++ b/StyletUnitTests/ViewManagerTests.cs @@ -132,6 +132,27 @@ namespace StyletUnitTests this.viewManager = new AccessibleViewManager(this.viewManagerConfig); } + [Test] + public void ViewManagerConfigRejectsNullViewAssemblies() + { + var config = new ViewManagerConfig(); + Assert.Throws(() => config.ViewAssemblies = null); + } + + [Test] + public void ViewManagerConfigRejectsNullNamespaceTransformations() + { + var config = new ViewManagerConfig(); + Assert.Throws(() => config.NamespaceTransformations = null); + } + + [Test] + public void ViewManagerRejectsNullViewFactory() + { + var config = new ViewManagerConfig(); + Assert.Throws(() => new ViewManager(config)); + } + [Test] public void OnModelChangedDoesNothingIfNoChange() { @@ -327,8 +348,6 @@ namespace StyletUnitTests model.Verify(x => x.AttachView(view)); } - - [Test] public void ViewNameResolutionWorksAsExpected() { @@ -348,5 +367,25 @@ namespace StyletUnitTests Assert.AreEqual("ViewModels.TestView", viewManager.ViewTypeNameForModelTypeName("ViewModels.TestViewModel")); } + + [Test] + public void NamespaceTransformationsTransformsNamespace() + { + this.viewManagerConfig.NamespaceTransformations["Foo.Bar"] = "Baz.Yay"; + var viewManager = new AccessibleViewManager(this.viewManagerConfig); + + Assert.AreEqual("Baz.Yay.ThingView", viewManager.ViewTypeNameForModelTypeName("Foo.Bar.ThingViewModel")); + Assert.AreEqual("Baz.Yay.Thing", viewManager.ViewTypeNameForModelTypeName("Foo.Bar.Thing")); + } + + [Test] + public void NamespaceTransformationsTransformOnlyFirstMatch() + { + this.viewManagerConfig.NamespaceTransformations["Foo.Bar"] = "Baz.Yay"; + this.viewManagerConfig.NamespaceTransformations["Baz.Yay"] = "One.Two"; + + Assert.AreEqual("Baz.Yay.ThingView", viewManager.ViewTypeNameForModelTypeName("Foo.Bar.ThingViewModel")); + Assert.AreEqual("One.Two.ThingView", viewManager.ViewTypeNameForModelTypeName("Baz.Yay.ThingViewModel")); + } } }