Added a separate assembly for the Ninject bindings because I do not wish to add Ninject as a core dependency (there are a ton of alternative dependency injectors out there!)

git-svn-id: file:///srv/devel/repo-conversion/dumps/svni@1 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2019-02-06 18:41:58 +00:00
commit 863fff7726
4 changed files with 180 additions and 0 deletions

View File

@ -0,0 +1,64 @@
<?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>{4B185E46-672F-4629-98E8-82DB07AA8147}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Nuclex.Windows.Forms.Ninject</RootNamespace>
<AssemblyName>Nuclex.Windows.Forms.Ninject</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\net-4.0\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\net-4.0\Debug\Nuclex.Windows.Forms.Ninject.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\net-4.0\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\net-4.0\Release\Nuclex.Windows.Forms.Ninject.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Ninject.ClientProfile">
<HintPath>..\..\ItGlueQueryTool\References\ninject\net-4.0\Ninject.ClientProfile.dll</HintPath>
</Reference>
<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.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Source\NinjectWindowManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Source\MvvmModule.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nuclex.Support\Nuclex.Support %28net-4.0%29.csproj">
<Project>{00567408-4f44-4c00-866e-b04a99e482f2}</Project>
<Name>Nuclex.Support %28net-4.0%29</Name>
</ProjectReference>
<ProjectReference Include="..\Nuclex.Windows.Forms\Nuclex.Windows.Forms %28net-4.0%29.csproj">
<Project>{b2bfa3ec-3b7b-4ee0-8395-8426b5c7a8b8}</Project>
<Name>Nuclex.Windows.Forms %28net-4.0%29</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,29 @@
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("Nuclex.Windows.Forms.Ninject")]
[assembly: AssemblyProduct("Nuclex.Windows.Forms.Ninject")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyCompany("Nuclex Development Labs")]
[assembly: AssemblyCopyright("Copyright © Nuclex Development Labs 2019")]
// 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("4b185e46-672f-4629-98e8-82db07aa8147")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]

46
Source/MvvmModule.cs Normal file
View File

@ -0,0 +1,46 @@
using System;
using Ninject;
using Ninject.Activation;
using Ninject.Modules;
using Nuclex.Windows.Forms.AutoBinding;
namespace Nuclex.Windows.Forms.Ninject {
/// <summary>Sets up the service bindings for an MVVM-based WPF application</summary>
public class MvvmModule : NinjectModule {
/// <summary>Called when the module is loaded into the kernel</summary>
public override void Load() {
// The window manager keeps track of active windows and can figure out
// which window to display for a view model by its naming convention.
Kernel.Bind<WindowManager>().To<NinjectWindowManager>().InSingletonScope();
Kernel.Bind<IWindowManager>().ToMethod(getWindowManager).InSingletonScope();
Kernel.Bind<IActiveWindowTracker>().ToMethod(getWindowManager).InSingletonScope();
Kernel.Bind<IAutoBinder>().ToMethod(CreateAutoBinder).InSingletonScope();
}
/// <summary>Creates and initializd the auto view model binder</summary>
/// <param name="context">
/// Context containing environmental informations about the request and the kernel
/// </param>
/// <returns>The view model auto binder that will be used by the application</returns>
protected virtual IAutoBinder CreateAutoBinder(IContext context) {
return new ConventionBinder();
}
/// <summary>Retrieves the window manager from the kernel</summary>
/// <param name="context">
/// Context containing environmental informations about the request and the kernel
/// </param>
/// <returns>The window manager registered to the kernel</returns>
private static WindowManager getWindowManager(IContext context) {
return context.Kernel.Get<WindowManager>();
}
}
} // namespace Nuclex.Windows.Forms.Ninject

View File

@ -0,0 +1,41 @@
using System;
using Ninject;
using Nuclex.Windows.Forms.AutoBinding;
namespace Nuclex.Windows.Forms.Ninject {
/// <summary>Window manager that is using Ninject</summary>
public class NinjectWindowManager : WindowManager {
/// <summary>Initializes a new window manager</summary>
/// <param name="kernel">
/// Ninject kernel the window manager uses to construct view models
/// </param>
/// <param name="autoBinder">
/// View model binder that will be used to bind all created views to their models
/// </param>
public NinjectWindowManager(IKernel kernel, IAutoBinder autoBinder = null) :
base(autoBinder) {
this.kernel = kernel;
}
/// <summary>Creates an instance of the specified type</summary>
/// <param name="type">Type an instance will be created of</param>
/// <returns>The created instance</returns>
/// <remarks>
/// Use this to wire up your dependency injection container. By default,
/// the Activator class will be used to create instances which only works
/// if all of your view models are concrete classes.
/// </remarks>
protected override object CreateInstance(Type type) {
return this.kernel.Get(type);
}
/// <summary>The Ninject kernel used to create new instances</summary>
private readonly IKernel kernel;
}
} // namespace Nuclex.Windows.Forms.Ninject