Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add *ViewModel -> *View resolution as fallback #71

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/FreshMvvm/FreshMvvm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<ItemGroup>
<Compile Include="FreshViewModelWrapper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="FreshBasePageModel.cs" />
<Compile Include="FreshIOC.cs" />
Expand Down
69 changes: 64 additions & 5 deletions src/FreshMvvm/FreshPageModelResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> () where T : FreshBasePageModel
{
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions src/FreshMvvm/FreshViewModelWrapper.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}