Clearly stated assemblies for automatic registration

Our application uses Autofac as an IoC container and uses automatic registration for several assemblies containing widely used code.

To reference each assembly, you can usually use Assembly.GetAssembly(typeof(<type>))it using one of the types in the corresponding assembly. This, however, is not very clear, because it is not easy to see which assembly is registered through each type, and can also lead to errors when moving types between projects.

To solve this problem, in each assembly I created an empty class with a name Types, which, of course, has an assembly namespace, so I can do automatic registration as follows:

builder.RegisterAssemblyTypes(
    Assembly.GetAssembly(typeof(Xx.Common.Types)),
    Assembly.GetAssembly(typeof(Xx.Core.Types)),
    Assembly.GetAssembly(typeof(Xx.Infrastructure.Types)))
    .AsSelf()
    .AsImplementedInterfaces();

This, of course, is a "dead code" otherwise. Is it a good or smart idea to do it this way, or is it better, more idiomatic? Don't I really want to worry about it?

+4
source share
1 answer

I think what you are doing is actually pretty smart. The approach that I take is similar, but instead of creating a new type, I point to an existing type (like you), but in full the quality of its name looks like this:

typeof(global::Company.Product.BusinessLayer.ICommandHandler<>).Assembly

It is really clear that we focus on the business layer here, and if the type in question moves to another project, there will be a compilation error.

+3
source

Source: https://habr.com/ru/post/1546059/


All Articles