Using Unity Automapper, Step-by-step


Although it’s pretty self-explanatory, I thought that some simple instructions how to use the Unity Automapper might be a good idea anyway.

Installation

If you’re reading this, there’s a good chance you’re coming here from the link in NuGet’s description, in which case you can skip straight to the Usage section below. Otherwise, read on…

  1. Fire up Visual Studio and open your solution.
  2. Open NuGet on the project you wish to perform the Unity registrations on (typically this will be your startup project)
  3. Search for Unity Automapper. NuGet isn’t the best at finding packages so you might have to search for it a bit.
    image
  4. Alternatively you can simply install it via the Package Manager Console with the command.image
  5. That’s it! You’re now ready to start using the AutoMapper!

Usage

This is even easier.

  1. Add a using statement to the top of your source file where you want to perform the registration on.image
  2. Create your Unity Container as normal.
  3. If you have a list of types at compile-time that you wish to use as the source of registrations, simply call the AutomapTypes() method: –image
  4. Alternatively you could just chuck in all the types in e.g. your executing assembly at run-time: –image
  5. If, on the other hand you’re using a more plug-in style architecture where e.g. you want to source your registrations from assemblies that your registration project doesn’t have a reference to, or you want to map concretes that are internal to another assembly etc., you can just provide the assembly names using the AutomapAssemblies() method: –image
  6. So in this example, perhaps we have an interface in our Services assembly e.g. IDataAccess, which is implemented by a class in DataAccess. You can now make the concrete implementation completely private; it’ll simply be found by the mapper and wired up.

That’s it! Either of those calls will perform any mappings that it can find into Unity; you’re now ready to go.

Other features

Here are a few other features that the mapper offers…

  1. If you want to make your data access layer a singleton in Unity, simply mark the interface (not the implementation!) with the [Singleton] attribute (this necessitates referencing the Automapper from that assembly as well).image
  2. If you want to specifically ignore a type from the Automapper – perhaps you have a fake class you sometimes turn on or use in testing etc. rather than when really running – just mark it with the [DoNotMap] attribute.image

That’s pretty much it. Two method calls and two (optional) attributes – not a massive API but hopefully one that saves you some time when using Unity.

2 thoughts on “Using Unity Automapper, Step-by-step

    1. I did actually look at them (albeit somewhat briefly). What I wanted to achieve with this is something that is a very simple to use API that doesn’t auto map everything but gets at least 80% of your wireup done. Look at the size of those APIs compared to mine – undoubtedly they offer more but I want something that’s more accessible, particularly for someone that’s a beginner to using DI etc.. I’ve got lots if ideas for how to make it more powerful but am really trying to keep the API dumb.

Leave a reply to Isaac Abraham Cancel reply