Registering types

public interface IMyService {}
public class MyService1 : IMyService {}
public class MyService2 : IMyService {}

You can only register types that can be instantiated and that have a public constructor:

var repo = new ServiceRepository();

repo.Register<MyService1>();

var provider = repo.CreateServiceProvider();

When an object is retrieved from the service repository, a new object of the type will be created each time:

var svc1 = provider.Get<IMyService>();
var svc2 = provider.Get<IMyService>();
var svc3 = provider.Get<MyService1>();

// svc1 = instance 1 of MyService1
// svc2 = instance 2 of MyService1 (different from svc1)
// svc3 = instance 3 of MyService1 (different from svc1 and svc2)

Note that if two types are registered that share the same interface or base type, the last one that was registered will resolve:

repo.Register<MyService1>();
repo.Register<MyService2>();
var svc1 = provider.Get<IMyService>();
var svc2 = provider.Get<IMyService>();
var svc3 = provider.Get<MyService1>();
var svc4 = provider.Get<MyService2>();

// svc1 = instance 1 of MyService2
// svc2 = instance 2 of MyService2 (different from svc1)
// svc3 = instance of MyService1
// svc4 = instance 3 of MyService2 (different from svc1 and svc2)

Specifying the registration type

By default, when a type is registered, it will resolve as the type itself, any base type and all interfaces it implements

public interface IService1 {}
public interface IServive2 {}
public class MyBaseService : IService1 {}
public class MyService : MyBaseService, IService2 {}
Registering a type without specifying the registration type
repo.Register<MyService>();
provider.Get<IService1>(); // succeeds, returns instance of MyService
provider.Get<IService2>(); // succeeds, returns instance of MyService
provider.Get<MyBaseService>(); // succeeds, returns instance of MyService
provider.Get<MyService>(); // succeeds, returns instance of MyService
Registering a type as a specific registration type
repo.Register<MyService>().As<IService1>();
provider.Get<IService1>(); // succeeds, returns instance of MyService
provider.Get<IService2>(); // returns null
provider.Get<MyBaseService>(); // returns null
provider.Get<MyService>(); // returns null
Registering a type as multiple registration types
repo.Register<MyService>().As<IService1>().AsSelf();
provider.Get<IService1>(); // succeeds, returns instance of MyService
provider.Get<IService2>(); // returns null
provider.Get<MyBaseService>(); // returns null
provider.Get<MyService>(); // succeeds, returns instance of MyService