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
Copy file name to clipboardExpand all lines: READMe.md
+43-97Lines changed: 43 additions & 97 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,27 +1,40 @@
1
1
# NetCore.AutoRegisterDi
2
2
3
-
This [NuGet library](https://www.nuget.org/packages/NetCore.AutoRegisterDi/) contains an extension method to scan an assemby and register all the non-generic public classes (see [excluded class types](https://github.com/JonPSmith/NetCore.AutoRegisterDi#1-the-registerassemblypublicnongenericclasses-method)) against their implemented interfaces(s) into the `Microsoft.Extensions.DependencyInjection` dependency injection provider.
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
-
**Version 2.0.0 update**: New attributes for defining the `ServiceLifetime` of your classes, e.g. adding the `[RegisterAsScoped]` attribute to a class will mean its `ServiceLifetime` in the DI will be set to `Scoped`. *Added by Fedor Zhekov (GitHub @ZFi88)*.
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
7
-
**Version 2.1.0 update**: Added ability ignore an interface, plus added `ISerializable` interface to list of ignored interfaces and outputs results so that you can check it is registering the correct things.
7
+
## Why have I written this extension?
8
8
9
-
I have written a simple version of AutoFac's `RegisterAssemblyTypes` method that works directly with Microsoft's DI provider. Here are an example of me using this with ASP.NET Core.
9
+
There are two reasons:
10
+
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
+
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.
14
+
15
+
## Documentation
16
+
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._
10
18
11
-
## Example 1 - scan the calling assembly
19
+
### Two, simple examples
20
+
21
+
#### Example 1 - scan the calling assembly
22
+
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
Thisexamplescansthethreeassembliesandregisters*all*theclassesthathaveoneormorepublicinterfaces. That's because I have commented out the `.Where(c => c.Name.EndsWith("Service"))` method.
-Aren't a generic type, e.g. MyClass\<AnotherClass\>
68
64
- Isn'tnested. e.g. Itwon't look at classes defined inside other classes
69
-
2. An `Where` extensionmethod, whichallowsyoutofiltertheclassestobeconsidered.
70
-
3. An `IgnoreThisInterface<IMyInterface>` extensionmethodthatallowsyoutoaddaninterfacetobeignored. Thisallowsyoutostopaclassbeingregistered
71
-
4. The `AsPublicImplementedInterfaces` methodwhichfindsantinterfacesonaclassandregistersthoseinterfacesaspointingtotheclass. ItalsoreturnsalistofresultsthatallowsyoucheckwhatclasseshavebeenregisteredwiththeDIprovider.
Therearesomeinterfacesthatyoudon't want your classes to be registered, for instance `IDisposable` and `ISerializable` and these are already registered in NetCore.AutoRegisterDi. This means that no class will be registered against these two interfaces.
111
-
112
-
Youcanaddextrainterfacestotheinterfaceignorelistusingthe `IgnoreThisInterface<IMyInterface>`, e.g. thefollowingcodewouldaddthetwointerfaces `IMyInterface1` and `IMyInterface2` totheinterfaceignorelist.
113
-
114
-
```c#
115
-
service.RegisterAssemblyPublicNonGenericClasses()
116
-
.IgnoreThisInterface<IMyInterface1>()
117
-
.IgnoreThisInterface<IMyInterface2>()
118
-
.AsPublicImplementedInterfaces();
119
-
```
120
-
121
-
### 4. The `AsPublicImplementedInterfaces` method
92
+
#### 3. The `AsPublicImplementedInterfaces` method
122
93
123
94
The `AsPublicImplementedInterfaces` methodfindsanypublic, non-nestedinterfaces
0 commit comments