.Net package to automatically inject attribute decorated services into IOC Container
If you like or are using this project please give it a star. Thanks!
Using dotnet cli
dotnet add package Netjection --version 1.0.7
or package reference
<PackageReference Include="Netjection" Version="1.0.7" />
// Attribute without any parameter inject service as scoped
// and implementation type will be interface name without "I" prefix.
[Injectable]
public interface IUserService { }
public class UserService : IUserService { }
// You can specify service lifetime.
[Injectable(Lifetime.Singleton)]
public interface IProductService { }
public class ProductService : IProductService { }
// You can specify implementation type.
[Injectable(typeof(ConsoleLogger),Lifetime.Transient)]
public interface ICustomLogger { }
public class ConsoleLogger : ICustomLogger { }
Or scope specific attributes
// Attribute without any parameter inject service as scoped
// and implementation type will be interface name without "I" prefix.
[InjectAsSingleton]
public interface ISingletonService { }
public class SingletonService : ISingletonService { }
// You can specify implementation type.
[InjectAsScoped(typeof(ScopedServiceV2))]
public interface IScopedService { }
public class ScopedServiceV2 : IScopedService { }
[InjectAsTransient]
public interface ITransientService { }
public class TransientService : ITransientService { }
can be used with classes
[InjectAsSingleton]
public class DummyStorage
{
public string GetDummyText() => "Some Dummy Text";
}
Netjection can map configuration with classes and inject as singleton in DI container. it will use class name to search configuration section, but you can specify custom section name in attribute constructor.
"Options": {
"Url": "localhost",
"Port": 8080,
"UserName": "user",
"Password": "password"
}
[Configure] // or [Configure("CustomSectionName")]
public class Options
{
public string Url { get; set; }
public int Port { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
}
then you can inject in constructor in three ways shown below.
public class WhereOptionsAreInjected
{
private readonly Test _test;
private readonly Test _testAsOptionsValue;
private readonly Test _testAsOptionsSnapshotValue;
public WhereOptionsAreInjected(Test test,IOptions<Test> testOptions,
IOptionsSnapshot<Test> testOptionsSnapshot)
{
_test = test;
_testAsOptionsValue = testOptions.Value;
_testAsOptionsSnapshotValue = testOptionsSnapshot.Value;
}
}
and then use extension method on IServiceCollection to inject all service
using Netjection
public void ConfigureServices(IServiceCollection services)
{
services.InjectServices(Assembly.GetExecutingAssembly(),Assembly.GetAssembly(typeof(SingletonService)));
}
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
injection by VectorsLab from Noun Project