tech·nic·al·ly agile

My.Unity.Resolve(Of Ninja)

Explore integrating Unity Application Block in WPF and ASP.NET with custom solutions. Discover tips and code examples to enhance your development skills!

Published on
3 minute read
Image
https://nkdagility.com/resources/whj_VeMcGAL

I have been attempting to integrate the Unity Application Block into my website to allow me to share the same base object code between a WPF and an ASP.NET application. I will let you know how I am getting along later as I am still knee deep in refactoring, but I have found something a little useful.

I have created a custom “My” object in Visual Studio. This means that I can do “My.Unity.Container” to retrieve my UnityContainer object form anywhere is my code. I know it my be trivial to achieve in other ways, but I am a VB degenerate having fun, so leave me alone…

The first thing we need is a way of consistently creating a singleton instance of our UnityContainer across all of our code in the AppDomain.

 1Imports Microsoft.Practices.Unity
 2
 3Public Class UnityContainer
 4    Inherits Microsoft.Practices.Unity.UnityContainer
 5
 6    Private Shared sm_UnityContainer As Microsoft.Practices.Unity.UnityContainer
 7    Private Shared sm_syncRoot As New Object
 8
 9    Private Sub New()
10        MyBase.New()
11    End Sub
12
13    Public Shared ReadOnly Property Current() As Microsoft.Practices.Unity.UnityContainer
14        Get
15            If (sm_UnityContainer Is Nothing) Then
16                SyncLock sm_syncRoot
17                    If (sm_UnityContainer Is Nothing) Then
18                        sm_UnityContainer = New UnityContainer
19                        'Dim section As UnityConfigurationSection = ConfigurationManager.GetSection("unity")
20                        'section.Containers.Default.Configure(sm_UnityContainer)
21                    End If
22                End SyncLock
23            End If
24            Return sm_UnityContainer
25        End Get
26    End Property
27
28End Class

I have commented the lines out, but you could also initialise the Unity Container from a config file, but remember that it will be the top level config of your application root an not the config from the Assembly that you code happens to be in.

We could just leave it at that, and If you use C# this is about your lot, but in VB you have the “My” namespace that gives you access to some useful things all in one place.

My.Unity.Resolve(Of Ninja)  

In order to achieve this you need to create a Module in the “My” namespace that has a single property that access the previous class.

 1Imports Common
 2
 3Namespace My
 4
 5  <HideModuleName()> _
 6  Module MyUnityExtensions
 7
 8        Friend ReadOnly Property Unity() As UnityContainer
 9            Get
10                Return UnityContainer.Current
11            End Get
12        End Property
13
14    End Module
15
16End Namespace

This the allows you to access the UnityContainer object in the same way that you would access My.User.

My.Unity.Resolve(Of Ninja)  

Inside the Unity object you will have all of the shared properties and methods that we created earlier.

My.Unity.Resolve(Of Ninja)  

To examine the use of this I have followed O1eg Smirnov  ’s Ninja Dependency Injection  scenario.

So, I have a Console application that registers a sword type and the resolves out a Ninja Object using Unity.

 1Imports NinjaCommon
 2
 3Module Module1
 4
 5    Sub Main()
 6
 7        ' Register Weapon
 8        My.Unity.RegisterType(Of IWeapon, Sword)()
 9        'Create Ninja
10        Dim ninja = My.Unity.Resolve(Of Ninja)()
11        ' Ninja uses weapon
12        ninja.Weapon.use()
13        ' Create Battle
14        Dim battle As New NinjaClasses.Battle
15        ' Start the fight
16        battle.StartFight()
17
18        Console.ReadKey()
19    End Sub
20
21End Module

But it then instantiates a Battle class from the NinjaClasses assembly which uses it own My.Unity class to access the same UnityContainer object.

 1Public Class Battle
 2
 3    Public Sub New()
 4
 5    End Sub
 6
 7    Public Sub StartFight()
 8        Console.WriteLine("Fight Starting ")
 9        ' Create 10 ninjas and get them to use their weapon.
10        For count = 1 To 10
11            Dim ninja = My.Unity.Resolve(Of NinjaCommon.Ninja)()
12            ninja.Weapon.use()
13        Next
14        Console.WriteLine("Fight ended")
15    End Sub
16
17End Class

Although this example in no way demonstrates the power of the Unity Application Block, and is a bit silly, I think it demonstartes the use of the “My” namespace.

Technorati Tags: .NET    Software Development    CodeProject    WPF 

Software Development
Comments

Related blog posts

No related videos found.

Connect with Martin Hinshelwood

If you've made it this far, it's worth connecting with our principal consultant and coach, Martin Hinshelwood, for a 30-minute 'ask me anything' call.

Our Happy Clients​

We partner with businesses across diverse industries, including finance, insurance, healthcare, pharmaceuticals, technology, engineering, transportation, hospitality, entertainment, legal, government, and military sectors.​

Healthgrades Logo
Teleplan Logo
Hubtel Ghana Logo
Flowmaster (a Mentor Graphics Company) Logo
Big Data for Humans Logo
Lockheed Martin Logo
Jack Links Logo
Brandes Investment Partners L.P. Logo
Trayport Logo
New Signature Logo
Ericson Logo

CR2

Boxit Document Solutions Logo
Capita Secure Information Solutions Ltd Logo
Genus Breeding Ltd Logo

NIT A/S

DFDS Logo
Boeing Logo
Department of Work and Pensions (UK) Logo
New Hampshire Supreme Court Logo
Washington Department of Transport Logo
Nottingham County Council Logo
Washington Department of Enterprise Services Logo
Royal Air Force Logo
Deliotte Logo
Bistech Logo
Xceptor - Process and Data Automation Logo
Workday Logo

CR2

Philips Logo