2012-03-22 3 views
9

내 솔루션에는 도메인 프로젝트와 MVC3 웹 프로젝트 (예 : MyApp.Domain 및 MyApp.Web)가 있습니다. 이전에는 사용했을 때 Ninject.Extensions.Conventions ver. 2, NinjectMVC3.cs 파일에서 다음 문을 사용할 수 있었고 솔루션 (웹 및 도메인 모두)에 필요한 종속성 (예 : IFoo가 Foo에 자동 바인딩 됨)이 필요했습니다.Ninject 3.0.0을 기반으로 한 컨벤션 기반 종속성 삽입

kernel.Scan(x => 
{ 
    x.FromAssembliesMatching("*"); 
    x.BindWith<DefaultBindingGenerator>(); 
}); 

난 그냥 Ninject에 3.0.0 (시험판)와 Ninject.Extensions.Conventions 3.0.0 (다른 시험판)하지만 바인딩을 기반 규칙에 대한 구문이 변경으로 업그레이드했다. 새 버전에서 다음 문을 사용할 수 있다는 것을 알았지 만 컨벤션 기반 인터페이스는 MyApp.Web이 아니라 MyApp.Domain을 자동으로 바인딩합니다. 이전 버전은 응용 프로그램 전체에서 인터페이스를 바인딩했습니다.

kernel.Bind(x => x 
    .FromThisAssembly() 
    .SelectAllClasses() 
    .BindToAllInterfaces()); 

나는 새로운 Ninject에 버전 바인딩을 기반으로 규칙을 구성하는 방법에 어떤 단서? 어셈블리를 지정하는 것과 관련이 있다고 가정하지만, FromAssembliesMatching("*")을 사용해 보았지만 모든 경우에 실패합니다.

- 편집 RegisterServices 방법에 내 소재 기존 코드를 표시하려면 -

private static void RegisterServices(IKernel kernel) 
{ 
    // This code used to work with v.2 of Ninject.Extensions.Conventions 
    // kernel.Scan(x => 
    // { 
    // x.FromAssembliesMatching("*"); 
    // x.BindWith<DefaultBindingGenerator>(); 
    // }); 

    // This is the new v3 code that automatically injects dependencies but only for interfaces in MyApp.Web, not MyApp.Domain 
    kernel.Bind(x => x.FromThisAssembly().SelectAllClasses().BindToAllInterfaces()); 

    // I tried this code, but it throws "Error activating IDependencyResolver" at "bootstrapper.Initialize(CreateKernel)" 
    // kernel.Bind(x => x.FromAssembliesInPath(AppDomain.CurrentDomain.RelativeSearchPath).SelectAllClasses().BindToAllInterfaces()); 

    // These are dependencies in MyApp.Web that ARE being bound properly by the current configuration 
    // kernel.Bind<IMemberQueries>().To<MemberQueries>(); 
    // kernel.Bind<IGrantApplicationQueries>().To<GrantApplicationQueries>(); 
    // kernel.Bind<IMailController>().To<MailController>(); 

    // These are dependencies in MyApp.Domain that ARE NOT being bound properly by the current configuration, so I have to declare them manually 
    // They used to be injected automatically with version 2 of the conventions extention 
    kernel.Bind(typeof(IRepository<>)).To(typeof(Repository<>)).InRequestScope(); 
    kernel.Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope(); 
    kernel.Bind<IMemberServices>().To<MemberServices>(); 
    kernel.Bind<IGrantApplicationServices>().To<GrantApplicationServices>(); 

    // These are dependencies that SHOULD NOT be bound by convention as they require a different scope or have unique naming 
    kernel.Bind(typeof(EfDbContext)).ToSelf().InRequestScope(); 
    kernel.Bind<IConfigurationProvider>().To<WebConfigConfigurationProvider>().InSingletonScope(); 
    kernel.Bind<IAuthorizationProvider>().To<MyAppAuthorizationProvider>(); 
    kernel.Bind<IPrincipal>().ToMethod(ctx => HttpContext.Current.User).InRequestScope(); 
    kernel.Bind<IGrantApplicationDocumentServices>().To<MySpecialNameGrantAplicationDocumentServices>().InRequestScope(); 
} 
+0

당신은 바라 보았다을 [위키] (https : //로 GitHub의 .com/ninject/ninject.extensions.conventions/wiki). 그렇지 않다면 어떤 비트가 당신에게 이해가 안되는지 몇 가지 아이디어를 줄 수 있습니까? –

+0

루벤, 나는 위키를 보았다. 이해할 수없는 것은 오래된 코드'x.FromAssembliesMatching ("*")'가 MyApp.Web과 MyApp.Domain에있는 모든 인터페이스를 작업하고 바인딩했다는 것입니다. 그러나 새로운 3.0.0에서는 구문이 작동하지 않습니다. 내가 찾은 가장 가까운 것은'x.FromThisAssembly()'이지만 MyApp.Web 내의 인터페이스 만 바인드합니다 (주입이 이루어지는 곳이기 때문에). MyApp.Domain의 인터페이스를 자동으로 바인딩하지는 않습니다. – bigmac

+0

코드를 보면 항상 AppDomain의 어셈블리 목록 또는 일부 옵서버 메커니즘을 통해 어셈블리를 가져 와서 명시 적으로 목록을 제공 할 수 있습니다. 그 외에는 소스를 읽는 것이 좋을뿐입니다. https://github.com/ninject/ninject.extensions.conventions/blob/master/src/Ninject.Extensions.Conventions/BindingBuilder/AssemblyFinder.cs –

답변

17

등가은 다음과 같습니다

kernel.Bind(x => x 
    .FromAssembliesMatching("*") 
    .SelectAllClasses() 
    .BindDefaultInterface()); 
+1

Thanks Remo. 나는 바보 같아. 그것은 내가 시도한 첫 번째 작업 중 하나 였지만 실패한 BindToDefaultInterfaces (복수형) 메서드를 사용했습니다.단수의 방법으로 변경했을 때 효과적이었습니다. 모든 게시물을 유감스럽게 생각하지만 도움을 주시면 감사하겠습니다. 모든 프로젝트에 큰 성과를 거뒀습니다! – bigmac