Skip to content

Commit 648aa85

Browse files
committed
Examples added
1 parent 2a1aeda commit 648aa85

30 files changed

+732
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
</configuration>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using DependencyInjection.Services;
2+
3+
namespace DependencyInjection.Consumers
4+
{
5+
internal class Consumer1
6+
{
7+
private readonly IFirstService service;
8+
9+
public Consumer1(IFirstService service)
10+
{
11+
this.service = service;
12+
}
13+
14+
public void Run()
15+
{
16+
this.service.Write();
17+
}
18+
}
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using DependencyInjection.Services;
7+
8+
namespace DependencyInjection.Consumers
9+
{
10+
class Consumer2
11+
{
12+
private readonly SecondService service;
13+
14+
public Consumer2(SecondService service)
15+
{
16+
this.service = service;
17+
}
18+
19+
public void Run()
20+
{
21+
this.service.Write();
22+
}
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using DependencyInjection.Services;
3+
4+
namespace DependencyInjection.Consumers
5+
{
6+
internal class Consumer3
7+
{
8+
private readonly FirstService service;
9+
private readonly string additional;
10+
11+
// At the moment the order of the paramaters is relevant. Add the custom ones (provided by the Create method) before the ones from the resolver
12+
public Consumer3(string additional, FirstService service)
13+
{
14+
this.service = service;
15+
this.additional = additional;
16+
}
17+
18+
public void Run()
19+
{
20+
this.service.Write();
21+
Console.WriteLine($"Consumer 3 with addition info: {this.additional}");
22+
}
23+
}
24+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{B0B61943-6546-45BA-86DF-1C62BD65A071}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>DependencyInjection</RootNamespace>
10+
<AssemblyName>DependencyInjection</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="KY.Core.Common, Version=4.0.0.0, Culture=neutral, processorArchitecture=MSIL">
37+
<HintPath>packages\KY.Core.Common.4.0.0\lib\netstandard2.0\KY.Core.Common.dll</HintPath>
38+
</Reference>
39+
<Reference Include="System" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Xml.Linq" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="Microsoft.CSharp" />
44+
<Reference Include="System.Data" />
45+
<Reference Include="System.Net.Http" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="Consumers\Consumer1.cs" />
50+
<Compile Include="Consumers\Consumer2.cs" />
51+
<Compile Include="Consumers\Consumer3.cs" />
52+
<Compile Include="Program.cs" />
53+
<Compile Include="Properties\AssemblyInfo.cs" />
54+
<Compile Include="Services\FirstService.cs" />
55+
<Compile Include="Services\SecondService.cs" />
56+
</ItemGroup>
57+
<ItemGroup>
58+
<None Include="App.config" />
59+
<None Include="packages.config" />
60+
</ItemGroup>
61+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
62+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.28407.52
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyInjection", "DependencyInjection.csproj", "{B0B61943-6546-45BA-86DF-1C62BD65A071}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B0B61943-6546-45BA-86DF-1C62BD65A071}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B0B61943-6546-45BA-86DF-1C62BD65A071}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B0B61943-6546-45BA-86DF-1C62BD65A071}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B0B61943-6546-45BA-86DF-1C62BD65A071}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {E3D01CAA-FD90-45E0-8C72-202492E27634}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using DependencyInjection.Consumers;
3+
using DependencyInjection.Services;
4+
using KY.Core.Dependency;
5+
6+
namespace DependencyInjection
7+
{
8+
internal class Program
9+
{
10+
private static void Main(string[] args)
11+
{
12+
DependencyResolver resolver = new DependencyResolver();
13+
14+
// Bind a type to an interface
15+
resolver.Bind<IFirstService>().To<FirstService>();
16+
17+
// Bind a type as ans singleton
18+
resolver.Bind<SecondService>().ToSingleton();
19+
20+
// Create an instance of an object with dependency injection
21+
Consumer1 consumer1 = resolver.Create<Consumer1>();
22+
consumer1.Run();
23+
24+
// Resolve an instance by hand
25+
resolver.Get<SecondService>().Write();
26+
27+
// Resolve by type
28+
Consumer2 consumer2 = (Consumer2)resolver.Create(typeof(Consumer2));
29+
consumer2.Run();
30+
31+
// Resolve with additional parameters
32+
Consumer3 consumer3 = resolver.Create<Consumer3>("abcdefg");
33+
consumer3.Run();
34+
35+
Console.ReadLine();
36+
}
37+
}
38+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("DependencyInjection")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("DependencyInjection")]
13+
[assembly: AssemblyCopyright("Copyright © 2019")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("b0b61943-6546-45ba-86df-1c62bd65a071")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace DependencyInjection.Services
4+
{
5+
internal interface IFirstService
6+
{
7+
void Write();
8+
}
9+
10+
internal class FirstService : IFirstService
11+
{
12+
public void Write()
13+
{
14+
Console.WriteLine("Hello from FirstService");
15+
}
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace DependencyInjection.Services
4+
{
5+
internal class SecondService
6+
{
7+
public DateTime Date { get; set; }
8+
9+
public SecondService()
10+
{
11+
this.Date = DateTime.Now;
12+
}
13+
14+
public void Write()
15+
{
16+
Console.WriteLine($"SecondService created on {this.Date:HH:mm:ss.fff}");
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)