DI Example
This commit is contained in:
@ -0,0 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace DependencyExample
|
||||
{
|
||||
public static class DependencyProvider
|
||||
{
|
||||
// This would be from the DI service
|
||||
public static IFileManager FileManager { get; set; }
|
||||
public static ILogger Logger { get; set; }
|
||||
}
|
||||
|
||||
public interface IFileManager
|
||||
{
|
||||
void WriteFileData(string path, string data);
|
||||
|
||||
string GetFileData(string path);
|
||||
}
|
||||
|
||||
public interface ILogger
|
||||
{
|
||||
void LogMessage(string message);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DependencyExample.Core\DependencyExample.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace DependencyExample
|
||||
{
|
||||
public class WindowsFileManager : IFileManager
|
||||
{
|
||||
public string GetFileData(string path)
|
||||
{
|
||||
return File.ReadAllText(path);
|
||||
}
|
||||
|
||||
public void WriteFileData(string path, string data)
|
||||
{
|
||||
DependencyProvider.Logger.LogMessage($"About to write `{data}` to `{path}`");
|
||||
|
||||
File.WriteAllText(path, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DependencyExample.Core\DependencyExample.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace DependencyExample
|
||||
{
|
||||
public class Logger : ILogger
|
||||
{
|
||||
public void LogMessage(string message)
|
||||
{
|
||||
Console.WriteLine("LOG: " + message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DependencyExample.Core\DependencyExample.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,26 @@
|
||||
using System;
|
||||
|
||||
namespace DependencyExample
|
||||
{
|
||||
public class MockFileManager : IFileManager
|
||||
{
|
||||
private string mData;
|
||||
|
||||
public bool FailOnPurpose { get; set; }
|
||||
|
||||
public string GetFileData(string path)
|
||||
{
|
||||
return mData;
|
||||
}
|
||||
|
||||
public void WriteFileData(string path, string data)
|
||||
{
|
||||
DependencyProvider.Logger.LogMessage($"Mocking `{data}` to `{path}`, appending ` mock` to the end");
|
||||
|
||||
if (FailOnPurpose)
|
||||
mData = $"{data} mock";
|
||||
else
|
||||
mData = data;
|
||||
}
|
||||
}
|
||||
}
|
||||
49
DependencyInjectionExample/DependencyExample.sln
Normal file
49
DependencyInjectionExample/DependencyExample.sln
Normal file
@ -0,0 +1,49 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.27703.2026
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyExample", "DependencyExample\DependencyExample.csproj", "{4E2E48F3-A142-41EA-B220-D0DB9A1F5B6A}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyExample.Core", "DependencyExample.Core\DependencyExample.Core.csproj", "{E032293F-65C1-4679-B337-BED41E3F6CEE}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyExample.FileManager", "DependencyExample.FileManager\DependencyExample.FileManager.csproj", "{4480631E-54CE-4443-9CA0-7F025D86710E}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyExample.MockFileManager", "DependencyExample.MemoryFileManager\DependencyExample.MockFileManager.csproj", "{5500D49A-0299-4671-B0D2-AB280190280E}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyExample.Logger", "DependencyExample.Logger\DependencyExample.Logger.csproj", "{9568AFCC-7D0E-4ACC-A575-520C5A77F311}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{4E2E48F3-A142-41EA-B220-D0DB9A1F5B6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4E2E48F3-A142-41EA-B220-D0DB9A1F5B6A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4E2E48F3-A142-41EA-B220-D0DB9A1F5B6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4E2E48F3-A142-41EA-B220-D0DB9A1F5B6A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{E032293F-65C1-4679-B337-BED41E3F6CEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{E032293F-65C1-4679-B337-BED41E3F6CEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E032293F-65C1-4679-B337-BED41E3F6CEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E032293F-65C1-4679-B337-BED41E3F6CEE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4480631E-54CE-4443-9CA0-7F025D86710E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4480631E-54CE-4443-9CA0-7F025D86710E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4480631E-54CE-4443-9CA0-7F025D86710E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4480631E-54CE-4443-9CA0-7F025D86710E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{5500D49A-0299-4671-B0D2-AB280190280E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{5500D49A-0299-4671-B0D2-AB280190280E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5500D49A-0299-4671-B0D2-AB280190280E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{5500D49A-0299-4671-B0D2-AB280190280E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{9568AFCC-7D0E-4ACC-A575-520C5A77F311}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{9568AFCC-7D0E-4ACC-A575-520C5A77F311}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9568AFCC-7D0E-4ACC-A575-520C5A77F311}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9568AFCC-7D0E-4ACC-A575-520C5A77F311}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {A96C376B-BF76-4B97-AC16-8B0C4DB26FBE}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DependencyExample.Core\DependencyExample.Core.csproj" />
|
||||
<ProjectReference Include="..\DependencyExample.FileManager\DependencyExample.FileManager.csproj" />
|
||||
<ProjectReference Include="..\DependencyExample.Logger\DependencyExample.Logger.csproj" />
|
||||
<ProjectReference Include="..\DependencyExample.MemoryFileManager\DependencyExample.MockFileManager.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
34
DependencyInjectionExample/DependencyExample/Program.cs
Normal file
34
DependencyInjectionExample/DependencyExample/Program.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
|
||||
namespace DependencyExample
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello World!");
|
||||
|
||||
// Inject specific services
|
||||
DependencyProvider.FileManager = new MockFileManager { FailOnPurpose = true };
|
||||
DependencyProvider.Logger = new Logger();
|
||||
|
||||
// Write some file, and read it back
|
||||
var data = "this is my test";
|
||||
var path = @"C:\Users\Luke\Desktop\test.txt";
|
||||
|
||||
// Log
|
||||
DependencyProvider.Logger.LogMessage($"Writing `{data}` to `{path}`");
|
||||
|
||||
// Write
|
||||
DependencyProvider.FileManager.WriteFileData(path, data);
|
||||
|
||||
// Read back
|
||||
var readBack = DependencyProvider.FileManager.GetFileData(path);
|
||||
|
||||
// Log
|
||||
DependencyProvider.Logger.LogMessage($"Read back `{readBack}`");
|
||||
|
||||
Console.Read();
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
DependencyInjectionExample/Diagram 1.png
Normal file
BIN
DependencyInjectionExample/Diagram 1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.3 MiB |
BIN
DependencyInjectionExample/Diagram 2.png
Normal file
BIN
DependencyInjectionExample/Diagram 2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.0 MiB |
Reference in New Issue
Block a user