0

一直以来,我一直在为此付出努力,所以我开始认为自己创建了一个反模式。尽管如此,DI创建模式

//Register self 
container.Register(Component.For<IWindsorContainer>().Instance(container)); 
//Register all 
container.Register(Component.For<IService1>().ImplementedBy<Service1>()); 
container.Register(Component.For<IService2>().ImplementedBy<Service2>()); 
//etc 

IService1 
{ 
    //blabla 
} 
IService2 {IService1 Service1{get;}} 

因此IService1和IService2可以创建没有任何特殊的。 从IService3开始,涉及IProject。

IProject{} 
//Resolve a service that, amongst other things, relies on an IProject 
IProjectGet 
{ 
    T Get<T>(IProject proj) 
     where T : class; 
} 
//Impl 
ProjectGet : IProjectGet 
{ 
    IWindsorContainer _cont; 
    public ProjectGet(IWindsorContainer cont){_cont=cont} 

    public T Get<T>(IProject proj) 
    { 
     //Resolve using the main (and only) container and pass the IProject 
     return _cont.Resolve<T>(new {p = proj}); 
    } 
} 

这是不行的,只有主服务与 “P =凸出”和任何其他依赖的主要服务有,这也依赖于该项目解决了, 导致异常话说项目服务没找到。

IService3 
{ 
    IService2 Service2{get;} 
    IProjectGet ProjectGet{get;} 
    IProjectLevelStuff SetActiveProject(IProject proj); 
} 
Service3 : IService3 
{ 
    IService2 Service2{get;private set;} 
    IProjectGet ProjectGet{get;private set;} 

    public Service3(IService2 s2, IProjectGet p) 
    { 
     ProjectGet = p; 
     Service2 = s2; 
    } 

    public IProjectLevelStuff SetActiveProject(IProject proj) 
    { 
     return ProjectGet.Get<IProjectLevelStuff>(proj); 
    } 
} 
ProjectLevelStuff : IProjectLevelStuff 
{ 
    IProject Project{get;private set;} 
    IService4 Service4 {get;private set;} 

    public ProjectLevelStuff(IProject p, IService4)//etc. 
} 
IService4 
{ 
    IService2 Service2{get;} 
    IService5 Service5{get;} 
    IService6 Service6{get;} 
    IProject Project{get;} 
} 
IService5{IProject Project{get;}} 
IService6{IProject Project{get;}} 

失败的原因只有ProjectLevelStuff得到的IProject传递给它,并自IService4和它的依赖也需要它,则抛出异常。即使这确实起作用,我也不喜欢它,因为每个依赖IProject的服务都被迫调用我想避免的那个参数'p'。

我只想继续使用我已经拥有的服务,但是这次添加了作为可解析依赖项传递给我们的通用Get方法的IProject实例。我找不到任何方法来复制容器并创建一个新的容器,然后添加主容器作为一个小孩不会改变任何东西(依赖项仍然丢失)。这是如何完成的?

温莎城堡确实有一个TypeFactory内置,但它基本上做同样的事情,我已经做了什么,并没有解决任何问题。我发现唯一的'解决方案'是创建一个新的容器并重新注册类型,但这次通过主容器解决它们(当然除了IProject)..这是作品中的维护噩梦。

更新:我添加了一些单元测试,以低于希望澄清了一些事情

+1

你好Natli,这是一个非常糟糕的想法通过容器内的容器。也许如果你尝试用TypedFactory重写代码,其他人可能会更容易帮助你。目前的代码对我来说很复杂,但看起来你需要一种束缚或范围的生活方式。 – Marwijn 2013-04-25 07:34:09

+0

@Marwijn嘿,我已经达到了一个非常不满意的解决方案,但我会添加它作为答案,希望能够澄清一些混淆。 – natli 2013-04-25 13:46:40

回答

0

一些奇怪的(但可能是有效的)原因温莎儿童开启的容器可以访问他们的父容器而不是反过来我的答案。这意味着为了使用新容器中的主容器中注册的服务,我们必须设置主容器的父级而不是新容器的父级。

这是非常不方便的,因为一个容器只能有一个Parent。

internal class ProjServices : IProjServices 
{ 
    private readonly IKwProject _proj; 
    private readonly IWindsorContainer _mainCont; 

