Skip to content
Michael Ahern edited this page Dec 25, 2013 · 13 revisions

Welcome to the IfInjector wiki. IfInjector is a micro-IoC injector intended for use in mobile (Xamarin /WP7.1+) environments. To simplify this usage the code is packaged as a single PCL for all environments.

The implementation is distinguished from other options by offering auto-wiring capabilities similar to a full IoC framework such as Ninjit, while weighing in at ~40 KB.

Supported Platforms

  • Mono
  • .Net 4.5
  • Silverlight
  • WP 7.1

Key Features

  • Auto-Wiring
  • Configuration through code
  • Configuration through attributes
  • Generics Support
  • Custom lifestyles
  • Fast (top performer on IoC Container Shootout)
  • Small

Quickstart

Getting the Library

The quickest way to get started is via the Nuget Package (http://www.nuget.org/packages/IfInjector). For Xamarin developers there is a nuget extension available by going to Tools > Add-in Manage, then searching for 'nuget'.

Alternatively you may clone and compile the source code yourself on github: https://github.com/iamahern/IfInjector.git.

CMD> Install-Package IfInjector

Wiring your components

IfInjector supports two ways to wire your components:

  1. Annotation based configuration
  2. Code based configuration You may use whichever method suits your development purposes.

Annotation Based Configuration

// =================================
// Setup
// =================================
using IfInjector;

[ImplementedBy(typeof(MyType))]
interface IMyType { }

interface IMyOtherType{}

[Singleton]
class MyType : IMyType {
  
   [Inject]
   public MyType(IMyOtherType myOtherType) {
      MyOtherType = myOtherType;
   }

   public IMyOtherType MyOtherType { get; private set; }

}

[Singleton]
class MyOtherType : IMyOtherType {}

// =================================
// Usage
// =================================
var injector = new Injector();

IMyType obj = injector.Resolve<IMyType>();

Code Based Configuration

// =================================
// Setup
// =================================
using IfInjector;

interface IMyType { }
interface IMyOtherType{}

class MyType : IMyType {
   // Constructor annotation is optional
   // - it will use this constructor since it is the only one available
   [Inject] 
   public MyType(IMyOtherType myOtherType) {
      MyOtherType = myOtherType;
   }

   public IMyOtherType MyOtherType { get; private set; }
}

class MyOtherType : IMyOtherType {}

// =================================
// Usage
// =================================
var injector = new Injector();

injector.Register(Binding.For<IMyType>().To<MyOtherType>().AsSingleton());
injector.Register(Binding.For<IMyOtherType>().To<MyOtherType>().AsSingleton());

IMyType obj = injector.Resolve<IMyType>();

Additional Details

TODO