Creating a Data Access layer using Unity

I am always pulling out the Unity assemblies. Maybe it is just because I am lazy, but I really can’t be bothered rolling my own dependency injection and mapping framework! I am going to use Unity only as a mapping frame work for now, I want to be able …

Offers

I am always pulling out the Unity assemblies. Maybe it is just because I am lazy, but I really can’t be bothered rolling my own dependency injection and mapping framework!

I am going to use Unity only as a mapping frame work for now, I want to be able to pass an Interface into a method, and get back the correct data access class.

The plan is to meet the diagram on the left. Only the factory and the interfaces are accessible, but are all set to “friend” which will require an assembly tag to be added to both the Factory and the Interfaces assemblies to allow explicit access.

This allows classes in the named assembly to access all classes and methods that have been prefaced with the “Friend” access level. Its a sneaky way of helping to maintain your layer integrity. So with this reference added only the Services.Server assembly, the one with the web service implementation, can access the factory and the interfaces, making them un-callable from any other assembly.

note: if you use only the assembly’s friendly name and not the explicit signed name then someone could just create an assembly of the same name as above and they will be able to call as if they were Services.Server. This is not to prevent hacking, just those pesky developers (me) who may try to bypass the service tier…

If, like me you are running tests you will also need to add an InternalsVisibleTo entry for your test assemblies or you will be plagued by “Assessors”..

First is my IDataAccess class, this is really only exists so there is some validation on my generics.

1

2

3

4

'Assembly:Hinshlabs.TfsDynamicPolicy.DataAccess.Common

PublicInterfaceIDataAccess

EndInterface

I also have a more specific Interface that allows for the loading of Artefacts. I have not yet implemented anything more than get and add, but you can see that it inherits (yes an interface and inherits in the same sentence) from the IDataAccess interface so we can pass it as a generic type that complies with IDataAccess.

1

2

3

4

5

6

7

8

9

10

11

12

'Assembly:Hinshlabs.TfsDynamicPolicy.DataAccess.Common

Imports Hinshlabs.TfsDynamicPolicy.Common

PublicInterfaceIArtifactDataAccess(OfTAsArtifact)

Inherits IDataAccess

FunctionGetArtifact(ByVal id AsGuid)AsT

FunctionGetArtifact(ByVal Uri AsUri)AsT

FunctionGetArtifacts()AsArtifactCollection

FunctionAddArtifact(ByVal artifact AsT)AsBoolean

EndInterface

Wow… that was hard, IDataAccess may have a little more than nothing in the future, but for now that is it. Now, what I need is a class that I can inherit from that will provide the functionality I need for any factory. This is mainly coz factories bread factories.

ThrowNewAccessViolationException(String.Format(CultureInfo.InvariantCulture,"There is no unity section called {0} in the config file {1}",UnityContainerName,ConfigFilename))

EndIf

section.Containers(UnityContainerName).Configure(m_UnityContainer)

EndSub

ProtectedReadOnly PropertyUnityContainer AsIUnityContainer

Get

Returnm_UnityContainer

EndGet

EndProperty

ProtectedMustOverride ReadOnly PropertyUnityContainerName AsString

ProtectedMustOverride ReadOnly PropertyConfigFilename AsString

EndClass

The UnityFactoryBase class provides a couple of features. It provides a Singleton pattern so that we make sure that there is only ever one instance per factory type T and we initialise a UnityContainer for each Instance of factory T. You can see that on the class definition the generic type T that will be passed in is of the same type as the class itself. This makes sure that the class that represents T inherits from UnityFactoryBase(Of T As {New, UnityFactoryBase(Of T)}). When configuring the Unity Container we get both the container name and the config file to load from the child class. This allows for both different config files per factory and the option to have a single config file and multiple unity configuration elements. Its up to you…

We need to inherit from UnityFactoryBase(Of T As {New, UnityFactoryBase(Of T)}) to create our factory and provide a config file to configure the UnityContainer. The inherited class is pretty simple as we have done most of the heavy lifting in the base class.

The DataAccessFactory provides the data needed by the UnityFactoryBase(Of DataAccessFactory) as well as the method that I need to retrieve an IDataAccess class. I will show you how it is called, but first there is the little matter of config:

I am not going to explain the config file as it is well (ish) documented, but it effectively mapped all generic calls to retrieve IArtifactDataAccess1 to the implementation in the XML assembly of ArtifactDataAccess1 and makes the resultant object a singleton.

To call this lovely package after you have populated it with many many DataAccess goodies all you need to do is this:

Lovely and simple, and to add a new implementation all I need is an Interface like IArtifactDataAccess.. lets call it IHypotheticalDataBits, a concrete implementation… lets call it HypoDataFromTwitter and a mapping in the config file:

Creating a Data Access layer using Unity was last modified: July 30th, 2009 by Martin Hinshelwood

-Every company deserves working software that successfully and consistently meets their customers needs on a regular cadence. We can help you get working software with continuous feedback so that your lean-agile teams can deliver continuous value with Visual Studio ALM, Team Foundation Server & Scrum. We have experts on hand to help improve your process and deliver more value at higher quality.