Skip to content

Commit 85ef687

Browse files
committed
Enhance Prism Unity sample
1 parent 76195d3 commit 85ef687

13 files changed

+80
-68
lines changed

WpfTraining/PrismUnityDemo/PrismUnityDemo/Bootstrapper.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ protected override void ConfigureContainer()
2121
{
2222
base.ConfigureContainer();
2323

24-
this.Container.RegisterType<MainWindowNavigationController>(
25-
new ContainerControlledLifetimeManager());
26-
this.Container.RegisterType(typeof(ProductMaintenanceViewModel));
27-
this.Container.RegisterTypeForNavigation<ProductDetailView>(nameof(ProductDetailView));
28-
this.Container.RegisterType(typeof(ProductDetailViewModel));
24+
// Singletons -> use ContainerControlledLifetimeManager
25+
this.Container.RegisterType<MainWindowNavigationController>(new ContainerControlledLifetimeManager());
2926
this.Container.RegisterType<GlobalCommands>(new ContainerControlledLifetimeManager());
27+
28+
// Register view that is used for Prism navigation
29+
this.Container.RegisterTypeForNavigation<ProductDetailView>(nameof(ProductDetailView));
30+
31+
// Register view models
32+
this.Container.RegisterType<ProductMaintenanceViewModel>();
33+
this.Container.RegisterType<ProductDetailViewModel>();
3034
}
3135

3236
protected override void ConfigureModuleCatalog()
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,8 @@
1-
using System.ComponentModel;
2-
3-
namespace PrismUnityDemo.Contracts
1+
namespace PrismUnityDemo.Contracts
42
{
5-
public class Product : IDataErrorInfo
3+
public class Product
64
{
75
public int ProductNumber { get; set; }
86
public string ProductName { get; set; }
9-
10-
public string Error
11-
{
12-
get
13-
{
14-
return this["ProductNumber"] + this["ProductName"];
15-
}
16-
}
17-
18-
public string this[string columnName]
19-
{
20-
get
21-
{
22-
switch (columnName)
23-
{
24-
case "ProductNumber":
25-
if (this.ProductNumber < 0)
26-
{
27-
return "Product number must not be negative.";
28-
}
29-
break;
30-
default:
31-
break;
32-
}
33-
34-
return string.Empty;
35-
}
36-
}
377
}
388
}

WpfTraining/PrismUnityDemo/PrismUnityDemo/Contracts/ProductSelectionChangedEvent.cs

-8
This file was deleted.

WpfTraining/PrismUnityDemo/PrismUnityDemo/Contracts/CloseProductEvent.cs WpfTraining/PrismUnityDemo/PrismUnityDemo/Contracts/PubSubEvents.cs

+4
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ namespace PrismUnityDemo.Contracts
55
public class CloseProductDetailEvent : PubSubEvent<IProductDetailViewModel>
66
{
77
}
8+
9+
public class ProductSelectionChangedEvent : PubSubEvent<Product>
10+
{
11+
}
812
}

WpfTraining/PrismUnityDemo/PrismUnityDemo/Contracts/Repository.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
namespace PrismUnityDemo.Contracts
55
{
6-
public abstract class Repository
6+
public interface IRepository
77
{
8-
public abstract IQueryable<Product> SelectAllProducts();
8+
IQueryable<Product> SelectAllProducts();
99
}
1010
}

WpfTraining/PrismUnityDemo/PrismUnityDemo/Data/MemoryRepository.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
namespace PrismUnityDemo.Data
55
{
6-
public class MemoryRepository : Repository
6+
public class MemoryRepository : IRepository
77
{
88
private Product[] sampleProducts = new[] {
99
new Product() { ProductNumber = 1, ProductName = "Twisted Drill" },
1010
new Product() { ProductNumber = 2, ProductName = "Indexable Drill" },
1111
new Product() { ProductNumber = 3, ProductName = "Slot Miller" }
1212
};
1313

14-
public override IQueryable<Product> SelectAllProducts()
14+
public IQueryable<Product> SelectAllProducts()
1515
{
1616
return this.sampleProducts.AsQueryable();
1717
}

WpfTraining/PrismUnityDemo/PrismUnityDemo/Data/RepositoryModule.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void Initialize()
1919
// Add some code to configure appropriate repository implementation
2020
var memRepo = new MemoryRepository();
2121

22-
this.container.RegisterInstance<Repository>(new MemoryRepository());
22+
this.container.RegisterInstance<IRepository>(new MemoryRepository());
2323
}
2424
}
2525
}

