SimpleContainer dependency injection system implemented

This commit is contained in:
2020-07-28 23:56:14 +02:00
parent 3e6245be9b
commit d790cb96d4
12 changed files with 287 additions and 2 deletions

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ImageHandlingLibrary
{
public class Filing
{
}
}

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CF1C98B6-5093-4B93-8B14-A0278B1AF724}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ImageHandlingLibrary</RootNamespace>
<AssemblyName>ImageHandlingLibrary</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Filing.cs" />
<Compile Include="InterFaces\IRegistring.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Registring.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ImageHandlingLibrary.InterFaces
{
public interface IRegistring
{
string GetRegistryRootDir();
void SetRegistryValues(string rootDir);
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ImageHandlingLibrary")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ImageHandlingLibrary")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("cf1c98b6-5093-4b93-8b14-a0278b1af724")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,59 @@
using ImageHandlingLibrary.InterFaces;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ImageHandlingLibrary
{
public class Registring :IRegistring
{
public Registring()
{
CU = Registry.CurrentUser;
}
public RegistryKey CU { get; set; }
public string GetRegistryRootDir()
{
string output;
RegistryKey PictureHandling = CU.OpenSubKey(@"SOFTWARE\IdoIt4u\PictureHandling");
if (PictureHandling == null)
{
output = @"D:\OurPictures";
SetRegistryValues(output);
}
else
{
output = (string)PictureHandling.GetValue(@"RootMap");
}
return output;
}
public void SetRegistryValues(string rootDir)
{
RegistryKey IdoIt4u = CU.OpenSubKey(@"SOFTWARE\IdoIt4u", true);
RegistryKey PictureHandling = null;
if (IdoIt4u != null)
{
PictureHandling = IdoIt4u.OpenSubKey(@"PictureHandling", true);
}
if (PictureHandling == null)
{
if (IdoIt4u == null)
{
IdoIt4u = Registry.CurrentUser.CreateSubKey(@"Software\IdoIt4u", true);
}
else
PictureHandling = IdoIt4u.CreateSubKey(@"PictureHandling", true);
}
else
{
PictureHandling.SetValue(@"RootMap", rootDir);
}
}
}
}

View File

@ -11,14 +11,57 @@ namespace ImageHandlingUI
{
public class Bootstrapper : BootstrapperBase
{
// implementing SimpleContainer (dependency injection system in Caliburn micro)
private SimpleContainer _container = new SimpleContainer();
public Bootstrapper()
{
Initialize();
}
// implementing SimpleContainer (dependency injection system in Caliburn micro)
//
protected override void Configure()
{
_container.Instance(_container);
_container
.Singleton<IWindowManager, WindowManager>()
.Singleton<IEventAggregator, EventAggregator>();
GetType().Assembly.GetTypes()
.Where(type => type.IsClass)
.Where(type => type.Name.EndsWith("ViewModel"))
.ToList()
.ForEach(viewModelType => _container.RegisterPerRequest(
viewModelType, viewModelType.ToString(), viewModelType));
}
//
// implementing SimpleContainer (dependency injection system in Caliburn micro)
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<ShellViewModel>();
}
// implementing SimpleContainer (dependency injection system in Caliburn micro)
//
protected override object GetInstance(Type service, string key)
{
return _container.GetInstance(service, key);
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return _container.GetAllInstances(service);
}
protected override void BuildUp(object instance)
{
_container.BuildUp(instance);
}
//
// implementing SimpleContainer (dependency injection system in Caliburn micro)
}
}

View File

@ -1,6 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View File

@ -34,13 +34,26 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac, Version=5.2.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
<HintPath>..\packages\Autofac.5.2.0\lib\net461\Autofac.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.1.1.1\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Specialized, Version=4.0.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Collections.Specialized.4.3.0\lib\net46\System.Collections.Specialized.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.7.1\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
@ -60,6 +73,7 @@
</Compile>
<Compile Include="LocalPicture.cs" />
<Compile Include="Program.cs" />
<Compile Include="ProgramModule.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
@ -87,5 +101,11 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ImageHandlingLibrary\ImageHandlingLibrary.csproj">
<Project>{cf1c98b6-5093-4b93-8b14-a0278b1af724}</Project>
<Name>ImageHandlingLibrary</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -1,4 +1,5 @@
using System;
using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -14,6 +15,10 @@ namespace PictureHandling
[STAThread]
static void Main()
{
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterModule<ProgramModule>();
var container = containerBuilder.Build();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

View File

@ -0,0 +1,20 @@
using Autofac;
using ImageHandlingLibrary;
using ImageHandlingLibrary.InterFaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PictureHandling
{
class ProgramModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<Registring>().As<IRegistring>();
}
}
}

View File

@ -1,4 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Autofac" version="5.2.0" targetFramework="net472" />
<package id="Microsoft.Bcl.AsyncInterfaces" version="1.1.1" targetFramework="net472" />
<package id="System.Collections.Specialized" version="4.3.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.7.1" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net472" />
</packages>

View File

@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PictureHandling", "PictureH
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageHandlingWPFUI", "ImageHandlingUI\ImageHandlingWPFUI.csproj", "{9F7FC0FF-02D4-4B31-8B8A-81B015483445}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ImageHandlingLibrary", "ImageHandlingLibrary\ImageHandlingLibrary.csproj", "{CF1C98B6-5093-4B93-8B14-A0278B1AF724}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -21,6 +23,10 @@ Global
{9F7FC0FF-02D4-4B31-8B8A-81B015483445}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F7FC0FF-02D4-4B31-8B8A-81B015483445}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F7FC0FF-02D4-4B31-8B8A-81B015483445}.Release|Any CPU.Build.0 = Release|Any CPU
{CF1C98B6-5093-4B93-8B14-A0278B1AF724}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF1C98B6-5093-4B93-8B14-A0278B1AF724}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF1C98B6-5093-4B93-8B14-A0278B1AF724}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF1C98B6-5093-4B93-8B14-A0278B1AF724}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE