Learning PRISM – Unity Redux
If you missed the first post in this series, please read it first. I received quite a few emails in response too my first article… I would like to address them here:
What does unity have to do with PRISM?
I have to agree that my first post did not really put unity in context. So lets try again… Their are 2 main areas where Unity help… As I stated in the first post, you can call RegisterType<> on a type… What I did not mention is that you can also call RegisterInstance<>. This allows us to register a instance that can be used. Taking my first example of ILogger, if I only wanted 1 instance of ILogger to be passed to everyone that uses ILogger, I could have changed my code as follows:
container.RegisterInstance<ILogger>(new ConsoleLogger());
Now when I call resolve, I will always get the same instance!
ILogger logger = container.Resolve<ILogger>();
This is a very cool feature of unity (And IoC’s in general)… But how is it used in PRISM?
Regions and RegionManager will only be discussed later… but thy use this feature. For now you can think of a region as a place holder on the screen where I can “inject” a view and the RegionManager is my way of finding the regions!
IRegionManager manager = container.Resolve<IRegionManager>();
IRegion mainRegion = this.regionManager.Regions["MainRegion"];
By just having a reference to the container, I was able to get the RegionManager and from the RegionManager I got a region!
Other shared services that use this is: IContainerFacade, IEventAggregator & IModuleLoader
The next feature that can be used from unity is the fact that unity can be setup/configured from a simple config file!!!
Read more about this feature here
This makes your application even more loosely coupled!
Why do you use PRISM and not Composite Application Guidance?
PRISM just sounds better :)
More information about Unity Application Block & PRISM
http://msdn.microsoft.com/en-us/library/cc468366.aspx
http://www.codeplex.com/unity
http://www.codeplex.com/CompositeWPF
Next to be covered is modularity!