WpfTraining/PrismUnityDemo/PrismUnityDemo/PrismUnityDemo.csproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,10 @@
105105
<SubType>Code</SubType>
106106
</Compile>
107107
<Compile Include="Bootstrapper.cs" />
108-
<Compile Include="Contracts\CloseProductEvent.cs" />
108+
<Compile Include="Contracts\PubSubEvents.cs" />
109109
<Compile Include="Contracts\GlobalCommands.cs" />
110110
<Compile Include="Contracts\IProductDetailView.cs" />
111111
<Compile Include="Contracts\Product.cs" />
112-
<Compile Include="Contracts\ProductSelectionChangedEvent.cs" />
113112
<Compile Include="Contracts\Repository.cs" />
114113
<Compile Include="Modules\ProductMaintenanceModule.cs" />
115114
<Compile Include="Data\MemoryRepository.cs" />

WpfTraining/PrismUnityDemo/PrismUnityDemo/ViewModels/ProductDetailViewModel.cs

+38-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace PrismUnityDemo.ViewModel
1414
{
15-
public class ProductDetailViewModel : BindableBase, INavigationAware, IActiveAware, IProductDetailViewModel
15+
public class ProductDetailViewModel : BindableBase, INavigationAware, IActiveAware, IProductDetailViewModel, IDisposable
1616
{
1717
public ProductDetailViewModel()
1818
{
@@ -37,7 +37,7 @@ public Product Product
3737

3838
public IRegionManager SubRegionManager { get; private set; }
3939

40-
private Repository repository { get; set; }
40+
private IRepository repository { get; set; }
4141

4242
public bool IsNavigationTarget(NavigationContext navigationContext)
4343
{
@@ -83,7 +83,7 @@ public bool IsActive
8383

8484
[InjectionMethod]
8585
public void OnInitialization(IEventAggregator eventAggregator, GlobalCommands globalCommands,
86-
Repository repository)
86+
IRepository repository)
8787
{
8888
this.eventAggregator = eventAggregator;
8989
this.globalCommands = globalCommands;
@@ -93,5 +93,40 @@ public void OnInitialization(IEventAggregator eventAggregator, GlobalCommands gl
9393
this.globalCommands.PrintAll.RegisterCommand(this.PrintCommand);
9494
this.globalCommands.Close.RegisterCommand(this.CloseCommand);
9595
}
96+
97+
private bool disposed = false;
98+
99+
public void Dispose()
100+
{
101+
// TODO: Check this.disposed in all public, protected, and protected internal members!
102+
103+
this.Dispose(true);
104+
}
105+
106+
private void Dispose(bool disposing)
107+
{
108+
if (!this.disposed)
109+
{
110+
// Cleanup unmanaged resources here
111+
112+
// Cleanup commands
113+
this.globalCommands.Print.UnregisterCommand(this.PrintCommand);
114+
this.globalCommands.PrintAll.UnregisterCommand(this.PrintCommand);
115+
this.globalCommands.Close.UnregisterCommand(this.CloseCommand);
116+
117+
if (disposing)
118+
{
119+
// Cleanup managed resources
120+
}
121+
122+
GC.SuppressFinalize(this);
123+
this.disposed = true;
124+
}
125+
}
126+
127+
~ProductDetailViewModel()
128+
{
129+
this.Dispose(false);
130+
}
96131
}
97132
}

WpfTraining/PrismUnityDemo/PrismUnityDemo/ViewModels/ProductMaintenanceViewModel.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ namespace PrismUnityDemo.ViewModel
1111
{
1212
public class ProductMaintenanceViewModel : BindableBase
1313
{
14-
private readonly Repository repository = null;
14+
private readonly IRepository repository = null;
1515
private readonly DelegateCommand refreshProductListCommand = null;
1616

1717
//[Import]
1818
//private NavigationController navigationController = null;
1919

20-
public ProductMaintenanceViewModel(Repository repository, IEventAggregator eventAggregator)
20+
public ProductMaintenanceViewModel(IRepository repository, IEventAggregator eventAggregator)
2121
{
2222
this.repository = repository;
2323
this.eventAggregator = eventAggregator;

WpfTraining/PrismUnityDemo/PrismUnityDemo/Views/MainWindow.xaml

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
<!-- Note the use of auto-wiring of view model here. For more details
2-
see e.g. http://brianlagunas.com/getting-started-prisms-new-viewmodellocator/ -->
3-
<Window x:Class="PrismUnityDemo.Views.MainWindow"
1+
<Window x:Class="PrismUnityDemo.Views.MainWindow"
42
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
53
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
64
xmlns:prism="http://prismlibrary.com/"
75
xmlns:v="clr-namespace:PrismUnityDemo.Views"
8-
prism:ViewModelLocator.AutoWireViewModel="True"
9-
Title="{Binding Title}" Height="350" Width="525">
6+
Title="Simple Prism Sample" Height="350" Width="525">
107
<DockPanel>
118
<Border Margin="5" Background="Orange" Height="25"
129
CornerRadius="3" DockPanel.Dock="Top">

WpfTraining/PrismUnityDemo/PrismUnityDemo/Views/MainWindowNavigationController.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Prism.Events;
33
using Prism.Regions;
44
using PrismUnityDemo.Contracts;
5+
using PrismUnityDemo.ViewModel;
56
using PrismUnityDemo.Views;
67
using System;
78
using System.Linq;
@@ -20,7 +21,7 @@ public MainWindowNavigationController(IRegionManager regionManager, IUnityContai
2021
this.container = container;
2122
}
2223

23-
public /*override*/ void ActivateProductDetailViewInjection(Product product)
24+
public void ActivateProductDetailViewInjection(Product product)
2425
{
2526
var detailRegion = this.regionManager.Regions[RegionNames.DetailRegion];
2627
var existingView = detailRegion.Views
@@ -34,12 +35,11 @@ public MainWindowNavigationController(IRegionManager regionManager, IUnityContai
3435
else
3536
{
3637
var pdv = this.container.Resolve<ProductDetailView>();
37-
pdv.DataContext = product;
38+
((ProductDetailViewModel)pdv.DataContext).Product = product;
3839
var rm = detailRegion.Add(
3940
pdv,
4041
string.Format("ProductDetailView{0}", product.ProductNumber),
4142
true);
42-
var rm2 = RegionManager.GetRegionManager(pdv);
4343
detailRegion.Activate(pdv);
4444
}
4545
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
using Microsoft.Practices.Unity;
22
using PrismUnityDemo.ViewModel;
3+
using System;
34
using System.Windows.Controls;
45

56
namespace PrismUnityDemo.Views
67
{
7-
public partial class ProductDetailView : UserControl
8+
public partial class ProductDetailView : UserControl, IDisposable
89
{
910
private ProductDetailViewModel viewModel;
1011

1112
public ProductDetailView(IUnityContainer container)
1213
{
1314
this.InitializeComponent();
14-
this.DataContext = this.viewModel = container.Resolve<ProductDetailViewModel>();
15+
this.DataContext = this.viewModel = container.Resolve<ProductDetailViewModel>();
1516
}
16-
}
17+
18+
public void Dispose()
19+
{
20+
// TODO: Implement IDisposable correctly...
21+
22+
if (this.viewModel != null)
23+
{
24+
this.viewModel.Dispose();
25+
}
26+
}
27+
}
1728
}

0 commit comments

Comments
 (0)