Skip to content

Commit

Permalink
Allow passing a contract name into view resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
anaisbetts committed May 19, 2013
1 parent 70a366e commit f52daa0
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions ReactiveUI/ViewLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace ReactiveUI
{
public interface IViewLocator : IEnableLogger
{
IViewFor ResolveView<T>(T viewModel) where T : class;
IViewFor ResolveView<T>(T viewModel, string contract = null) where T : class;
}

public static class ViewLocator
Expand Down Expand Up @@ -44,7 +44,7 @@ public DefaultViewLocator(Func<string, string> viewModelToViewFunc = null)
/// <param name="viewModel">The ViewModel for which to find the
/// associated View.</param>
/// <returns>The View for the ViewModel.</returns>
public IViewFor ResolveView<T>(T viewModel)
public IViewFor ResolveView<T>(T viewModel, string contract = null)
where T : class
{
// Given IFooBarViewModel (whose name we derive from T), we'll look
Expand All @@ -54,10 +54,9 @@ public IViewFor ResolveView<T>(T viewModel)
// * IViewFor<FooBarViewModel> (the original behavior in RxUI 3.1)

var attrs = viewModel.GetType().GetCustomAttributes(typeof (ViewContractAttribute), true);
string key = null;

if (attrs.Any()) {
key = ((ViewContractAttribute) attrs.First()).Contract;
contract = contract ?? ((ViewContractAttribute) attrs.First()).Contract;
}

// IFooBarView that implements IViewFor (or custom ViewModelToViewFunc)
Expand All @@ -66,7 +65,7 @@ public IViewFor ResolveView<T>(T viewModel)
var type = Reflection.ReallyFindType(typeToFind, false);

if (type != null) {
var ret = RxApp.DependencyResolver.GetService(type, key) as IViewFor;
var ret = RxApp.DependencyResolver.GetService(type, contract) as IViewFor;
if (ret != null) return ret;
}
} catch (Exception ex) {
Expand All @@ -76,7 +75,7 @@ public IViewFor ResolveView<T>(T viewModel)
var viewType = typeof (IViewFor<>);

// IViewFor<FooBarViewModel> (the original behavior in RxUI 3.1)
return (IViewFor) RxApp.DependencyResolver.GetService(viewType.MakeGenericType(viewModel.GetType()), key);
return (IViewFor) RxApp.DependencyResolver.GetService(viewType.MakeGenericType(viewModel.GetType()), contract);
}

static string interfaceifyTypeName(string typeName)
Expand Down

0 comments on commit f52daa0

Please sign in to comment.