Skip to content

Commit

Permalink
Merge pull request #2029 from PrismLibrary/issue-2026
Browse files Browse the repository at this point in the history
added RegiserMappings generic method
  • Loading branch information
brianlagunas authored Mar 5, 2020
2 parents 030d31c + ee20851 commit 257e763
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Xunit;
using Prism.Regions;
using Prism.Wpf.Tests.Mocks;
using CommonServiceLocator;

namespace Prism.Wpf.Tests.Regions
{
Expand All @@ -26,6 +27,46 @@ public void ShouldGetRegisteredMapping()
Assert.Same(regionAdapter, returnedAdapter);
}

[Fact]
public void ShouldGetRegisteredMapping_UsingGenericControl()
{
var regionAdapterMappings = new RegionAdapterMappings();
var regionAdapter = new MockRegionAdapter();

regionAdapterMappings.RegisterMapping<ItemsControl>(regionAdapter);

var returnedAdapter = regionAdapterMappings.GetMapping<ItemsControl>();

Assert.NotNull(returnedAdapter);
Assert.Same(regionAdapter, returnedAdapter);
}

[Fact]
public void ShouldGetRegisteredMapping_UsingGenericControlAndAdapter()
{
try
{
var regionAdapterMappings = new RegionAdapterMappings();
var regionAdapter = new MockRegionAdapter();

ServiceLocator.SetLocatorProvider(() => new MockServiceLocator
{
GetInstance = t => regionAdapter
});

regionAdapterMappings.RegisterMapping<ItemsControl, MockRegionAdapter>();

var returnedAdapter = regionAdapterMappings.GetMapping<ItemsControl>();

Assert.NotNull(returnedAdapter);
Assert.Same(regionAdapter, returnedAdapter);
}
finally
{
ServiceLocator.SetLocatorProvider(null);
}
}

[Fact]
public void ShouldGetMappingForDerivedTypesThanTheRegisteredOnes()
{
Expand Down Expand Up @@ -76,7 +117,6 @@ public void RegisterAMappingThatAlreadyExistsThrows()
regionAdapterMappings.RegisterMapping(typeof(ItemsControl), regionAdapter);
regionAdapterMappings.RegisterMapping(typeof(ItemsControl), regionAdapter);
});

}

[Fact]
Expand Down
36 changes: 34 additions & 2 deletions Source/Wpf/Prism.Wpf/Regions/RegionAdapterMappings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


using Prism.Properties;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -36,6 +34,25 @@ public void RegisterMapping(Type controlType, IRegionAdapter adapter)
mappings.Add(controlType, adapter);
}

/// <summary>
/// Registers the mapping between a type and an adapter.
/// </summary>
/// <typeparam name="TControl">The type of the control</typeparam>
public void RegisterMapping<TControl>(IRegionAdapter adapter)
{
RegisterMapping(typeof(TControl), adapter);
}

/// <summary>
/// Registers the mapping between a type and an adapter.
/// </summary>
/// <typeparam name="TControl">The type of the control</typeparam>
/// <typeparam name="TAdapter">The type of the IRegionAdapter to use with the TControl</typeparam>
public void RegisterMapping<TControl, TAdapter>() where TAdapter : IRegionAdapter
{
RegisterMapping(typeof(TControl), (IRegionAdapter)CommonServiceLocator.ServiceLocator.Current.GetInstance(typeof(TAdapter)));
}

/// <summary>
/// Returns the adapter associated with the type provided.
/// </summary>
Expand All @@ -61,5 +78,20 @@ public IRegionAdapter GetMapping(Type controlType)
}
throw new KeyNotFoundException(String.Format(CultureInfo.CurrentCulture, Resources.NoRegionAdapterException, controlType));
}

/// <summary>
/// Returns the adapter associated with the type provided.
/// </summary>
/// <typeparam name="T">The control type used to obtain the <see cref="IRegionAdapter"/> mapped.</typeparam>
/// <returns>The <see cref="IRegionAdapter"/> mapped to the <typeparamref name="T"/>.</returns>
/// <remarks>This class will look for a registered type for <typeparamref name="T"/> and if there is not any,
/// it will look for a registered type for any of its ancestors in the class hierarchy.
/// If there is no registered type for <typeparamref name="T"/> or any of its ancestors,
/// an exception will be thrown.</remarks>
/// <exception cref="KeyNotFoundException">When there is no registered type for <typeparamref name="T"/> or any of its ancestors.</exception>
public IRegionAdapter GetMapping<T>()
{
return GetMapping(typeof(T));
}
}
}

0 comments on commit 257e763

Please sign in to comment.