Skip to content

wipiano/static-dependency-injection

Repository files navigation

StaticDependencyInjection

これは ASP.NET Core の DI をより簡単にするためのライブラリです. (TODO: 英語への翻訳)

Usage

DI に登録したい型に Attribute をつけます.

[SingletonService]
public class FooServiceImpl : IFooService
{
    // ...
}

public interface IBarService
{
    // ...
}

[ScopedServiceAs(typeof(IBarService))]
public class DefaultBarService : IBarService
{
    // ...
}

[TransientServiceWith(typeof(FooServiceImpl))]
public interface IFooService
{
    // ...
}

このあと, AddStaticServices 拡張メソッドを呼び出します.

serviceCollection.AddStaticServices(Assembly.GetAssembly(typeof(IFooService)));

この呼び出しによって,下記と等価な処理が実行されます.

serviceCollection.TryAddSingleton<FooServiceImpl>();
serviceCollection.TryAddScoped<IBarService, DefaultBarService>();
serviceCollection.TryAddTransient<IFooService, FooServiceImpl>();

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages