Project Description
MicroSliver is a micro, non-bloat, non-intimidating and speedy .NET (ASP.NET, Silverlight, RIA Services, WPF, Forms and Metro!) dependency injection container which has just the essentials.

Supports Metro .NET (WinRT)

Contains extension for Silverlight and Metro MVVM DI support.

Contains extension for RIA Services Domain Service DI support.

Contains extension for ASP.NET MVC Controller DI support.

Tired of large DI frameworks that require a significant amount of knowledge in order to understand or build on top of?  Look no further...

Available on NuGet!

SignalR Dependency Resolver available on NuGet and GitHub!

Features

  • Fast!  Early bench marking has it crushing other DI/IoC containers!
  • Fluid API!  No annoying configuration files here.
  • Compact!  Release build DLL is bite-size < 15KB.
  • No Bloat!  Contains the bare essentials:  Constructor DI &  3 Life Cycle Options!
  • Simple to Use!  Instantiate the container, add your mappings and you're off!
  • .NET!  Built using .NET 4.0.
  • Metro .NET! Supports WinRT!
  • Silverlight and Metro MVVM Support!  Extension to inject ViewModels!
  • RIA Services Support! Extension to inject custom domain services!
  • ASP.NET MVC Support!  Extension to inject controllers!
  • SignalR Support! Dependency Resolver to inject hubs!

How to use?  Simple!

MicroSliver requires that objects which are injected (without creator) have a single public constructor without primitive/value types. 

In the example below, 2 different interfaces are mapped to concrete classes.  The life cycle associated with the first mapping is subsequently instantiated each time whereas with the second mapping (Singleton) it is only created once.  A call is then made to the container, Get<T>(), that will instantiate and/or inject the object based on the type and mappings.  Not shown is the ability to limit scope by web request (ToRequestScope()).

IIoC ioc = new IoC();
// Every time an object of type IContract is requested, it will return a new instance of type Concrete
ioc.Map<IContract, Concrete>();
// When an object of type ISingletonContract is requested, if an instance already exists, it'll return that otherwise return a new instance
ioc.Map<ISingletonContract, SingletonConcrete>().ToSingletonScope();

// The IoC container will instantiate & inject any interfaces in the constructor of ClassA based on the mappings
var classA = ioc.Get<ClassA>();
// Fetch an implementation for IContract
var concrete = ioc.Get<IContract>();
// Fetch an implementation for ISingletonContract
var concreteSingleton = ioc.Get<ISingletonContract>();
...
public class ClassA {
    public ClassA(IContract contract, ISingletonContract singletonContract){...}
}

You also have the ability to control object instantiation by providing a Creator.

// Every time an object of type IContract is requested, it will make a call to your creator.  You do have the ability to make it a singleton.
ioc.Map<IContract>(new ContractCreator());

var classA = ioc.Get<ClassA>();

public class ContractCreator : ICreator
{
    public object Create()
    {
        return new Concrete();
    }
}

How to use with Silverlight or Metro .NET (WinRT)?  Simple!

Create a class that acts as a ViewModelLocator (Service Locator pattern - be aware of cons).  (See below for additional Silverlight or Metro extension.)

public class ViewModelLocator
{
    public static IIoC ioc;
    public  ViewModelLocator()
    {
        if(ioc == null)
        {
           ioc = new IoC();
           ioc.Map<IInterfaceA, ClassA>();
           //Add rest of mappings
        }
     }
     
    public MyViewModel myViewModel
    {
        get
        {
            return ioc.Get<MyViewModel>();
         }
     }
}

Add a reference in the Application.Resources for the App.xaml. 

... 

<Application.Resources>
        <vl:ViewModelLocator x:Key="viewModelLocator"/>
</Application.Resources>

...

For each view that binds to a ViewModel class, let the ViewModelLocator handle the instantiation (and DI) of your ViewModel.

...

<UserControl DataContext="{Binding myViewModel, Source={StaticResource viewModelLocator}}">

...

<TextBlock Text="{Binding Path=UserFirstName, Mode=TwoWay}"></TextBlock>

How to use RIA Services extension?  Simple!

In the Application_Start of your Global.asax, instantiate your IoC Container, add your mappings and set the DomainService.Factory to an instance of MicroSliverDomainFactory.  You are now able to use dependency injection with your RIA Servicves domain services.

using System.ServiceModel.DomainServices.Server;
...
public class Global : HttpApplication
{
    ...        
    protected void Application_Start(object sender, EventArgs e)
    {
        
        var ioc = new IoC();

        ioc.Map<IContract, Concrete>().ToRequestScope();
        // Add rest of mappings
        DomainService.Factory = new MicroSliver.Web.Extensions.MicroSliverDomainFactory(ioc);
        
    }
    ...
}

How to use ASP.NET MVC extension? Simple!

In your Global, inherit from the MicroSliverHttpApplication and override the LoadIIoC & Application_Start methods.  Within the LoadIIoC instantiate your IoC container and add your mappings.  You are now able to use dependency injection with your controllers.

public class MvcApplication : MicroSliver.Web.Extensions.MicroSliverHttpApplication
{

    protected override IIoC LoadIIoC()
    {
        var ioc = new IoC();

        ioc.Map<IContract, Concrete>().ToRequestScope();
        // Add rest of mappings
        return ioc;
    }

    protected override void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new { controller = @"[^\.]*" }
        );

    }
}

 How to use Silverlight or Metro .NET (WinRT) extension?  Simple!

Create a class that inherits from the MicroSliverViewModelLocator (namespace: MicroSliver.Extensions.Metro or MicroSliver.Extensions.Silverlight), add your mappings and make a call to LoadIoC in the Base Class.

public class ViewModelLocator : MicroSliverViewModelLocator
{
    public  ViewModelLocator()
    {
        IIoC ioc = new IoC();
        ioc.Map<IInterfaceA, ClassA>();
        //Add rest of mappings

        //Use this call for Silverlight
        LoadIoC(ioc, Assembly.GetExecutingAssembly());

        //Use this call for Metro
        LoadIoC(ioc, this.GetType().GetTypeInfo().Assembly);

     }
}

Add a reference in the Application.Resources for the App.xaml. 

..

<Application.Resources>
        <vl:ViewModelLocator x:Key="viewModelLocator"/>
</Application.Resources>

..

For each view that binds to a ViewModel class, let the ViewModelLocator handle the instantiation (and DI) of your ViewModel.  Note that brackets are required since it is doing an indexer binding and that *full* namespace of the ViewModel is required for instantiation.

...

<UserControl DataContext="{Binding [FullNameSpace.UserViewModel], Source={StaticResource viewModelLocator}}">

...

<TextBlock Text="{Binding Path=UserFirstName, Mode=TwoWay}"></TextBlock>

How to use SignalR extension? Simple!

In your Global, instantiate your IoC container and add your mappings.  To set MicroSliver as a custom SignalR dependency resolver, simply instantiate MicroSliverDependencyResolver, pass in your IoC container and set GlobalHost.DependencyResolver to it.  Your hubs, for example, are ready to be injected!

var ioc = new IoC();
ioc.Map<IContract, Concrete>();
// Add rest of mappings

var resolver = new MicroSliverDependencyResolver(ioc);
GlobalHost.DependencyResolver = resolver;
RouteTable.Routes.MapHubs();

Follow Jeremiah Billmann on Twitter: @JBillmann

Last edited Jun 21, 2012 at 7:19 PM by jbillmann, version 88