.NetFramework/C#でDIする

目的

.NetFramework/C#でDIする !!

.NetCoreでDI?違います。いまさらですが .NetFrameworkです。。。

Ninjectというライブラリを使用してDIを行います。

.NetFramework/C#でDIする ための環境

  • OS:Windows 10
  • VisualStudio2015
  • .NetFramework 4.6.2
  • Ninject 3.3.6

サンプルコード

VisualStudio2015でConsoleアプリケーションを作成します。

NugetからDIモジュールNinjectをインストール

.NetFramework/C#でDIする Ninjectモジュールのインストール

プログラム作成

以下のプログラムは全て同じ階層で作成しています。

IUserRepository.cs

ユーザー名を返すだけのインターフェイスです。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
namespace DISample
{
public interface IUserRepository
{
string FetchUserName();
}
}
using System; namespace DISample { public interface IUserRepository { string FetchUserName(); } }
using System;

namespace DISample
{
    public interface IUserRepository
    {
        string FetchUserName();
    }
}

InMemoryUserRepository.cs

先ほどのIUserRepositoryインターフェイスを実装したクラスです。

今回はサンプルなので固定値でユーザー名を返すようにしています。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using System;
namespace DISample
{
public class InMemoryUserRepository : IUserRepository
{
public string FetchUserName()
{
return "John Doe";
}
}
}
using System; namespace DISample { public class InMemoryUserRepository : IUserRepository { public string FetchUserName() { return "John Doe"; } } }
using System;

namespace DISample
{
    public class InMemoryUserRepository : IUserRepository
    {
        public string FetchUserName()
        {
            return "John Doe";
        }
    }
}

Program.cs

12行~14行でDIの処理を行っています。

13行目のBindメソッドでDIを行うインターフェイスの指定を行い、ToメソッドでIUserRepositoryを使用する際に実際に処理が行われる実装クラスの指定を行っています。

ここでは、InMemoryUserRepositoryクラスが使用されるようになっています。

14行目では8行目のメンバー変数の_repositoryにDIした結果を代入しています。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
using Ninject;
using System;
namespace DISample
{
class Program
{
private static readonly IUserRepository _repository;
static Program()
{
IKernel kernel = new StandardKernel();
kernel.Bind<IUserRepository>().To<InMemoryUserRepository>();
_repository = kernel.Get<IUserRepository>();
}
static void Main(string[] args)
{
string userName = _repository.FetchUserName();
Console.WriteLine(userName);
Console.ReadLine();
}
}
}
using Ninject; using System; namespace DISample { class Program { private static readonly IUserRepository _repository; static Program() { IKernel kernel = new StandardKernel(); kernel.Bind<IUserRepository>().To<InMemoryUserRepository>(); _repository = kernel.Get<IUserRepository>(); } static void Main(string[] args) { string userName = _repository.FetchUserName(); Console.WriteLine(userName); Console.ReadLine(); } } }
using Ninject;
using System;

namespace DISample
{
    class Program
    {
        private static readonly IUserRepository _repository;

        static Program()
        {
            IKernel kernel = new StandardKernel();
            kernel.Bind<IUserRepository>().To<InMemoryUserRepository>();
            _repository = kernel.Get<IUserRepository>();
        }

        static void Main(string[] args)
        {
            string userName = _repository.FetchUserName();
            Console.WriteLine(userName);
            Console.ReadLine();
        }        
    }
}

出力結果として、”John Doe”が得られます。

.NetFramework/C#でDIできましたが、これだけではうまみを感じれません。

次の記事でさらにいい感じにDIを使える方法説明していきます。

以上.

他の記事

MSTestでDIする方法 TODO

ASP.NET MVC5でDIする方法 TODO

ASP.NET MVC5でDIしつつ結合テストする方法 TODO

ゆるキャリエンジニア日誌 IT

コメントする