Re-jig ViewManager slightly to make it easier to override

This commit is contained in:
Antony Male 2015-02-27 09:19:36 +00:00
parent 68c5ed2a50
commit 21e6441a2f
2 changed files with 44 additions and 13 deletions

View File

@ -154,17 +154,7 @@ namespace Stylet
protected virtual Type ViewTypeForViewName(string viewName)
{
// TODO: This might need some more thinking
var viewType = this.Assemblies.SelectMany(x => x.GetExportedTypes()).FirstOrDefault(x => x.FullName == viewName);
if (viewType == null)
{
var e = new StyletViewLocationException(String.Format("Unable to find a View with type {0}", viewName), viewName);
logger.Error(e);
throw e;
}
logger.Info("Searching for a View with name {0}, and found {1}", viewName, viewType);
return viewType;
return this.Assemblies.SelectMany(x => x.GetExportedTypes()).FirstOrDefault(x => x.FullName == viewName);
}
/// <summary>
@ -194,6 +184,17 @@ namespace Stylet
throw new StyletViewLocationException(String.Format("Unable to transform ViewModel name {0} into a suitable View name", modelName), viewName);
var viewType = this.ViewTypeForViewName(viewName);
if (viewType == null)
{
var e = new StyletViewLocationException(String.Format("Unable to find a View with type {0}", viewName), viewName);
logger.Error(e);
throw e;
}
else
{
logger.Info("Searching for a View with name {0}, and found {1}", viewName, viewType);
}
return viewType;
}

View File

@ -92,6 +92,27 @@ namespace StyletUnitTests
}
}
private class ResolvingViewManager : ViewManager
{
public ResolvingViewManager(IViewManagerConfig config) : base(config) { }
public Type ViewType;
protected override Type ViewTypeForViewName(string viewName)
{
return ViewType;
}
public new Type LocateViewForModel(Type modelType)
{
return base.LocateViewForModel(modelType);
}
protected override string ViewTypeNameForModelTypeName(string modelTypeName)
{
return "testy";
}
}
private class TestView : UIElement
{
@ -158,14 +179,14 @@ namespace StyletUnitTests
}
[Test]
public void CreateViewForModelThrowsIfViewNotFound()
public void CreateViewForModelReturnsNullIfViewNotFound()
{
var config = new Mock<IViewManagerConfig>();
config.Setup(x => x.GetInstance(typeof(C1))).Returns(null);
config.SetupGet(x => x.Assemblies).Returns(new List<Assembly>());
var viewManager = new AccessibleViewManager(config.Object);
Assert.Throws<StyletViewLocationException>(() => viewManager.ViewTypeForViewName("Test"));
Assert.IsNull(viewManager.ViewTypeForViewName("Test"));
}
[Test]
@ -174,6 +195,15 @@ namespace StyletUnitTests
Assert.Throws<StyletViewLocationException>(() => this.viewManager.LocateViewForModel(typeof(C1)));
}
[Test]
public void LocateViewForModelThrowsIfTypeLocationDoesntWork()
{
var config = new Mock<IViewManagerConfig>();
var viewManager = new ResolvingViewManager(config.Object);
viewManager.ViewType = null;
Assert.Throws<StyletViewLocationException>(() => viewManager.LocateViewForModel(typeof(C1)));
}
[Test]
public void LocateViewForModelFindsViewForModel()
{