2012-11-27 2 views
1

다른 전신 스타일 질문입니다. 첫째, 내 NinjectModule :프로젝트 간 Interop 종속성 해결

using Ninject.Modules; 
using Microsoft.Office.Interop.Word; 

namespace NinjectSample.Util.Injection 
{ 
    public class SampleModule : NinjectModule 
    { 
     public override void Load() 
     { 
      Bind<_Application>().ToMethod(ctx => GetApp()); 
     } 

     public _Application GetApp() 
     { 
      return new Application(); 
     } 
    } 
} 

우선 (작품!) :

 IKernel kernel = new StandardKernel(new SampleModule()); 
     var foo = kernel.Get<_Application>(); 

 IKernel kernel = new StandardKernel(new SampleModule()); 
     var foo = kernel.Get<BusinessClass>(); 

BusinessClass이 변경 다른 어셈블리 코드에 정의되어

namespace BusinessClassLibrary 
{ 
    public class BusinessClass 
    { 
     private _Application _app; 

     [Inject] 
     public BusinessClass(_Application application) 
     { 
      _app = application; 
     } 
    } 
} 

결과가 표시됩니다. 지역 :

Error activating _Application 
No matching bindings are available, and the type is not self-bindable. 
Activation path: 
    2) Injection of dependency _Application into parameter application of constructor of type BusinessClass 
    1) Request for BusinessClass 

Suggestions: 
    1) Ensure that you have defined a binding for _Application. 
    2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 
    3) Ensure you have not accidentally created more than one kernel. 
    4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 
    5) If you are using automatic module loading, ensure the search path and filters are correct. 

나는 너무 깊이 상호 운용성에 아니지만, 닷넷의 내 기본적인 이해 나에게 다음과 같은 우연히 발견 할 수 있습니다 :

  • Application는 그것의 기초 _Application 같은 인터페이스입니다. 그럼에도 불구하고 생성자를 호출 할 수 있습니다. 왜?

  • Ninject는 커널이 정의 된 어셈블리에 종속성이있는 동안 작동하는 팩토리 메서드를 사용하도록 지시합니다. 다른 어셈블리에있을 때 자체 바인딩으로 종속성을 해결하려고하는 이유는 무엇입니까? 어셈블리?

답변

1

응용 프로그램은 해당 기본 _Application 같은 인터페이스입니다. 그럼에도 불구하고 생성자를 호출 할 수 있습니다. 왜?

이것은 컴파일러 트릭입니다. 무슨 일이 일어나고 있는지에 대한 정보는 Creating instance of interface in C#을 참조하십시오. 코드에서 문제가 발생하면 확실히 이상하게 보입니다. 불행히도, 현재 질문의 실제 Ninject 부분을 도와 드릴 수는 없습니다.

+0

CoClass 비즈니스를 지적 해 주셔서 감사합니다! –