    public ProjServices(IKwProject proj, IWindsorContainer mainCont) 
    { 
     _mainCont = mainCont; 
     _proj = proj; 
    } 

    public T Resolve<T>() 
    { 
     T rett; 

     //Create new container 
     var projCont = new WindsorContainer(); 
     //Register new service 
     projCont.Register(Component.For<IKwProject>().Instance(_proj)); 

     //Set hierarchy 
     lock (_mainCont) 
     { 
      projCont.AddChildContainer(UiContainer); //ui needs project, set parent to projCont 
      UiContainer.AddChildContainer(_mainCont); //main needs ui, set parent to uiCont 

      //Resolve using main, which now has access to UI and Project services 
      try 
      { 
       rett = _mainCont.Resolve<T>(); 
      } 
      finally 
      { 
       projCont.RemoveChildContainer(UiContainer); 
       UiContainer.RemoveChildContainer(_mainCont); 
      } 
     } 

     return rett; 
    } 

    private static readonly object UIContainerLock = new object(); 
    private static volatile IWindsorContainer _uiContainer; 
    private static IWindsorContainer UiContainer 
    { 
     get 
     { 
      if(_uiContainer==null) 
       lock(UIContainerLock) 
        if (_uiContainer == null) 
        { 
         //Register the UI services 
        } 
      return _uiContainer; 
     } 
    } 
} 

现在,如果我想在未来,我想我会得到再次卡住,由于单亲,唯一的事情....我怎么做正确使用甚至较新的容器这些新容器这个,请吗?

UPDATE:

2010年和2012年的VS单元测试:

ServiceTest。拉链(798 KB) https://mega.co.nz/#!z4JxUDoI!UEnt3TCoMFVg-vXKEAaJrhzjxfhcvirsW2hv1XBnZCc

或者复制&粘贴:

using System; 
using Castle.MicroKernel.Registration; 
using Castle.Windsor; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 

namespace ServiceTest 
{ 
    /// <summary> 
    /// A service that doesn't rely on anything else 
    /// </summary> 
    public interface IService1 
    { 
    } 
    class Service1 : IService1 
    { 
    } 

    /// <summary> 
    /// The Project 
    /// </summary> 
    public interface IProject 
    { 
     string Name { get; } 
    } 
    public class Project : IProject 
    { 
     public Project(string name) 
     { 
      Name = name; 
     } 

     public string Name { get; private set; } 
    } 

    /// <summary> 
    /// A Service that relies on a Project 
    /// </summary> 
    public interface IService2 
    { 
     IProject Project { get; } 
     string GetProjectName(); 
    } 
    /// <summary> 
    /// The implementation shows it also relies on IService3 
    /// </summary> 
    public class Service2 : IService2 
    { 
     public Service2(IProject project, IService3 service3) 
     { 
      Project = project; 
      Service3 = service3; 
     } 

     public IProject Project { get; private set; } 
     public IService3 Service3 { get; private set; } 

     public string GetProjectName() 
     { 
      return Project.Name; 
     } 
    } 
    /// <summary> 
    /// IService3 is a Service that also relies on the Project 
    /// </summary> 
    public interface IService3 
    { 
     IProject Project { get; } 
    } 
    public class Service3 : IService3 
    { 
     public Service3(IProject project) 
     { 
      Project = project; 
     } 

     public IProject Project { get; private set; } 
    } 

    /// <summary> 
    /// Class1 uses the service without any dependencies so it will be easy to resolve 
    /// </summary> 
    public class Class1 
    { 
     public Class1(IService1 service1) 
     { 
      Service1 = service1; 
     } 

     public IService1 Service1 { get; private set; } 
    } 

    /// <summary> 
    /// Class2 also uses that service, but it also relies on a Project ánd IService2 
    /// which as you know also relies on the Project and IService3 which also relies on 
    /// the Project 
    /// </summary> 
    public class Class2 
    { 
     public Class2(IService1 service1, IProject project, IService2 service2) 
     { 
      Service1 = service1; 
      Project = project; 
      Service2 = service2; 
     } 

     public IProject Project { get; private set; } 
     public IService1 Service1 { get; private set; } 
     public IService2 Service2 { get; private set; } 
    } 

