-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Property Injection for non-property setters #224
Comments
According to the documentation for this class, there really is no property, but only the getter and setter methods. Normally this difference would not be noticeable, however there's a difference with regards to the Objective-C run-time. The Objective-C run-time only provides type introspection for properties. Not for method or initializer parameters or return values. Typhoon has a feature where it can auto-wire properties for you. For example, instead of typing: [definition injectProperty:@selector(persistentStoreCoordinator) with:something]; . . . we can just type. . . [definition injectProperty:@selector(persistentStoreCoordinator)]; Similarly, the typhoon autowire macros take advantage of this, for simple "convention over configuration" cases. . . . but it will only work in the case of a true property. Therefore, I believe your change could cause this feature to be broken in the above case, as we don't really have a true property. We _can_ have single parameter methods behave like properties. But, we will need to add some testing and add a make sure that it fails correctly Example: Exception: "Property xyz is not a true property, and therefore does not \
have type information. Please use explicit wiring instead of auto-wiring)." |
Would you like to send a pull-request with the change, along with correctly handling the above case? (Default to null-type / raise exception when auto-wiring). |
Alternatively we can raise an exception, with clear instructions on how to use method injection instead. Which is better? |
Thank you. I'm sorry I haven't understood Auto-Wiring enough. I will use injectMethod:parameters: instead of injectProperty:with:. I have no excuse for wasting your time out of my ignorance. |
property Injection for non-property setters is good idea, I think I'll do that in future. |
Let's leave this open. After discussion with @alexgarbarev we think it would be good to make "properties" that consist of only a getter/setter to behave like ordinary properties. . . instead we'll raise an exception if the user asks for this type to be auto-wired. I think this will be the most useful, and least surprising behavior. |
@masatz we've released this in 2.0.7. Would you mind verifying it for us, so that we can close the ticket?
|
I installed Typhoon 2.0.7 and I verified that Thank you very much for your great work! |
I tried to inject NSPersistentStoreCoordinator(PSC) instance to NSManagedObjectContext(MOC) using TyphoonDefinition#injectProperty:with:.
I guessed it would work well because MOC has 'setPersistentStoreCoordinator' setter method, however, then application crashed with 'TyphoonPropertySetterNotFoundException'.
So, I tried next to inject PSC using injectMethod:parameters:, it works wel;.
Also, I added following codes into TyphoonIntrospectionUtils#setterForPropertyWithName:inClass: below "if (property) {...}", it works well, too:
Its basic idea is that, when class_getProperty returns null, then tries to obtain Method using class_getInstanceMethod with selector with expected setter name with format "set:", and if it return non-null Method, return the selector as setter selector.
(Please overlook poorness of my code. I'm an absolute beginner.)
Is there more suitable way to inject setters which are NOT written with @Property syntax using injectProperty:with?
Or, in these cases, I shouldn't use injectProperty:with: but injectMethod:parameters:?
The text was updated successfully, but these errors were encountered: