Skip to content

IDjinn/FastInject

Repository files navigation

FastInject

Easy and fast way to inject your services into DI container of your application.

How to use it

Every service you want to inject just needs to use the Inject attribute, with ServiceLifetime to insert into your DI container.

[Inject(ServiceLifetime.Singleton)]
public class SomeServie : ISomeService {
	// ...
}

Now just configure it at once in your DI.

services.InjectAllFromRootType(typeof(Program));

Example

[Inject(ServiceLifetime.Singleton)]
public class SomeServie : ISomeService {
	// ...
}

public static class DependencyInjector {
	public static IServiceCollection AddPresentation(this IServiceCollection services) {
		// All services flagged with the Inject attribute will be registered
		services.InjectAllFromRootType(typeof(DependencyInjector));
		// Now every service with Inject attribute in same namespace of DependencyInjector class will be inserted automatically into DI container.
		
		return services;
	}

}

About

C# DI injection by attributes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages