You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have written a simple version of AutoFac's `RegisterAssemblyTypes` method that works directly with Microsoft's DI provider, i.e this library to scan an assemby (or assemblies) on your application and register all the public normal classes (i.e not [generic classes](https://www.tutorialspoint.com/Generics-vs-non-generics-in-Chash)) that have an interface into the Microsoft NET's [Dependency injection provider]https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection) (DI for short).
3
+
I have written a simple version of AutoFac's `RegisterAssemblyTypes` method that works directly with Microsoft's DI provider, i.e this library to scan an assemby (or assemblies) on your application and register all the public normal classes (i.e not [generic classes](https://www.tutorialspoint.com/Generics-vs-non-generics-in-Chash)) that have an interface into the Microsoft NET's [Dependency injection provider]https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection (DI for short).
4
4
5
5
The NetCore.AutoRegisterDi is available on [NuGet as EfCore.SchemaCompare](https://www.nuget.org/packages/NetCore.AutoRegisterDi) and is an open-source library under the MIT license. See [ReleaseNotes](https://github.com/JonPSmith/NetCore.AutoRegisterDi/blob/masterReleaseNotes.md) for details of changes and information for each release.
6
6
@@ -10,15 +10,15 @@ There are two reasons:
10
10
11
11
1. I really hate having to hand-code each registering of my services - this extension method scans assembles and finds/registers classes with interfaces for you.
12
12
2. I used to use [AutoFac's](https://autofac.org/)[assembly scanning](http://autofac.readthedocs.io/en/latest/register/scanning.html#assembly-scanning)
13
-
feature, but I then saw a [tweet by @davidfowl](https://twitter.com/davidfowl/status/987866910946615296) about [Dependency Injection container benchmark(https://ipjohnson.github.io/DotNet.DependencyInjectionBenchmarks/) which showed the Microsoft's DI provider was much faster than AutoFac. I therefore implemented a similar (but not exactly the same) feature for the Microsoft.Extensions.DependencyInjection library.
13
+
feature, but I then saw a [tweet by @davidfowl](https://twitter.com/davidfowl/status/987866910946615296) about [Dependency Injection container benchmark](https://ipjohnson.github.io/DotNet.DependencyInjectionBenchmarks/) which showed the Microsoft's DI provider was much faster than AutoFac. I therefore implemented a similar (but not exactly the same) feature for the `Microsoft.Extensions.DependencyInjection` library.
14
14
15
-
##Documentation
15
+
# Documentation
16
16
17
17
_NOTE: There is an [article about this library](https://www.thereformedprogrammer.net/asp-net-core-fast-and-automatic-dependency-injection-setup/) which gives you an overview of this library. Useful if you haven't used this library before._
18
18
19
-
###Two, simple examples
19
+
## Two, simple examples
20
20
21
-
####Example 1 - scan the calling assembly
21
+
### Example 1 - scan the calling assembly
22
22
23
23
This example scans the assembly where you call the AutoRegisterDi's `RegisterAssemblyPublicNonGenericClasses` method for all the classes which has one or more public interfaces and the Class's name ends with "Service" are registered with .NET's
24
24
@@ -32,7 +32,7 @@ public void ConfigureServices(IServiceCollection services)
32
32
.AsPublicImplementedInterfaces();
33
33
```
34
34
35
-
#### Example 2 - scanning multiple assemblies
35
+
### Example 2 - scanning multiple assemblies
36
36
37
37
Thisexamplescansthethreeassembliesandregisters*all*theclassesthathaveoneormorepublicinterfaces. That's because I have commented out the `.Where(c => c.Name.EndsWith("Service"))` method.
38
38
@@ -54,22 +54,24 @@ public void ConfigureServices(IServiceCollection services)
*See [MicrosoftDIDocs](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1) for more on this*.*
96
+
The `AsPublicImplementedInterfaces` methodfindsanypublic, non-nestedinterfaces (apartfrom `IDisposable` and `ISerializable`) thateachclassimplementsandregisterseachinterface, knownas_servicetype_, againsttheclass, knownasthe_implementationtype_. Thismeansifyouuseaninterfaceinaconstructor (orotherDI-enabledplaces) thentheMicrosoftDIresolverwillprovideaninstanceoftheclassthatinterfacewaslinkedto. _See [MicrosoftDIDocs](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1) for more on this_.
Bydefaultitwillregistertheclassesashavingalifetimeof `ServiceLifetime.Transient`, butthe `AsPublicImplementedInterfaces` methodhasanoptionalparametercalled `lifetime` whichyoucanchangetoanotherlifetime. Notethatalltheclasseswillhavethesamelifetime, butyoucanuseAutoRegisterDi's attributes to set different lifetime to a class (see section 5).
Someclasseshaveinterfacesthatwedon't really want to be registered to the DI provider - for instance `IDisposable` and `ISerializable`. Therefore AutoRegisterDi has two methods which allow you to define a interface that shouldn'tberegisteredonanyclasses. Theyare:
106
+
107
+
#### The `IgnoreThisInterface<TInterface>` method
108
+
109
+
Thismethodallowsyoutoaddainterfacetotoalistofinterfacesthatyoudon't register to the DI provider. The example below adds the `IMyInterface` in the list of interfaces to not register.
110
+
111
+
```c#
112
+
service.RegisterAssemblyPublicNonGenericClasses()
113
+
.IgnoreThisInterface<IMyInterface>()
114
+
.AsPublicImplementedInterfaces();
115
+
```
116
+
117
+
NOTES
118
+
119
+
-Thelistofinterfacestoignorehasalreadygotthe `IDisposable` and `ISerializable` interfaces.
#### The `IgnoreThisGenericInterface(Type interfaceType)` method
123
+
124
+
ThismethodwasaskedforLajosMarton (GitHub@martonx). Hewasusing [`record`s](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record) and found that each `record` has a `IEquatable<RecordType>` and he didn't wanted the DI provider be up with interfaces that aren't used.
-Ihaven't `IEquatable<>` to the ignore interface list as there may be a valid use to register a `IEquatable<SomeClass>` sometimes. In that case you would need to use `IgnoreThisGenericInterface` to define all your `record`s separably.
137
+
-Themethodworksforanygenericinterfacetypeaslongthatthegenericinterfacehasnoargumentsarefilledin, e.g `IDictionary<,>` isfine, but `IDictionary<,string>` won't work (you get an exception).
0 commit comments