Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 views
in Technique[技术] by (71.8m points)

c# - Windsor LifeStyle - Shared instance per Graph

I have 2 types of ViewModel's

      public class ViewModelA 
      {
          IService service;
          private ViewModelB childViewModel; 

           public ViewModelA(IService service,ViewModelB childViewModel)
           {
               this.service = service;
               this.childViewModel = childViewModel;
           }

           public ViewModelB ChildViewModel
           {
                get { return childViewModel; } 
            } 
      }  

      public class ViewModelB 
      {
          IService serivce;  
          public ViewModelB(IService service)
          {
              this.service = service;
          }  
      } 

I have a Service registered into a Windsor Container :

     public class Service : IService {}

     container.Register(Component.For<IService>()
                  .ImplementedBy<Service >().LifeStyle.Transient); 

I want ViewModelA and ViewModelB to share the same instance of IService.

I Do not wan't all instances of ViewModelA and ViewModelB to share the same instance.

Each Parent/Child Pair would have his own instance , i wan't to achieve this using DependencyInjection can this be done ?

I wan't this to be be done through Dependency Injection since i have an entire hierarchy of ViewModels under A and not just one (B) viewmodel.

VM A -> VM B -> VM C -> VM D ... (and let's say ill go over the all alphabet) all these need to share the same instance of IService.

and another instance of A and it's decedents would share a a different instance of IService.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may be able to use Scoped Lifestyles. Here's an example of some unit tests that seem to do what you want:

[Fact]
public void VMsInSameScopeSharesService()
{
    var container = new WindsorContainer();
    container.Register(Component.For<ViewModelA>().LifestyleTransient());
    container.Register(Component.For<ViewModelB>().LifestyleTransient());
    container.Register(Component
        .For<IService>().ImplementedBy<NullService>().LifestyleScoped());

    using (container.BeginScope())
    {
        var a = container.Resolve<ViewModelA>();

        Assert.Equal(a.service, a.childViewModel.service);
    }
}

[Fact]
public void VMsInDifferentScopesDoNotShareServices()
{
    var container = new WindsorContainer();
    container.Register(Component.For<ViewModelA>().LifestyleTransient());
    container.Register(Component.For<ViewModelB>().LifestyleTransient());
    container.Register(Component
        .For<IService>().ImplementedBy<NullService>().LifestyleScoped());

    IService service1;
    using (container.BeginScope())
    {
        var a = container.Resolve<ViewModelA>();

        service1 = a.service;
    }
    IService service2;
    using (container.BeginScope())
    {
        var a = container.Resolve<ViewModelA>();

        service2 = a.service;
    }

    Assert.NotEqual(service1, service2);
}

However, this is quite an exotic requirement, which makes me wonder why you want it to behave exactly like this, or if you couldn't structure your code in a way that would make this simpler.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...