@@ -7,20 +7,41 @@ Microsoft.Extensions.DependencyInjection dependency injection provider.
77
88I have written a simple version of AutoFac's ` RegisterAssemblyTypes `
99method that works directly with Microsoft's DI provider.
10- Here is an example of me using this with ASP.NET Core
10+ Here are an example of me using this with ASP.NET Core
11+
12+ #### Example 1 - scan the calling assembly
1113
1214``` c#
1315public void ConfigureServices (IServiceCollection services )
1416{
1517 // ... other configure code removed
1618
17- var assemblyToScan = Assembly .GetExecutingAssembly (); // ..or whatever assembly you need
18-
19- service .RegisterAssemblyPublicNonGenericClasses (assemblyToScan )
19+ service .RegisterAssemblyPublicNonGenericClasses ()
2020 .Where (c => c .Name .EndsWith (" Service" ))
2121 .AsPublicImplementedInterfaces ();
2222```
2323
24+
25+ #### Example 2 - scaning multiple assemblies
26+
27+ ```c #
28+ public void ConfigureServices (IServiceCollection services )
29+ {
30+ // ... other configure code removed
31+
32+ var assembliesToScan = new []
33+ {
34+ Assembly .GetExecutingAssembly (),
35+ Assembly .GetAssembly (typeof (MyServiceInAssembly1 )),
36+ Assembly .GetAssembly (typeof (MyServiceInAssembly2 ))
37+ };
38+
39+ service .RegisterAssemblyPublicNonGenericClasses (assembliesToScan )
40+ .Where (c => c .Name .EndsWith (" Service" ))
41+ .AsPublicImplementedInterfaces ();
42+ ```
43+
44+
2445Licence : MIT .
2546
2647** See [this article ](https :// www.thereformedprogrammer.net/asp-net-core-fast-and-automatic-dependency-injection-setup/)
@@ -52,29 +73,24 @@ registers those interfaces as pointing to the class.
5273
5374#### 1. The `RegisterAssemblyPublicNonGenericClasses` method
5475
55- The `RegisterAssemblyPublicNonGenericClasses ` method will find all the classes
56- in the assembly that I referenced that are considered useful for registering .
57- The exact criteria I use are :
76+ The `RegisterAssemblyPublicNonGenericClasses ` method will find all the classes in
77+
78+ 1 . If no assemblies are provided then it scans the assembly that called this method .
79+ 2 . You can provide one or more assemblies to be scanned . The easiest way to reference an assembly is to use something like this `Assembly .GetAssembly (typeof (MyService ))`, which gets the assembly that `MyService ` was defined in .
80+
81+ I only consider classes which match ALL of the critera below :
5882
5983- Public access
6084- Not nested , e .g . It won 't look at classes defined inside other classes
6185- Not Generic , e .g . MyClass \< T \>
6286- Not Abstract
6387
64- The method takes a list / array of assemblies to scan . Two typical ways of providing an assembly are :
65-
66- - `Assembly .GetExecutingAssembly ()`, which does what is says
67- - `Assembly .GetAssembly (typeof (YourClass ))`, which gets the assembly that `YourClass ` was defined in .
6888
6989#### 2. The `Where` method
7090
71- Pretty straightforward - you are provided with the `Type ` of each class and
72- you can filter by any of the `Type ` properties etc . This allows you to
73- do things like only registering certain classes ,
74- e .g `Where (c => c .Name .EndsWith (" Service" ))`
91+ Pretty straightforward - you are provided with the `Type ` of each class and you can filter by any of the `Type ` properties etc . This allows you to do things like only registering certain classes , e .g `Where (c => c .Name .EndsWith (" Service" ))`
7592
76- * NOTE : Useful also if you want to register some classes with a different timetime scope -
77- See next section .*
93+ * NOTE : Useful also if you want to register some classes with a different timetime scope - See next section .*
7894
7995#### 3. The `AsPublicImplementedInterfaces` method
8096
0 commit comments