Comments (3)
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.
@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.
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)
- Is there a way to check if the Scope was disposed? HOT 5
- Using Microsoft DI and Simple injector the other way around (reversed crosswire) to integrate with NServiceBus? HOT 1
- How to handle mixed constructor parameters with multiple instances? ex. (DependentSvc svc, string p1, string p2) HOT 2
- d__15 MoveNext Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. HOT 1
- Single constructor requirement incompatible with ASP.NET Core HOT 2
- Resolution of IEnumerable<T> for non registered service T. HOT 1
- Hybrid Async Scoped MVC 5 + Async Method bug HOT 12
- First-chance exception thrown when registering open-generic type with type constraint HOT 3
- Degraded Register performance when AllowOverridingRegistrations is set to true HOT 10
- Exception thrown when attempting to inject COM interface HOT 4
- Remove COM support
- How to register third party constructor with runtime string arguments HOT 7
- What would happen in case of duplicate registrations? HOT 1
- @inject support in .NET Core MVC views HOT 9
- ThreadScopedLifestyle vs AsyncScopedLifestyle HOT 1
- Sharing Instances between Parent and child scopes HOT 4
- Add debug information for Scopes HOT 2
- How to use decorator for shared interface? HOT 4
- What's best way to get .net 3.5 SimpleInjector Decorator Memory Leak bug fix? HOT 1
- Open-generic registrations for types with generic array generic type arguments can't be resolved
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from simpleinjector.