diff --git a/src/FreshMvvm/FreshMvvm.csproj b/src/FreshMvvm/FreshMvvm.csproj index d8f33a8..70a0ce1 100644 --- a/src/FreshMvvm/FreshMvvm.csproj +++ b/src/FreshMvvm/FreshMvvm.csproj @@ -31,6 +31,7 @@ false + diff --git a/src/FreshMvvm/FreshPageModelResolver.cs b/src/FreshMvvm/FreshPageModelResolver.cs index 74c54d3..5df5a70 100644 --- a/src/FreshMvvm/FreshPageModelResolver.cs +++ b/src/FreshMvvm/FreshPageModelResolver.cs @@ -5,7 +5,20 @@ namespace FreshMvvm { public static class FreshPageModelResolver { - public static IFreshPageModelMapper PageModelMapper { get; set; } = new FreshPageModelMapper(); + private static bool _isPageModelMapperFound; + + private static IFreshPageModelMapper _pageModelMapper; + + public static IFreshPageModelMapper PageModelMapper + { + get { return _pageModelMapper; } + + set + { + _pageModelMapper = value; + _isPageModelMapperFound = true; + } + } public static Page ResolvePageModel () where T : FreshBasePageModel { @@ -33,10 +46,12 @@ public static Page ResolvePageModel (Type type, object data) public static Page ResolvePageModel (Type type, object data, FreshBasePageModel pageModel) { - var name = PageModelMapper.GetPageTypeName (type); - var pageType = Type.GetType (name); - if (pageType == null) - throw new Exception (name + " not found"); + Type pageType; + + if (_isPageModelMapperFound == false) + pageType = FindPageModelMapper(type); + else + pageType = ResolvePageType(_pageModelMapper, type); var page = (Page)FreshIOC.Container.Resolve (pageType); @@ -45,6 +60,50 @@ public static Page ResolvePageModel (Type type, object data, FreshBasePageModel return page; } + private static Type FindPageModelMapper(Type type) + { + string exceptionMessages = string.Empty; + Type pageType; + var pModelMapper = new FreshPageModelMapper(); + try + { + pageType = ResolvePageType(pModelMapper, type); + PageModelMapper = pModelMapper; + return pageType; + } + catch(Exception e) + { + exceptionMessages += e.Message; + } + + var vModelMapper = new FreshViewModelMapper(); + try + { + pageType = ResolvePageType(vModelMapper, type); + PageModelMapper = vModelMapper; + return pageType; + } + catch(Exception e) + { + exceptionMessages += string.Format(", {0}", e.Message); + throw new Exception(exceptionMessages); + } + } + + private static Type ResolvePageType(IFreshPageModelMapper pageModelMapper, Type type) + { + var name = pageModelMapper.GetPageTypeName(type); + var pageType = Type.GetType(name); + + if (pageType == null) + throw new Exception(string.Format("Type not found: [{0}]", name)); + + if (_isPageModelMapperFound == false) + _isPageModelMapperFound = true; + + return pageType; + } + public static Page BindingPageModel(object data, Page targetPage, FreshBasePageModel pageModel) { pageModel.WireEvents (targetPage); diff --git a/src/FreshMvvm/FreshViewModelWrapper.cs b/src/FreshMvvm/FreshViewModelWrapper.cs new file mode 100644 index 0000000..d1b9572 --- /dev/null +++ b/src/FreshMvvm/FreshViewModelWrapper.cs @@ -0,0 +1,15 @@ +using System; + +namespace FreshMvvm +{ + public class FreshViewModelMapper : IFreshPageModelMapper + { + public string GetPageTypeName(Type pageModelType) + { + return pageModelType.AssemblyQualifiedName + .Replace ("PageModel", "View") + .Replace ("ViewModel", "View"); + } + } +} +