Saturday, December 6, 2014

InjectionMap.Wpf: Extension for InjectionMap

InjectionMap.Wpf is a extension to InjectionMap that allows to define the ViewModel in the xaml of the View. InjectionMap will resolve the ViewModel and bind it to the DataContext of the View. All parameters of the constructor can be resolved using ConstructorInjection.

Create a ViewModel. A class that will be added to the DataContext
namespace ViewModels
{
 public class ViewModel : INotifyPropertyChanged
 {
  public ViewModel(ISettings settings)
  {
    ...
  }
 }
}

Map the dependencies

using(var mapper = new InjectionMapper())
{
      mapper.Map<ISettings, Settings>();
}

Bind the ViewModel to the View

<Window x:Class="MainWindow"
 xmlns:wf="http://schemas.wickedflame.ch/2013/xaml/presentation"
        xmlns:vm="clr-namespace:ViewModels"
        wf:InjectionResolver.Resolve="{x:Type vm:ViewModel}">
 <!-- Optional Namespace for InjectionMap.Wpf: xmlns:wf="clr-namespace:InjectionMap;assembly=personalplaner.common" -->
...
</Window>
The ViewModel will be resolved and bound to the DataContext of the UserControl/Window.

Tuesday, September 2, 2014

InjectionMap.Configuration: Extension for InjectionMap

InjectionMap.Configuration is a small extension to InjectionMap that allows to register the mappings in the application configuration file.


Some simple types to demonstrate the mapping:
public interface IKeyOne { }
public class ObjectTypeOne : IKeyOne { }

public class InjectionMapInitializer : IMapInitializer
{
    public void InitializeMap(IMappingProvider container)
    {
        container.Map<IKeyThree, ObjectTypeThree>();
    }
}

Define the mappings in the config file.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!-- Define the section -->
    <section name="injectionMap" type="InjectionMap.Configuration.InjectionMapSection, InjectionMap.Configuration" />
  </configSections>

  <injectionMap>
    <mappings>
      <!-- Register Map IKeyOne to ObjectTypeOne -->
      <map key="TestApp.IKeyOne, TestApp" for="TestApp.ObjectTypeOne, TestApp"/>
      <!-- Register Map ObjectTypeOne to self -->
      <map key="TestApp.ObjectTypeOne, TestApp" toSelf="true"/>
    </mappings>
    <initializers>
      <!-- Register MapInitializers -->
      <init type="TestApp.InjectionMapInitializer, TestApp"/>
    </initializers>
  </injectionMap>
</configuration>
</pre>

The Section has to be called "injectionMap" for InjectionMap to find it.
Import the namespace InjectionMap.Configuration and create a instance of InjectionMapper. To initialize the configuration just call the extensionmethod Initialize() on InjectionMapper. This call should only be made once per AppDomain, ideally in the application startup.

using InjectionMap.Configuration;
...

using (var mapper = new InjectionMapper())
{
    mapper.Initialize();
}

InjectionMap.Configuration can be downloaded from NuGet
PM > Install-Package InjectionMap.Configuration

The sourcecode is on Github

Thursday, May 29, 2014

Inject parameters into unmapped Types using InjectionMap

InjectionMap lets you easily resolve mapped types and automatically injects the parameters into the constructor. This helps to resolve types that depend upon constructors that contain parameters.

In this example there there is a type named MappedTyp that has a constructor that accepts a type of ITypeArgument.
public interface ITypeArgument
{
}

public class TypeArgument : ITypeArgument
{
}

public interface IMappedType
{
}

public class MappedType : IMappedType
{
   public UnmappedType(ITypeArgument argument) { }
}

InjectionMap can resolve MappedType when both ITypeArgument and IMappedType are mapped into InjectionMap.
using (var mapper = new InjectionMapper())
{
   mapper.Map<ITypeArgument, TypeArgument>();
   mapper.Map<IMappedType, MappedType>();
}

using (var resolver = new InjectionResolver())
{
   var map = resolver.Resolve<IMappedType>();
}

This will automatically create a instance of MappedTyp and inject a resolved instance of ITypeArgument into the constructor.

Optionaly the parameter can be injected manually. Simply create an instance of UnmappedType and inject TypeArgument by resolving ITypeArgument with InjectionMap.
public interface ITypeArgument
{
}

public class TypeArgument : ITypeArgument
{
}

public class UnmappedType
{
   public UnmappedType(ITypeArgument argument) { }
}

using (var mapper = new InjectionMapper())
{
   mapper.Map<ITypeArgument, TypeArgument>();
}

using (var resolver = new InjectionResolver())
{
   var map = new UnmappedType(resolver.Resolve<ITypeArgument>());
}

Since the version 1.4.3 InjectionMap can also resolve unmapped types and inject the parameters based on the registered maps. Simply create a mapping of ITypeArgument and resolve UnmappedType without mapping it in InjectionMap. InjectionMap creates an instance of UnmappedType and injects TypeArgument by resolving ITypeArgument.

public interface ITypeArgument
{
}

public class TypeArgument : ITypeArgument
{
}

public class UnmappedType
{
   public UnmappedType(ITypeArgument argument) { }
}

using (var mapper = new InjectionMapper())
{
   mapper.Map<ITypeArgument, TypeArgument>();
}

using (var resolver = new InjectionResolver())
{
   var map = resolver.Resolve<UnmappedType>();
}

There is no need to create a new instance of UnmappedType or to register it to the InjectionMap. The Parameters will automatically be resolved and injected.