A simple yet effective auto registration tool for dotnet
Doesn't support automatic registration of generic types, you will have to register them manually.
Registrator.Net is a simple auto registration tool for dotnet.
I have been trying to find a simple tool to register my internal dependencies and didn't find any that I liked, so I decided to create my own.
Registrator.Net is intended for developers who want something simple that just works.
It is not designed to deal with every case, but the simple ones, the ones you will be doing 99% of the time.
Install-Package Registrator.Net
Tag classes, records and structs with any of the following attributes:
- AutoRegisterType
- AutoRegisterTypeAndInterfaces
- AutoRegisterInterfaces
Then in your Program.cs or Startup.cs, call services.AutoRegisterTypesInAssemblies(assembly1,assembly2,assembly3...);
If you want to skip the registration of types that implement a certain interface from a certain assembly, you can call
services.AutoRegisterTypesInAssemblies(new RegistratorConfiguration()
{
Assemblies = [typeof(ConcreteType).Assembly],
ExcludedAssemblies = [typeof(IRequestHandler<>).Assembly,typeof(IMediator).Assembly]
});By default all registered types are registered as ServiceLifetime.Scoped, but you can change it by passing a ServiceLifetime as a parameter of the attribute.
You can also add keyed services if you use the Key property of the attribute.
[AutoRegisterInterfaces(ServiceLifetime.Scoped, "17")]
public class ConcreteType17 : IInterface25, IInterface26 { }In the above the interfaces of the class with be resolved to ConcreteType17 when requested with the key 17
You can tag your types with the tag property of the attributes. If a tag is provided, the type will be registered only if the tag is null or present in the RegistratorConfiguration.Tags array or if the tag is null.
Example
[AutoRegisterTypeAndInterfaces(ServiceLifetime.Transient, tag: "tag1")]
public class ConcreteTypeWithMatchingTags : IInterfaceAndTypeWithMatchingTags { }
[AutoRegisterTypeAndInterfaces(ServiceLifetime.Transient, tag: "tag2")]
public class ConcreteTypeWithNoMatchingTags : IInterfaceAndTypeWithNoMatchingTags { }
[AutoRegisterTypeAndInterfaces(ServiceLifetime.Transient)]
public class ConcreteTypeWithNoTags : IInterfaceAndTypeWithNoTags { }
...
services.AutoRegisterTypesInAssemblies(c =>
{
c.Assemblies = [typeof(ConcreteType).Assembly];
c.Tags = ["tag1"];
});In the above example, only ConcreteTypeWithMatchingTags and ConcreteTypeWithNoTags will be registered.
You can order the registration of your types by using the order property of the attributes. The types will be registered in ascending order.
Example:
[AutoRegisterType(ServiceLifetime.Transient, order: 1)]
public class ConcreteTypeWithOrder1 { }
[AutoRegisterType(ServiceLifetime.Transient, order: 2)]
public class ConcreteTypeWithOrder2 { }
[AutoRegisterType(ServiceLifetime.Transient, order: 3)]
public class ConcreteTypeWithOrder3 { }You can exclude types from registration by namespace by using the ExcludedTypesNamespaces property of the RegistratorConfiguration class.
The decision to use Types to discover the namespace was made to avoid the use of strings and make it more type safe.
Example:
services.AutoRegisterTypesInAssemblies(c =>
{
c.Assemblies = [typeof(ConcreteType).Assembly];
c.ExcludedTypesNamespaces = [typeof(ClassToExclude)];
});In the above example, all types sharing the namespace of ClassToExclude will be excluded from registration.
Logo Provided by Vecteezy