    /// <summary> 
    /// Set up the base services 
    /// </summary> 
    [TestClass] 
    public class UnitTestBase 
    { 
     protected WindsorContainer Cont; 

     [TestInitialize] 
     public void BaseSetup() 
     { 
      Cont = new WindsorContainer(); 
      Cont.Register(Component.For<IService1>().ImplementedBy<Service1>().LifestyleTransient()); 
      Cont.Register(Component.For<IService2>().ImplementedBy<Service2>().LifestyleTransient()); 
      Cont.Register(Component.For<IService3>().ImplementedBy<Service3>().LifestyleTransient()); 

      Cont.Register(Component.For<Class1>().LifestyleTransient()); 
      Cont.Register(Component.For<Class2>().LifestyleTransient()); 
     } 

     [TestMethod] 
     public void Class1_Resolves() 
     { 
      Cont.Resolve<Class1>(); 
     } 
    } 

    /// <summary> 
    /// Set up the base unit tests 
    /// </summary> 
    [TestClass] 
    public class UnitTestClass2Base : UnitTestBase 
    { 
     protected void RunTest3Times(Func<string, IWindsorContainer> getContainer) 
     { 
      const string projNameBase = "MyProjectName"; 
      Func<int, string> getProjectName = i => projNameBase + i; 

      for (var i = 0; i < 3; i++) 
      { 
       var pName = getProjectName(i); 
       GetClass2ForProject(getContainer(pName), pName); 
      } 
     } 

     protected void GetClass2ForProject(IWindsorContainer cont, string projName) 
     { 
      var c2 = cont.Resolve<Class2>(); 

      Assert.IsTrue(c2.Project.Name == projName); 
      Assert.IsTrue(c2.Service2.Project.Name == projName); 
      Assert.IsTrue(c2.Service2.GetProjectName() == projName); 
     } 
    } 

    /// <summary> 
    /// This will fail on the second request because we cannot 
    /// overwrite the earlier registration. And iirc containers can't 
    /// be altered after the first resolve. 
    /// </summary> 
    [TestClass] 
    public class Attempt_1 : UnitTestClass2Base 
    { 
     [TestMethod] 
     public void Class2_Resolves_Project_Scoped_Requests() 
     { 
      RunTest3Times(s => 
       { 
        Cont.Register(Component.For<IProject>().Instance(new Project(s))); 
        return Cont; 
       }); 
     } 
    } 

    /// <summary> 
    /// It looks like we have to create a new container for every Project 
    /// So now the question remains; how do we get to keep using the base IService implementations 
    /// in the container that is scoped for the IProject? 
    /// </summary> 
    [TestClass] 
    public class Attempt_2 : UnitTestClass2Base 
    { 
     static IWindsorContainer CreateContainer(IProject p) 
     { 
      var ret = new WindsorContainer(); 
      ret.Register(Component.For<IProject>().Instance(p)); 
      return ret; 
     } 

     /// <summary> 
     /// This will fail because the services in the main 
     /// container can't access the IProject in the new container 
     /// </summary> 
     [TestMethod] 
     public void Class2_Resolves_Project_Scoped_Requests_1() 
     { 
      RunTest3Times(s => 
      { 
       //Add the project container as a Child to the Main container 
       var projCont = CreateContainer(new Project(s)); 
       Cont.AddChildContainer(projCont); 
       return Cont; 
      }); 
     } 

     /// <summary> 
     /// Doing the previous approach the other way around works. 
     /// But now we can only resolve one thing at a time 
     /// </summary> 
     [TestMethod] 
     public void Class2_Resolves_Project_Scoped_Requests_2() 
     { 
      IWindsorContainer projCont = null; 

      //Add the Main container as a Child to the project container 
      // (in other words set the Parent of Main to Project) 
      // and then resolve using the main container. 
      //A container can only have one parent at a time so we can only 
      // resolve one scoped thing at a time. 
      RunTest3Times(s => 
       { 
        if (projCont != null) 
         projCont.RemoveChildContainer(Cont); 

        projCont = CreateContainer(new Project(s)); 
        projCont.AddChildContainer(Cont); 
        return Cont; 
       }); 
     } 

