Giter VIP home page Giter VIP logo

Comments (3)

dotnetjunkie avatar dotnetjunkie commented on June 3, 2024

The behavior you are experiencing has nothing to do with the hybrid lifestyle but rather with how Simple Injector handles singletons. Singleton instances are constructed together with their compiled expression trees, as the final expression tree of a singleton registration is a ConstantExpression, which refers to a pre-initialized instance—that singleton instance.

Upon compilation, a hybrid lifestyle registration takes the expression trees of both underlying registrations and wraps it in a ConditionalExpression. But since in your case one of the lifestyles is a singleton, that ConditionalExpression wraps the ConstantExpression that references the initialized singleton instance.

This means that, even if the singleton is not resolved, it has to be initialized even if the compiled condition doesn't hit the singleton case.

By the way, I hope that your Lifestyle.CreateHybrid(Lifestyle.Scoped, Lifestyle.Singleton) lifestyle is just something you use for testing this scenario, because I'd say that using this in your production application is not something I would typically advice. What you are basically simulating here is behavior similar to that of Autofac, where a scoped instance is resolved from the container's 'root scope' in case there is no active scope. Simple Injector very explicitly doesn't allow this, because this can easily lead to bugs, such as multi-threading issues.

If you wish, can you elaborate on your specific case why you need this? Perhaps I can feedback on your design and provide better alternatives either for your design or for the Simple Injector registrations.

from simpleinjector.

FDUdannychen avatar FDUdannychen commented on June 3, 2024

@dotnetjunkie I agree the design is probably not a good idea, my purpose is to refactor a legacy system, keeping existed configurations available. Each configuration file has two parts: the shared configs (defined in the top of the config file), and the per-job configs, something like this:

<Config Name="Config1" Value="Value1" />
<Config Name="Config2" Value="Value2" />
<Job1>
    <Config Name="Config1" Value="OverrideValue1" />
    <Config Name="Config3" Value="Value3" />
</Job1>
<Job2>...

In the application there is a ConfigReader : IConfigReader, in a job's context (there is a SimpleInjector scope), the ConfigReader is resolved to a scoped instance with corresponding configurations. If there is no job context, the ConfigReader is resolved to a singleton instance that only reads shared configs. That's why I want a hybrid lifestyle.

My original design is a ConfigReaderFactory where you can create ConfigReader from different configuration pieces. It works fine but due to some legacy code, I need to pass configurations deep through the method calls to make ConfigReaderFactory work, that's annoying. I want a ConfigReader that can be used anywhere in the code without worrying about configs anymore.

Any suggestion about this? Thanks.

from simpleinjector.

dotnetjunkie avatar dotnetjunkie commented on June 3, 2024

I'm sorry for my late response. I find it difficult to suggest anything here, as anything I'd suggest would probably be difficult to implement in your legacy system. I would suggest finding a solution that doesn't involve using hybrid lifestyles, for instance having a separate IJobConfigReader abstraction and implementation registered scoped, with the 'core' reader being the IConfigReader being registered as singleton, but that likely requires changing the way ConfigReader currently works, or how jobs are provided with a specific version, which might be too much to chew off for you.

from simpleinjector.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.