Skip to content

Commit 619a102

Browse files
committed
First version
1 parent a2149ca commit 619a102

20 files changed

+788
-8
lines changed

.vscode/launch.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
// Use IntelliSense to find out which attributes exist for C# debugging
3+
// Use hover for the description of the existing attributes
4+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/Test/bin/Debug/netcoreapp2.1/Test.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}/Test",
16+
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
17+
"console": "internalConsole",
18+
"stopAtEntry": false,
19+
"internalConsoleOptions": "openOnSessionStart"
20+
},
21+
{
22+
"name": ".NET Core Attach",
23+
"type": "coreclr",
24+
"request": "attach",
25+
"processId": "${command:pickProcess}"
26+
}
27+
,]
28+
}

.vscode/tasks.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/Test/Test.csproj"
11+
],
12+
"problemMatcher": "$msCompile"
13+
}
14+
]
15+
}

LICENCE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Inventory Innovations, Inc. - build by Jon P Smith (GitHub JonPSmith)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

NetCore.AutoRegisterDi.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ VisualStudioVersion = 15.0.27703.2026
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetCore.AutoRegisterDi", "NetCore.AutoRegisterDi\NetCore.AutoRegisterDi.csproj", "{FC8E0DD3-616E-4B90-9342-754E08593D4C}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{90B883DD-4329-4A6A-B73D-3D9682DC4B35}"
9+
EndProject
10+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{82D01612-D8DE-4C78-8394-19712BF8FCF5}"
11+
ProjectSection(SolutionItems) = preProject
12+
LICENCE.txt = LICENCE.txt
13+
READMe.md = READMe.md
14+
EndProjectSection
15+
EndProject
816
Global
917
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1018
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +23,10 @@ Global
1523
{FC8E0DD3-616E-4B90-9342-754E08593D4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
1624
{FC8E0DD3-616E-4B90-9342-754E08593D4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
1725
{FC8E0DD3-616E-4B90-9342-754E08593D4C}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{90B883DD-4329-4A6A-B73D-3D9682DC4B35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{90B883DD-4329-4A6A-B73D-3D9682DC4B35}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{90B883DD-4329-4A6A-B73D-3D9682DC4B35}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{90B883DD-4329-4A6A-B73D-3D9682DC4B35}.Release|Any CPU.Build.0 = Release|Any CPU
1830
EndGlobalSection
1931
GlobalSection(SolutionProperties) = preSolution
2032
HideSolutionNode = FALSE
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2018 Inventory Innovations, Inc. - build by Jon P Smith (GitHub JonPSmith)
2+
// Licensed under MIT licence. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using Microsoft.Extensions.DependencyInjection;
7+
8+
namespace NetCore.AutoRegisterDi
9+
{
10+
public class AutoRegisterData
11+
{
12+
public AutoRegisterData(IServiceCollection services, IEnumerable<Type> typesToConsider)
13+
{
14+
Services = services ?? throw new ArgumentNullException(nameof(services));
15+
TypesToConsider = typesToConsider ?? throw new ArgumentNullException(nameof(typesToConsider));
16+
}
17+
18+
public IServiceCollection Services { get; }
19+
20+
public IEnumerable<Type> TypesToConsider { get; }
21+
public Func<Type, bool> TypeFilter { get; set; }
22+
}
23+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) 2018 Inventory Innovations, Inc. - build by Jon P Smith (GitHub JonPSmith)
2+
// Licensed under MIT licence. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Linq;
6+
using System.Reflection;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
namespace NetCore.AutoRegisterDi
10+
{
11+
public static class AutoRegisterHelpers
12+
{
13+
/// <summary>
14+
/// This finds all the public, non-generic, non-nested classes in an assembly
15+
/// in the provided assemblies
16+
/// </summary>
17+
/// <param name="services">the NET Core dependency injection service</param>
18+
/// <param name="assemblies">Each assembly you want scanned </param>
19+
/// <returns></returns>
20+
public static AutoRegisterData RegisterAssemblyPublicNonGenericClasses(this IServiceCollection services, params Assembly[] assemblies)
21+
{
22+
var allPublicTypes = assemblies.SelectMany(x => x.GetExportedTypes()
23+
.Where(y => y.IsClass && !y.IsAbstract && !y.IsGenericType && !y.IsNested));
24+
return new AutoRegisterData(services, allPublicTypes);
25+
}
26+
27+
/// <summary>
28+
/// This allows you to filter the classes in some way.
29+
/// For instance <code>Where(c =\> c.Name.EndsWith("Service")</code> would only register classes who's name ended in "Service"
30+
/// </summary>
31+
/// <param name="autoRegData"></param>
32+
/// <param name="predicate">A function that will take a type and return true if that type should be included</param>
33+
/// <returns></returns>
34+
public static AutoRegisterData Where(this AutoRegisterData autoRegData, Func<Type, bool> predicate)
35+
{
36+
if (autoRegData == null) throw new ArgumentNullException(nameof(autoRegData));
37+
autoRegData.TypeFilter = predicate;
38+
return new AutoRegisterData(autoRegData.Services, autoRegData.TypesToConsider.Where(predicate));
39+
}
40+
41+
/// <summary>
42+
/// This registers the classes against any public interfaces (other than IDisposable) implemented by the class
43+
/// </summary>
44+
/// <param name="autoRegData">AutoRegister data produced by <see cref="RegisterAssemblyPublicNonGenericClasses"/></param> method
45+
/// <param name="lifetime">Allows you to define the timetime of the service - defaults to ServiceLifetime.Transient</param>
46+
/// <returns></returns>
47+
public static IServiceCollection AsPublicImplementedInterfaces(this AutoRegisterData autoRegData,
48+
ServiceLifetime lifetime = ServiceLifetime.Transient)
49+
{
50+
if (autoRegData == null) throw new ArgumentNullException(nameof(autoRegData));
51+
foreach (var classType in (autoRegData.TypeFilter == null
52+
? autoRegData.TypesToConsider
53+
: autoRegData.TypesToConsider.Where(autoRegData.TypeFilter)))
54+
{
55+
var interfaces = classType.GetTypeInfo().ImplementedInterfaces
56+
.Where(i => i != typeof(IDisposable) && (i.IsPublic));
57+
foreach (var infc in interfaces)
58+
{
59+
autoRegData.Services.Add(new ServiceDescriptor(infc, classType, lifetime));
60+
}
61+
}
62+
63+
return autoRegData.Services;
64+
}
65+
}
66+
}

NetCore.AutoRegisterDi/Class1.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

NetCore.AutoRegisterDi/NetCore.AutoRegisterDi.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5+
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
56
</PropertyGroup>
67

8+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
9+
<DocumentationFile>bin\Debug\netstandard2.0\NetCore.AutoRegisterDi.xml</DocumentationFile>
10+
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.0" />
14+
</ItemGroup>
15+
716
</Project>

READMe.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# NetCore.AutoRegisterDi
2+
3+
This library contains
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
namespace Xunit.Extensions.AssertExtensions
2+
{
3+
/// <summary>
4+
/// Extensions which provide assertions to classes derived from <see cref="bool"/>.
5+
/// </summary>
6+
public static class BooleanAssertionExtensions
7+
{
8+
/// <summary>
9+
/// Verifies that the condition is false.
10+
/// </summary>
11+
/// <param name="condition">The condition to be tested</param>
12+
/// <exception cref="FalseException">Thrown if the condition is not false</exception>
13+
public static void ShouldBeFalse(this bool condition)
14+
{
15+
Assert.False(condition);
16+
}
17+
18+
/// <summary>
19+
/// Verifies that the condition is false.
20+
/// </summary>
21+
/// <param name="condition">The condition to be tested</param>
22+
/// <param name="userMessage">The message to show when the condition is not false</param>
23+
/// <exception cref="FalseException">Thrown if the condition is not false</exception>
24+
public static void ShouldBeFalse(this bool condition,
25+
string userMessage)
26+
{
27+
Assert.False(condition, userMessage);
28+
}
29+
30+
/// <summary>
31+
/// Verifies that an expression is true.
32+
/// </summary>
33+
/// <param name="condition">The condition to be inspected</param>
34+
/// <exception cref="TrueException">Thrown when the condition is false</exception>
35+
public static void ShouldBeTrue(this bool condition)
36+
{
37+
Assert.True(condition);
38+
}
39+
40+
/// <summary>
41+
/// Verifies that an expression is true.
42+
/// </summary>
43+
/// <param name="condition">The condition to be inspected</param>
44+
/// <param name="userMessage">The message to be shown when the condition is false</param>
45+
/// <exception cref="TrueException">Thrown when the condition is false</exception>
46+
public static void ShouldBeTrue(this bool condition,
47+
string userMessage)
48+
{
49+
Assert.True(condition, userMessage);
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)