IT TIP

Ninject 모듈의 의도는 무엇입니까?

itqueen 2020. 12. 12. 12:52
반응형

Ninject 모듈의 의도는 무엇입니까?


나는 ninject에 대한 완전한 초보자

다른 사람의 코드를 떼어 내고 nInject 모듈의 여러 인스턴스를 찾았습니다. Ninject.Modules.Module에서 파생 된 클래스이며 대부분의 코드를 포함하는로드 메서드가 있습니다.

이러한 클래스는 StandardKernel 인스턴스의 LoadModule 메서드를 호출하고 모듈 클래스의 인스턴스를 전달하여 호출됩니다.

아마도 여기에서 분명한 것을 놓치고 있지만, 평범한 오래된 클래스를 만들고 그 메서드를 호출하는 것보다 이것의 이점은 무엇입니까, 아니면 정적 메서드가있는 정적 클래스일까요?

 


Ninject 모듈은 IoC 컨테이너에 다양한 유형을 등록하는 데 사용되는 도구입니다. 장점은 이러한 모듈이 자체 클래스에 유지된다는 것입니다. 이를 통해 자체 모듈에 서로 다른 계층 / 서비스를 배치 할 수 있습니다.

// some method early in your app's life cycle
public Kernel BuildKernel()
{
    var modules = new INinjectModule[] 
    {
        new LinqToSqlDataContextModule(), // just my L2S binding
        new WebModule(),
        new EventRegistrationModule()
    };
    return new StandardKernel(modules);
}

// in LinqToSqlDataContextModule.cs
public class LinqToSqlDataContextModule : NinjectModule
{
    public override void Load()
    {
        Bind<IRepository>().To<LinqToSqlRepository>();
    }
}

여러 모듈을 사용하면 IoC 컨테이너 내에서도 문제를 분리 할 수 ​​있습니다.

나머지 질문은 Ninject뿐만 아니라 IoC 및 DI 전체에 관한 것 같습니다. 예, 정적 구성 개체를 사용하여 IoC 컨테이너가 수행하는 모든 작업을 수행 할 수 있습니다. IoC 컨테이너는 여러 계층의 종속성이있을 때 정말 멋집니다.

public interface IInterfaceA {}
public interface IInterfaceB {}
public interface IInterfaceC {}

public class ClassA : IInterfaceA {}

public class ClassB : IInterfaceB
{
    public ClassB(IInterfaceA a){}
}

public class ClassC : IInterfaceC
{
    public ClassC(IInterfaceB b){}
}

이 시점에서 ClassC를 구축하는 것은 여러 깊이의 인터페이스로 인해 고통 스럽습니다. 커널에 IInterfaceC를 요청하는 것이 훨씬 쉽습니다.

var newc = ApplicationScope.Kernel.Get<IInterfaceC>();

아마도 여기에서 분명한 것을 놓치고 있지만, 평범한 오래된 클래스를 만들고 그 메서드를 호출하는 것보다 이것의 이점은 무엇입니까, 아니면 정적 메서드가있는 정적 클래스일까요?

예, Bind<X>().To<Z>()모듈 없이도 바인딩을 설정하기 위해 여러 명령문을 호출 할 수 있습니다 .

The difference is that if you put these statements in a module then:

  • IKernel.Load(IEnumerable<Assembly>) can dynamically discover such modules through reflection and load them.
  • the bindings are logically grouped together under a name; you can use this name to unload them again with IKernel.Unload(string)

Maybe I'm missing something obvious here, but what is the benefit of this over just creating a plain old class and calling its method, or perhaps a static class with a static method?

For us, it is the ability to add tests at a later time very easily. Just override a few bindings with mockobjects and voila.....on legacy code without a DI that wired "everything" up, it is near impossible to start inserting test cases without some rework. With a DI in place AND as long as it was used properly where the DI wired everything up, it is very simple to do so even on legacy code that may be very ugly.

In many DI frameworks, you can use the production module for your test with a test module that overrides specific bindings with mockobjects(leaving the rest of the wiring in place). These may be system tests more than unit tests, but I tend to prefer higher level tests than the average developer as it tests the integration between classes and it is great documentation for someone who joins the project and can see the whole feature in action(instead of just parts of the feature) without having to setup a whole system).

참고URL : https://stackoverflow.com/questions/2056409/what-is-the-intention-of-ninject-modules

반응형