Comments (7)
You can do this very thing using a provider.
[self bindBlock:^(JSObjectionInjector *context) {
return [context getObject:[MyClass class]];
} toProtocol:@protocol(MyProtocol)];
I introduced the provider pattern to solve these kind of unique problems. With the intention of integrating them into Objection if the pattern is recurring.
from objection.
That's Great. Thanks.
Please can you clarify something for me. In my use-case I need to be able to use the functionality available from Modules numerous times during the application lifecycle. For example I might want to use bind:toClass during initialisation and again at some point later. Given that the only time a module can be used is when creating an injector, I was planning on using a fresh module and injector each time it was needed, safe in the knowledge that all injectors shared the same context, so bindings declared in a module passed to one injector would be available to subsequent injectors, however in the README you state:
An injector manages its own object context. Which means that a singleton is per injector and is not necessarily a true singleton.
So is there any way to have persisted singletons and be able to add bindings after the initial injector is created?
from objection.
You could have a global injector that manages the singletons that you want to persist using a provider.
[self bindBlock:^(JSObjectionInjector *context) {
return [gGlobalContext getObject:[MyClass class]];
} toClass:[MyClass class]];
Though I'm not familiar enough with your particular situation to say whether this is what you are looking for.
from objection.
Ok. Let me put it another way:
Is it possible to register an eager singleton in a module passed to Injector 1, and have subsequent injectors supply the same instance of the same class to objects they create? In other words are singletons only respected if the same injector that consumed the module in which they were registered?
from objection.
Subsequent injectors would have different instances. In order to achieve what you are doing you'd have to configure an injector with singletons that you want to persist across other injectors and keep a reference to it. Subsequent injectors would have to be configured with a different module that created provider bindings to get access to the global singletons.
For example,
@interface ApplicationModule : JSObjectionModule
@end
@implementation ApplicationModule
- (void)configure {
[self bindInstance:someObject toClass:[MyClass class]];
... More bindings ...
}
@end
@interface EagerSingletonBindingsApplicationModule : ApplicationModule
@end
@implementation EagerSingletonBindingsApplicationModule
- (void)configure {
[super configure];
[self registerEagerSingleton:[Singleton class]];
}
@end
@interface ProviderBindingsApplicationModule : ApplicationModule
@end
@implementation ProviderBindingsApplicationModule
- (void)configure {
[super configure];
[self bindBlock:^(JSObjectionInjector *context) {
return [[JSObjection globalInjector] getObject:[Singleton class]];
} toClass:[Singleton class]]
}
@end
- (void)someBootsrappingMethod {
EagerSingletonBindingsApplicationModule *eagerSingletonBindingsModule = [[[EagerSingletonBindingsApplicationModule alloc] init] autorelease];
JSObjectionInjector *injector = [JSObjection createInjector:eagerSingletonBindingsModule];
[JSObjection setGlobalInjector:eagerSingletonBindingsModule];
}
from objection.
That's Perfect. Really appreciate you taking the time to put me on the right path.
from objection.
No problem. You are using Objection in a relatively complex manner and I can see where you would have questions.
from objection.
Related Issues (20)
- Support setting properties of non-object types
- Does objection inject dependencies automatically on creation? HOT 2
- frida ๅฝไปค้่ฏฏ
- Compatibility with Swift HOT 3
- Reseting Objection's global context storage caches HOT 2
- Injection at Initialization HOT 3
- Inject property declared in a category.
- Sharing singleton scope when deriving a new injector HOT 1
- Ability to specify override objects HOT 5
- @objection_initializer not passing arguments HOT 2
- Injecting object with protocol and superclass
- about sample? HOT 1
- why not use modern objective-c
- Why "JSObjectionModule.m" file line fifth to write "#import <Objection/JSObjectionProviderEntry.h>", but not "#import "JSObjectionProviderEntry.h"".
- why JSObjectionEntry's injector property is "assign" not "weak"?
- Passing nil arguments for get objection object HOT 7
- inScope:JSObjectionScopeSingleton doesn't work since Cocoapods version 1.6 HOT 19
- Build is broken on XCode 6.4 HOT 4
- Getting a reference to the JSObjectionInjector which created an object? HOT 2
- Undeclared selector warnings
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 objection.