0

I has a C# library like this:

public SomeClass<T> : ISomeClass<T>{
    public SomeClass(IMainClass mainClass){ this._mainclass=mainClass }
}

public MainClass : IMainClass {
    private Dictionary<string,dynamic> _myDic;
    public MainClass() { /* some code */ }
    public ISomeClass<T> ReturnSomeClass()
    {
        if(_myDic.Contains(ISomeClass<T>)
          return _myDic.Value //**someclass<T> type
          //add to _myDic
        return new SomeClass<T>();
    }
}

public OtherClass<T> : IOtherClass<T> {
    public OtherClass(ISomeClass<T> someClass) { this._someClass=someClass }
}

Next, I register this classes in Microsoft Unity for DI.

class Boot{
    public void Registering(){
        container.RegisterType<ISomeClass,SomeClass>();
        container.RegisterType<IMainClass,MainClass>();
        container.RegisterType<IOtherClass,OtherClass>();
   }
}

When my client class is Calling container.Resolve< IMainclass >(), ctor in mainClass is fire and a instance of MainClass is created.Now where container.Resolve< IOtherClass >() is calling in client class ctor for that is fire and by DI a new SomeClass is created.As well as by DI on creating SomeClass, ctor for this class is calling and a new mainClass is created.

how can be only one MainClass created object on both Resolve by container in my client class?

Amin Sahra
  • 15
  • 3
  • I am trying to understand your question. So `SomeClass` depends on `IMainClass`? And then `MainClass.ReturnSomeClass` can create instances of `SomeClass`? which instance of `IMainClass` do you want to inject into `SomeClass`? How did you setup the service locator? – Yacoub Massad Nov 24 '15 at 17:08
  • 1
    By the way, [the service locator is an anti-pattern](http://yacoubsoftware.blogspot.com/2015/10/object-composability-another-reason-why.html) and you might want to change your design to use Dependency Injection instead. – Yacoub Massad Nov 24 '15 at 17:09
  • If you define patterns as anti-patterns just because there are some situations where it do not fit, then YES it's an anti pattern. But with that [reasoning all patterns would also be anti patterns](http://stackoverflow.com/questions/22795459/is-servicelocator-anti-pattern) – Amin Sahra Nov 24 '15 at 18:08
  • Service Locator is an anti-pattern because there is simply no scenario where it is the best solution; there is ALWAYS a better solution; that is what makes it an anti-pattern. – Steven Nov 24 '15 at 18:22
  • Also note my comment on jgauffin's answer explaining why his reasoning is false and his counter example in fact **is not** an example of the Service Locator pattern. – Steven Nov 24 '15 at 18:25
  • @SaeedAdabzadeh, My note about the service locator was just a note, it could be useful for you, but maybe your are in a situation were you can't refactor. In my first comment I asked you some questions because I want to understand your question. The question is still not clear to me. Can you please rephrase the question and make it more clear? – Yacoub Massad Nov 24 '15 at 19:02
  • @YacoubMassad.I think everything is simple.Here are 3 classes which are interdependent and by DI this dependecy should fixed.problem is just this:both mainClass and OtherClass must-have the same object of someClass per each resolve. – Amin Sahra Nov 24 '15 at 19:33
  • @SaeedAdabzadeh, `MainClass` does not have a field of type `ISomeClass`. Do you mean that `MainClass.ReturnSomeClass` should always return the same instance of `SomeClass` ? What is the relation between `MainClass` and `OtherClass` I cannot see them in the same object graph. – Yacoub Massad Nov 24 '15 at 19:41
  • @YacoubMassad,OtherClass have relation to SomeClass and SomeClass have relation to MainClass.MainClass have a dictionary that stored created object by ReturnSomeClass() method.if dictionary contain ISomeClass then object stored in dictionary returned otherwise a new object will be returned. – Amin Sahra Nov 24 '15 at 19:51
  • @SaeedAdabzadeh, you didn't mention the dictionary in your question. Also in the code of `ReturnSomeClass`, you are not using any dictionary. I still cannot understand how is `ReturnSomeClass` related to your question. – Yacoub Massad Nov 24 '15 at 20:06
  • @YacoubMassad,I update my question – Amin Sahra Nov 24 '15 at 20:27
  • @SaeedAdabzadeh, you say "ctor in mainClass is fire and a instance of someClass is created". I cannot see that in the code, the ctor in main Class is parameterless. The code seems to have many missing parts. – Yacoub Massad Nov 24 '15 at 20:47
  • @SaeedAdabzadeh, your code has to be [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) – Yacoub Massad Nov 24 '15 at 20:48

1 Answers1

0

If you want register ISomeClass as singleton, set lifeTimeManager

Container.RegisterType(ISomeClass, SomeClass, null, new ContainerControlledLifetimeManager());
galakt
  • 1,374
  • 13
  • 22
  • by ContainerControlledLifetimeManager() created object is in whole life app.this not my desired behavior.I want to object created from each resolve new one . but both mainClass and OtherClass have the same object of someClass. – Amin Sahra Nov 24 '15 at 17:36