Register Type

There are various ways to use Register. Let's take the following complex type as an example.

class ServiceA : IServiceA, IInputPort, IDisposable { /* ... */ }

Register Concrete Type#

builder.Register<ServiceA>(Lifetime.Singleton);

It can resolve like this:

class ClassA
{
public ClassA(ServiceA serviceA) { /* ... */ }
}

Register as Interface#

builder.Register<IServiceA, ServiceA>();

It can resolve like this:

class ClassA
{
public ClassA(IServiceA serviceA) { /* ... */ }
}

Register as multiple Interface#

builder.Register<ServiceA>(Lifetime.Singleton)
.As<IServiceA, IInputPort>();

It can resolve like this:

class ClassA
{
public ClassA(IServiceA serviceA) { /* ... */ }
}
class ClassB
{
public ClassB(IInputPort handlerA) { /* ... */ }
}

Register all implemented interfaces automatically#

builder.Register<ServiceA>(Lifetime.Singleton)
.AsImplementedInterfaces();

It can resolve like this:

class ClassA
{
public ClassA(IServiceA serviceA) { /* ... */ }
}
class ClassB
{
public ClassB(IHandlerB handlerA) { /* ... */ }
}

Register all implemented interfaces and concrete type#

builder.Register<ServiceA>(Lifetime.Singleton)
.AsImplementedInterfaces()
.AsSelf();

It can resolve like this:

class ClassA
{
public ClassA(IServiceA serviceA) { /* ... */ }
}
class ClassB
{
public ClassB(IHandlerB handlerA) { /* ... */ }
}
class ClassB
{
public ClassB(ServiceA serviceA) { /* ... */ }
}

Register lifecycle marker interfaces#

class GameController : IStartable, ITickable, IDisposable { /* ... */ }
builder.RegisterEntryPoint<GameController>(Lifetime.Singleton);
note

this is similar to Register<GameController>(Lifetime.Singleton).AsImplementedInterfaces()

If you have multiple EntryPoints, you have the option to use the following declaration as grouping.

builder.UseEntryPoints(Lifetime.Scoped, entryPoints =>
{
entryPoints.Add<ScopedEntryPointA>();
entryPoints.Add<ScopedEntryPointB>();
entryPoints.Add<ScopedEntryPointC>().AsSelf();
});

This is the same as:

builder.RegisterEntryPoint<ScopedEntryPointA>(Lifetime.Scoped);
builder.RegisterEntryPoint<ScopedEntryPointB>(Lifetime.Scoped);
builder.RegisterEntryPoint<ScopedEntryPointC>(Lifetime.Scoped).AsSelf();

Register instance#

// ...
var obj = new ServiceA();
// ...
builder.RegisterInstance(obj);
note

RegisterIntance always Scoped lifetime. So it has no arguments.

It can resolve like this:

class ClassA
{
public ClassA(ServiceA serviceA) { /* ... */ }
}

Register instance as interface#

Allow As* decrelations.

builder.RegisterInstance<IInputPort>(serviceA);
builder.RegisterInstance(serviceA)
.As<IServiceA, IInputPort>();
builder.RegisterInstance()
.AsImplementedInterfaces();

Register type-specific parameters#

If the types are not unique, but you have a dependency you want to inject at startup, you can use below:

builder.Register<SomeService>(lifetime.Singleton)
.WithParameter<string>("http://example.com");

Or, You can parameter name as a key.

builder.Register<SomeService>(Lifetime.Singleton)
.WithParameter("url", "http://example.com");

It can resolve like this:

class SomeService
{
public SomeService(string url) { /* ... */ }
}

This Register is with only SomeService.

class OtherClass
{
// ! Error
public OtherClass(string hogehoge) { /* ... */ }
}