Skip to content

Commit

Permalink
refactor Regions to inject IContainerExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
dansiegel committed Apr 12, 2020
1 parent c0e2786 commit 0d24b8a
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 105 deletions.
38 changes: 24 additions & 14 deletions PrismLibrary_Wpf.sln
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.29926.136
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{0DF20AD9-0A3F-4B61-BC8A-2D6DED59A7D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prism.Core", "src\Prism.Core\Prism.Core.csproj", "{F1503310-6555-48EA-AAFA-BF069E6E0EB0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Core", "src\Prism.Core\Prism.Core.csproj", "{F1503310-6555-48EA-AAFA-BF069E6E0EB0}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{F1F2E587-3A5D-4A76-B0BA-A04791628A9C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prism.Core.Tests", "tests\Prism.Core.Tests\Prism.Core.Tests.csproj", "{31AE5358-9077-4CAB-BE3B-FB6415A5EC49}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Core.Tests", "tests\Prism.Core.Tests\Prism.Core.Tests.csproj", "{31AE5358-9077-4CAB-BE3B-FB6415A5EC49}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Wpf", "Wpf", "{0CEBA764-DF92-4A2A-8F0B-EF2C32BC6514}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prism.Wpf", "src\Wpf\Prism.Wpf\Prism.Wpf.csproj", "{8DC0C660-7F42-40C4-8DE0-BA2A078C1202}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Wpf", "src\Wpf\Prism.Wpf\Prism.Wpf.csproj", "{8DC0C660-7F42-40C4-8DE0-BA2A078C1202}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prism.DryIoc.Wpf", "src\Wpf\Prism.DryIoc.Wpf\Prism.DryIoc.Wpf.csproj", "{A2976E8C-589A-47C7-B81D-D59CBCED2E0B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.DryIoc.Wpf", "src\Wpf\Prism.DryIoc.Wpf\Prism.DryIoc.Wpf.csproj", "{A2976E8C-589A-47C7-B81D-D59CBCED2E0B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prism.Unity.Wpf", "src\Wpf\Prism.Unity.Wpf\Prism.Unity.Wpf.csproj", "{2F184285-B860-4C68-AAF7-73AF6B2F98D8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Unity.Wpf", "src\Wpf\Prism.Unity.Wpf\Prism.Unity.Wpf.csproj", "{2F184285-B860-4C68-AAF7-73AF6B2F98D8}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Wpf", "Wpf", "{414633C4-2FB9-4A81-8071-832C33C027BC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prism.Wpf.Tests", "tests\Wpf\Prism.Wpf.Tests\Prism.Wpf.Tests.csproj", "{226A223E-8239-4986-A3BD-3730E457EAB2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Wpf.Tests", "tests\Wpf\Prism.Wpf.Tests\Prism.Wpf.Tests.csproj", "{226A223E-8239-4986-A3BD-3730E457EAB2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prism.DryIoc.Wpf.Tests", "tests\Wpf\Prism.DryIoc.Wpf.Tests\Prism.DryIoc.Wpf.Tests.csproj", "{A60CF7B0-56AD-4568-9720-E30EE5855149}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.DryIoc.Wpf.Tests", "tests\Wpf\Prism.DryIoc.Wpf.Tests\Prism.DryIoc.Wpf.Tests.csproj", "{A60CF7B0-56AD-4568-9720-E30EE5855149}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prism.Unity.Wpf.Tests", "tests\Wpf\Prism.Unity.Wpf.Tests\Prism.Unity.Wpf.Tests.csproj", "{FD11FA19-6A06-471D-8471-94C1153104CD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.Unity.Wpf.Tests", "tests\Wpf\Prism.Unity.Wpf.Tests\Prism.Unity.Wpf.Tests.csproj", "{FD11FA19-6A06-471D-8471-94C1153104CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Prism.IocContainer.Wpf.Tests.Support", "tests\Wpf\Prism.IocContainer.Wpf.Tests.Support\Prism.IocContainer.Wpf.Tests.Support.csproj", "{4039BC28-15D4-4EE1-82D8-80BD4CC61C50}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prism.IocContainer.Wpf.Tests.Support", "tests\Wpf\Prism.IocContainer.Wpf.Tests.Support\Prism.IocContainer.Wpf.Tests.Support.csproj", "{4039BC28-15D4-4EE1-82D8-80BD4CC61C50}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Prism.Container.Wpf.Shared", "tests\Wpf\Prism.Container.Wpf.Shared\Prism.Container.Wpf.Shared.shproj", "{BD42A7D6-A84D-4D27-9C28-7F6A2EC477F1}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
tests\Wpf\Prism.Container.Wpf.Shared\Prism.Container.Wpf.Shared.projitems*{bd42a7d6-a84d-4d27-9c28-7f6a2ec477f1}*SharedItemsImports = 13
tests\Wpf\Prism.Container.Wpf.Shared\Prism.Container.Wpf.Shared.projitems*{fd11fa19-6a06-471d-8471-94c1153104cd}*SharedItemsImports = 5
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Expand All @@ -38,9 +44,6 @@ Global
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F1503310-6555-48EA-AAFA-BF069E6E0EB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F1503310-6555-48EA-AAFA-BF069E6E0EB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
Expand Down Expand Up @@ -151,6 +154,9 @@ Global
{4039BC28-15D4-4EE1-82D8-80BD4CC61C50}.Release|x86.ActiveCfg = Release|Any CPU
{4039BC28-15D4-4EE1-82D8-80BD4CC61C50}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F1503310-6555-48EA-AAFA-BF069E6E0EB0} = {0DF20AD9-0A3F-4B61-BC8A-2D6DED59A7D2}
{31AE5358-9077-4CAB-BE3B-FB6415A5EC49} = {F1F2E587-3A5D-4A76-B0BA-A04791628A9C}
Expand All @@ -163,5 +169,9 @@ Global
{A60CF7B0-56AD-4568-9720-E30EE5855149} = {414633C4-2FB9-4A81-8071-832C33C027BC}
{FD11FA19-6A06-471D-8471-94C1153104CD} = {414633C4-2FB9-4A81-8071-832C33C027BC}
{4039BC28-15D4-4EE1-82D8-80BD4CC61C50} = {414633C4-2FB9-4A81-8071-832C33C027BC}
{BD42A7D6-A84D-4D27-9C28-7F6A2EC477F1} = {414633C4-2FB9-4A81-8071-832C33C027BC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {544BBD3D-C3AA-405A-9832-122679F8AD05}
EndGlobalSection
EndGlobal
14 changes: 7 additions & 7 deletions src/Wpf/Prism.Wpf/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected virtual void ConfigureModuleCatalog()
/// </summary>
protected virtual void ConfigureViewModelLocator()
{
ViewModelLocationProvider.SetDefaultViewModelFactory((type) => ContainerLocator.Current.Resolve(type));
ViewModelLocationProvider.SetDefaultViewModelFactory((type) => ContainerLocator.Container.Resolve(type));
}

/// <summary>
Expand All @@ -106,7 +106,7 @@ protected virtual void RegisterFrameworkExceptionTypes()
/// </summary>
protected virtual void InitializeModules()
{
IModuleManager manager = ContainerLocator.Current.Resolve<IModuleManager>();
IModuleManager manager = ContainerLocator.Container.Resolve<IModuleManager>();
manager.Run();
}

Expand All @@ -118,12 +118,12 @@ protected virtual void InitializeModules()
/// <returns>The <see cref="RegionAdapterMappings"/> instance containing all the mappings.</returns>
protected virtual RegionAdapterMappings ConfigureRegionAdapterMappings()
{
RegionAdapterMappings regionAdapterMappings = ContainerLocator.Current.Resolve<RegionAdapterMappings>();
RegionAdapterMappings regionAdapterMappings = ContainerLocator.Container.Resolve<RegionAdapterMappings>();
if (regionAdapterMappings != null)
{
regionAdapterMappings.RegisterMapping(typeof(Selector), ContainerLocator.Current.Resolve<SelectorRegionAdapter>());
regionAdapterMappings.RegisterMapping(typeof(ItemsControl), ContainerLocator.Current.Resolve<ItemsControlRegionAdapter>());
regionAdapterMappings.RegisterMapping(typeof(ContentControl), ContainerLocator.Current.Resolve<ContentControlRegionAdapter>());
regionAdapterMappings.RegisterMapping(typeof(Selector), ContainerLocator.Container.Resolve<SelectorRegionAdapter>());
regionAdapterMappings.RegisterMapping(typeof(ItemsControl), ContainerLocator.Container.Resolve<ItemsControlRegionAdapter>());
regionAdapterMappings.RegisterMapping(typeof(ContentControl), ContainerLocator.Container.Resolve<ContentControlRegionAdapter>());
}

return regionAdapterMappings;
Expand All @@ -135,7 +135,7 @@ protected virtual RegionAdapterMappings ConfigureRegionAdapterMappings()
/// </summary>
protected virtual IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
{
var defaultRegionBehaviorTypesDictionary = ContainerLocator.Current.Resolve<IRegionBehaviorFactory>();
var defaultRegionBehaviorTypesDictionary = ContainerLocator.Container.Resolve<IRegionBehaviorFactory>();

if (defaultRegionBehaviorTypesDictionary != null)
{
Expand Down
131 changes: 58 additions & 73 deletions src/Wpf/Prism.Wpf/Regions/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ namespace Prism.Regions
/// </summary>
public class Region : IRegion
{
private ObservableCollection<ItemMetadata> itemMetadataCollection;
private string name;
private ViewsCollection views;
private ViewsCollection activeViews;
private object context;
private IRegionManager regionManager;
private IRegionNavigationService regionNavigationService;
private ObservableCollection<ItemMetadata> _itemMetadataCollection;
private string _name;
private ViewsCollection _views;
private ViewsCollection _activeViews;
private object _context;
private IRegionManager _regionManager;
private IRegionNavigationService _regionNavigationService;

private Comparison<object> sort;
private Comparison<object> _sort;

/// <summary>
/// Initializes a new instance of <see cref="Region"/>.
/// </summary>
public Region()
{
this.Behaviors = new RegionBehaviorCollection(this);
Behaviors = new RegionBehaviorCollection(this);

this.sort = Region.DefaultSortComparison;
_sort = Region.DefaultSortComparison;
}

/// <summary>
Expand All @@ -51,17 +51,14 @@ public Region()
/// <value>The context value to be shared.</value>
public object Context
{
get
{
return this.context;
}
get => _context;

set
{
if (this.context != value)
if (_context != value)
{
this.context = value;
this.OnPropertyChanged("Context");
_context = value;
OnPropertyChanged(nameof(Context));
}
}
}
Expand All @@ -72,25 +69,22 @@ public object Context
/// <value>The name of the region.</value>
public string Name
{
get
{
return this.name;
}
get => _name;

set
{
if (this.name != null && this.name != value)
if (_name != null && _name != value)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.CannotChangeRegionNameException, this.name));
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.CannotChangeRegionNameException, _name));
}

if (string.IsNullOrEmpty(value))
{
throw new ArgumentException(Resources.RegionNameCannotBeEmptyException);
}

this.name = value;
this.OnPropertyChanged("Name");
_name = value;
OnPropertyChanged(nameof(Name));
}
}

Expand All @@ -102,13 +96,15 @@ public virtual IViewsCollection Views
{
get
{
if (this.views == null)
if (_views == null)
{
this.views = new ViewsCollection(ItemMetadataCollection, x => true);
this.views.SortComparison = this.sort;
_views = new ViewsCollection(ItemMetadataCollection, x => true)
{
SortComparison = _sort
};
}

return this.views;
return _views;
}
}

Expand All @@ -120,19 +116,23 @@ public virtual IViewsCollection ActiveViews
{
get
{
if (this.views == null)
if (_views == null)
{
this.views = new ViewsCollection(ItemMetadataCollection, x => true);
this.views.SortComparison = this.sort;
_views = new ViewsCollection(ItemMetadataCollection, x => true)
{
SortComparison = _sort
};
}

if (this.activeViews == null)
if (_activeViews == null)
{
this.activeViews = new ViewsCollection(ItemMetadataCollection, x => x.IsActive);
this.activeViews.SortComparison = this.sort;
_activeViews = new ViewsCollection(ItemMetadataCollection, x => x.IsActive)
{
SortComparison = _sort
};
}

return this.activeViews;
return _activeViews;
}
}

Expand All @@ -142,22 +142,19 @@ public virtual IViewsCollection ActiveViews
/// <value>The comparison to use.</value>
public Comparison<object> SortComparison
{
get
{
return this.sort;
}
get => _sort;
set
{
this.sort = value;
_sort = value;

if (this.activeViews != null)
if (_activeViews != null)
{
this.activeViews.SortComparison = this.sort;
_activeViews.SortComparison = _sort;
}

if (this.views != null)
if (_views != null)
{
this.views.SortComparison = this.sort;
_views.SortComparison = _sort;
}
}
}
Expand All @@ -170,17 +167,14 @@ public Comparison<object> SortComparison
/// used by the developer explicitely.</remarks>
public IRegionManager RegionManager
{
get
{
return this.regionManager;
}
get => _regionManager;

set
{
if (this.regionManager != value)
if (_regionManager != value)
{
this.regionManager = value;
this.OnPropertyChanged("RegionManager");
_regionManager = value;
OnPropertyChanged(nameof(RegionManager));
}
}
}
Expand All @@ -193,19 +187,16 @@ public IRegionNavigationService NavigationService
{
get
{
if (this.regionNavigationService == null)
if (_regionNavigationService == null)
{
this.regionNavigationService = ContainerLocator.Current.Resolve<IRegionNavigationService>();
this.regionNavigationService.Region = this;
_regionNavigationService = ContainerLocator.Container.Resolve<IRegionNavigationService>();
_regionNavigationService.Region = this;
}

return this.regionNavigationService;
return _regionNavigationService;
}

set
{
this.regionNavigationService = value;
}
set => _regionNavigationService = value;
}

/// <summary>
Expand All @@ -216,12 +207,12 @@ protected virtual ObservableCollection<ItemMetadata> ItemMetadataCollection
{
get
{
if (this.itemMetadataCollection == null)
if (_itemMetadataCollection == null)
{
this.itemMetadataCollection = new ObservableCollection<ItemMetadata>();
_itemMetadataCollection = new ObservableCollection<ItemMetadata>();
}

return this.itemMetadataCollection;
return _itemMetadataCollection;
}
}

Expand Down Expand Up @@ -274,10 +265,9 @@ public virtual void Remove(object view)
{
ItemMetadata itemMetadata = this.GetItemMetadataOrThrow(view);

this.ItemMetadataCollection.Remove(itemMetadata);
ItemMetadataCollection.Remove(itemMetadata);

DependencyObject dependencyObject = view as DependencyObject;
if (dependencyObject != null && Regions.RegionManager.GetRegionManager(dependencyObject) == this.RegionManager)
if (view is DependencyObject dependencyObject && Regions.RegionManager.GetRegionManager(dependencyObject) == this.RegionManager)
{
dependencyObject.ClearValue(Regions.RegionManager.RegionManagerProperty);
}
Expand Down Expand Up @@ -382,9 +372,8 @@ private void InnerAdd(object view, string viewName, IRegionManager scopedRegionM
itemMetadata.Name = viewName;
}

DependencyObject dependencyObject = view as DependencyObject;

if (dependencyObject != null)
if (view is DependencyObject dependencyObject)
{
Regions.RegionManager.SetRegionManager(dependencyObject, scopedRegionManager);
}
Expand All @@ -407,11 +396,7 @@ private ItemMetadata GetItemMetadataOrThrow(object view)

private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler eventHandler = this.PropertyChanged;
if (eventHandler != null)
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

/// <summary>
Expand Down
Loading

0 comments on commit 0d24b8a

Please sign in to comment.