내 응용 프로그램에서 Ninject와 NamedScopeExtension을 사용하고 있습니다. 개체 그래프에서 더 깊은 개체 중 하나는 명명 된 범위를 정의한 루트 개체에 대한 액세스가 필요합니다. DefinesNamedScope()
은 InNamedScope()
을 암시하지 않으며 대신 루트를 요청할 때 새로운 루트 개체가 만들어집니다.Ninject : 공장에서 NamedScope의 루트 객체에 접근하는 방법
예이 예에서
using System;
using Ninject;
using Ninject.Extensions.NamedScope;
using Ninject.Syntax;
namespace NInjectNamedScope
{
public interface IScopeRoot
{
Guid Guid { get; }
void DoSomething();
}
public interface IFactory
{
Guid Guid { get; }
IOther CreateOther();
}
public interface IOther
{
void SayHello();
}
internal class ScopeRoot : IScopeRoot
{
private readonly IFactory m_factory;
private readonly IResolutionRoot m_kernel;
public Guid Guid { get; private set; }
public ScopeRoot(IFactory factory, IResolutionRoot kernel)
{
m_factory = factory;
m_kernel = kernel;
Guid = Guid.NewGuid();
}
public void DoSomething()
{
Console.WriteLine("ScopeRoot.DoSomething(): Entering");
Console.WriteLine("ScopeRoot GUID: {0}", Guid);
Console.WriteLine("IFactory GUID: {0}", m_factory.Guid);
var other = m_factory.CreateOther();
Console.WriteLine("ScopeRoot.DoSomething(): Other created");
other.SayHello();
Console.WriteLine("ScopeRoot.DoSomething(): Exiting");
}
}
internal class Factory : IFactory
{
private IResolutionRoot m_kernel;
public Guid Guid { get; private set; }
public Factory(IResolutionRoot kernel)
{
m_kernel = kernel;
Guid = Guid.NewGuid();
}
public IOther CreateOther()
{
return m_kernel.Get<IOther>();
}
}
internal class Other : IOther
{
private readonly IScopeRoot m_root;
private readonly IFactory m_factory;
public Other(IScopeRoot root, IFactory factory)
{
m_root = root;
m_factory = factory;
}
public void SayHello()
{
Console.WriteLine("Other.SayHello(): Hello");
Console.WriteLine("Our IScopeRoot has GUID: {0}", m_root.Guid);
Console.WriteLine("Our IFactory has GUID: {0}", m_factory.Guid);
}
}
public class MainClass
{
public static void Main(string[] args)
{
var kernel = new StandardKernel();
kernel.Bind<IScopeRoot>().To<ScopeRoot>().DefinesNamedScope("RootScope");
kernel.Bind<IFactory>().To<Factory>().InNamedScope("RootScope");
kernel.Bind<IOther>().To<Other>().InNamedScope("RootScope");
var root = kernel.Get<IScopeRoot>();
root.DoSomething();
Console.ReadKey();
}
}
}
, 루트 마찬가지로 Other
가 Factory
의 동일한 인스턴스를 수신하지만, ScopeRoot
의 새로운 인스턴스 대신 지정된 범위를 정의 기존 인스턴스를 주입 생성된다.
어떻게하면 공장에서 명명 된 범위의 루트에 액세스 할 수 있습니까? 이 예제는 단순화되어 있습니다. 실제로 범위 루트와 팩토리 메서드 사이에는 여러 레이어의 개체가 있으므로 단순히 this
을 전달할 수는 없습니다.
당신의 솔루션은 매력처럼 작동합니다. 정말 고마워요! 두번째 제안 ('IBindingRoot.Bind(). () .WithParameter (new NamedScopeParameter (scopeName));')가 작동하지 않으므로 응답에서 제거하겠다는 자유를 얻었다. –
jeyk