2010-11-30 5 views
0

私はNinjectを初めて使用しており、私はstackoverflowも新しくなっています。Ninjectカーネルからインスタンスを取得

私はninject.web.mvc拡張子とそれを使用しています、私はこのようにそれを正しく初期化することができました:

public class MvcApplication : NinjectHttpApplication 
{ 
    protected override void OnApplicationStarted() 
    { 
     RegisterRoutes(RouteTable.Routes); 
    } 

    protected override IKernel CreateKernel() 
    { 
     var kernel = new StandardKernel(); 
     kernel.Load(AssemblyLocator.GetBinFolderAssemblies()); 
     return kernel; 
    } 
} 

をそしてここでbinフォルダ内のすべてのアセンブリをスキャンし、私のクラスassemlylocatorあり、アセンブリ内のすべてのNinjectモジュールを検索します。

public static class AssemblyLocator 
{ 
    private static readonly ReadOnlyCollection AllAssemblies = null; 
    private static readonly ReadOnlyCollection BinFolderAssemblies = null; 

    static AssemblyLocator() 
    { 
     AllAssemblies = new ReadOnlyCollection<Assembly>( 
      BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList()); 

     IList<Assembly> binFolderAssemblies = new List<Assembly>(); 

     string binFolder = HttpRuntime.AppDomainAppPath + "bin\\"; 
     IList<string> dllFiles = Directory.GetFiles(binFolder, "*.dll", 

     SearchOption.TopDirectoryOnly).ToList(); 

     foreach (string dllFile in dllFiles) 
     { 
      AssemblyName assemblyName = AssemblyName.GetAssemblyName(dllFile); 
      Assembly locatedAssembly = AllAssemblies.FirstOrDefault(a => 
      AssemblyName.ReferenceMatchesDefinition(a.GetName(), assemblyName)); 

      if (locatedAssembly != null) 
      { 
       binFolderAssemblies.Add(locatedAssembly); 
      } 
     } 

     BinFolderAssemblies = new ReadOnlyCollection<Assembly> (binFolderAssemblies); 
    } 

    public static ReadOnlyCollection<Assembly> GetAssemblies() 
    { 
     return AllAssemblies; 
    } 

    public static ReadOnlyCollection<Assembly> GetBinFolderAssemblies() 
    { 
     return BinFolderAssemblies; 
    } 
} 

すべては私のコントローラで正常に動作します:

public class ReteController : Controller 
{ // // GET: /Rete/ 

    private readonly IReteService _service; 

    public ReteController(IReteService _service) 
    { 
     if (_service == null) 
     { 
      throw new ArgumentNullException("IReteService"); 
     } 
     this._service = _service; 
    } 

    public ActionResult Index() 
    { 
     return View(_service.getReti()); 
    } 

ほとんどすべてが学ぶことは簡単だったここまでは、今私の問題は、私がバインドされたオブジェクトの新しいインスタンスを作成する必要がある場合ということですNinjectModule from Ninjectカーネルにヒアリングをどのようにしてアクセスするのか分かりません。

//this is jus a ex. 

public ActionResult NewRete() { 
    IRete xItem = Kernel.get(); 
    xItem.name= "hope"; 
    return View(xItem); 
} 

私のコントローラからカーネルを見つけることができないという問題があります。私もコンストラクタにそれを注入する必要がありますか?

誰かが私を助けてくれることを願っています。皆さんがいつも私に与える大きな助けに感謝します。

答えて