2017-01-05 7 views
-1

これは愚かな質問かもしれませんが、私はこのtutorielに従います。Ninjectモジュールを使用しようとしています

は、その後、私は、私はNinject

名「のバインド」を使用しています。この次のエラーを持って存在していません。

何が起こっているかここでは簡単な例

using Ninject.Modules; 
using Ninject; 

namespace WCFExampleLibrary.Services 
{ 
    public class IocServices : NinjectModule 
    { 
     public override void Load() 
     { 
      Bind<ICourseRepository>().To<CourseRepository>(); 
      Bind<IStudentRepository>().To<StudentRepository>(); 
      Bind<IEnrollementRepository>().To<EnrollementRepository>(); 

     } 
    } 
} 


using System.Reflection; 
using Ninject; 
namespace WCFExampleLibrary.Services 
{ 
    public static class FactoryBuilder 
    { 
     private static IKernel _Kernal { get; set; } 
     public static T GetServices<T>() 
     { 
      _Kernal = new StandardKernel(); 
      _Kernal.Load(Assembly.GetExecutingAssembly()); 
      return _Kernal.Get<T>(); 
     } 
    } 
} 
+0

どうやって使用していますか?より多くのコードを見る必要があります。 –

答えて

1

は(あなたがNinjectModuleから継承しなければなりません):

class Program 
    { 
     static void Main(string[] args) 
     { 

      Ninject.IKernel kernel = new StandardKernel(new TestModule()); 

      var samurai = kernel.Get<Samurai>(); 
      samurai.Attack("your enemy"); 

      Console.ReadLine(); 
     } 
    } 


    public interface IWeapon 
    { 
     string Hit(string target); 
    } 

    public class Sword : IWeapon 
    { 
     public string Hit(string target) 
     { 
      return "Slice " + target + " in half"; 
     } 
    } 

    public class Dagger : IWeapon 
    { 
     public string Hit(string target) 
     { 
      return "Stab " + target + " to death"; 
     } 
    } 

    public class Samurai 
    { 
     readonly IWeapon[] allWeapons; 

     public Samurai(IWeapon[] allWeapons) 
     { 
      this.allWeapons = allWeapons; 
     } 

     public void Attack(string target) 
     { 
      foreach (IWeapon weapon in this.allWeapons) 
       Console.WriteLine(weapon.Hit(target)); 
     } 
    } 

    class TestModule : Ninject.Modules.NinjectModule 
    { 
     public override void Load() 
     { 
      Bind<IWeapon>().To<Sword>(); 
      Bind<IWeapon>().To<Dagger>(); 
     } 
    } 

は、それはあなたのために正常に動作します願っています。 よろしくお願いします。Andy

関連する問題