     /// <summary> 
     /// The only way around that issue seems to be to register all project-dependent 
     /// services in the new container. Then re-register all original services 
     /// in the new container and pass the resolving on to the main container; 
     /// a maintenance nightmare and especially painful for named registrions. 
     /// </summary> 
     [TestMethod] 
     public void Class2_Resolves_Project_Scoped_Requests_3() 
     { 
      Func<IProject, IWindsorContainer> createContainer2 = p => 
       { 
        var contNew = new WindsorContainer(); 

        //Pass resolving of the non-dependent services on to the main container. 
        // this way it will respect it's lifestyle rules and not create new 
        // instances of services we wanted to use as a singleton etc. 
        contNew.Register(Component.For<IService1>().UsingFactoryMethod(() => Cont.Resolve<IService1>()).LifestyleTransient()); 
        contNew.Register(Component.For<Class1>().UsingFactoryMethod(() => Cont.Resolve<Class1>()).LifestyleTransient()); 

        //Register the dependent services directly in the new container so they can access the project 
        contNew.Register(Component.For<IService2>().ImplementedBy<Service2>().LifestyleTransient()); 
        contNew.Register(Component.For<IService3>().ImplementedBy<Service3>().LifestyleTransient()); 
        contNew.Register(Component.For<Class2>().LifestyleTransient()); 

        contNew.Register(Component.For<IProject>().Instance(p)); 

        return contNew; 
       }; 

      RunTest3Times(s => 
      { 
       var projCont = createContainer2(new Project(s)); 
       return projCont; 
      }); 
     } 
    } 
} 
+0

目前我还不清楚你试图达到什么目的。儿童容器是Windsor的先进功能,通常不是必需的。 ProjServices公开了Resolve 方法,这不应该是必需的。这可能是因为您将容器用作服务定位器。你能写一个简单的测试来展示你想要达到的目标吗? – Marwijn 2013-04-26 04:42:54

+0

我加了单元测试,谢谢:) – natli 2013-04-26 12:14:38

1

,请检查您是否可以使用使用范围下面的方法:

[SetUp] 
public void Setup() 
{ 
    int counter = 0; 

    _container = new WindsorContainer(); 
    _container.AddFacility<TypedFactoryFacility>(); 
    _container.Register(
     Component.For<IService1>().ImplementedBy<Service1>().LifestyleScoped(), 
     Component.For<IService2>().ImplementedBy<Service2>().LifestyleScoped(), 
     Component.For<IService3>().ImplementedBy<Service3>().LifestyleScoped(), 
     Component.For<Class1>().LifestyleTransient(), 
     Component.For<Class2>().LifestyleTransient(), 
     Component.For<IProject>().ImplementedBy<Project>().LifestyleScoped().DynamicParameters((k, d) => d["name"] = "MyProjectName"+counter++) 
     ); 
} 

[Test] 
public void TestClass1() 
{ 
    using (_container.BeginScope()) 
    { 
     Class1 object1 = _container.Resolve<Class1>();; 
     var object2 = _container.Resolve<Class1>(); 
     Assert.AreNotSame(object1, object2); 

     Assert.AreSame(object1.Service1, object2.Service1); 
    } 
} 

[Test] 
public void TestClass2() 
{ 
    Class2 object1; 
    using (_container.BeginScope()) 
    { 
     object1 = _container.Resolve<Class2>(); 
     var object2 = _container.Resolve<Class2>(); 
     Assert.AreNotSame(object1, object2); 

     Assert.AreSame(object1.Project, object2.Project); 
     Assert.AreSame(object1.Service2.Project, object2.Service2.Project); 
    } 

    Class2 object3; 
    using (_container.BeginScope()) 
    { 
     object3 = _container.Resolve<Class2>(); 
    } 

    Assert.AreNotSame(object1.Project, object3.Project); 
} 
+0

感谢您花时间写出来,我以前没有遇到过LifestyleScoped。在您的代码中,项目名称是为每个范围自动创建的。你打算如何以这样一种方式进行这项工作,即服务实现可以定义项目想要的名称? – natli 2013-04-30 10:01:35

+0

为什么不使用服务构造函数呢? – Marwijn 2013-05-01 14:51:00

+0

那我该如何保留'LifestyleScoped'?我必须将服务中的项目名称传递给容器,以便它可以将其更新,这意味着我会失去'LifestyleScoped',因为它出现在您的示例中。 – natli 2013-05-02 14:49:40