Pass parameter to constructor - NativeLookup.create - codenameone

How can I pass parameter to constructor of Native code in Codename one?
I could only see NativeLookup.create method which takes no parameter. The requirement is to create native object based on parameter (like port number or url).
NativeCode nativeCode = (NativeCode) NativeLookup.create(NativeCode.class);
It would be even great if there is any way to get instance from a factory method which takes parameter. Thanks in advance.

You can't and you don't need to!
The native interface is a simple interface. If you have an object that needs a constructor argument just create it within the native object methods. Keep in mind that passing an argument to a constructor might work well for Android but when you want to do the same thing in iOS's Objective-C etc. this might not make sense.

Related

Changing data type of a 3rd party react component property?

I have a component that works fine but one of the props is type "string" and I would like to pass a component or simply make the data type "any". Is there a way to do this?
To make this more clear, I am trying to modify the behavior of a third-party class. This is in this case very safe and I have changed the 3rd party code directly but it is much better, if possible in Typescript, to inherit and in the inherited class, change the data type of the prop so that I can, instead of simply displaying text, display, for example, a list.
Well, imo passing in type any usually defeats the purpose of using Typescript in the first place. The way I understand it , you have two options:
Separate the props: have a prop of type string and another of type ReactNode since it is a component you are planning on passing in.
If for some reason it has to be a single prop, use string | ReactNode as its type. Although I would recommend option 1 over this.
Thinking even further, you might want to consider not passing a component as a prop at all(for performance reasons). Designing your components such that you can work with doing regular ES6 import to get in a component would be much much better, but again this depends on your usage and design.

How do I get Autofac delegate factories to work with obfuscation?

We're updating code to use Autofac. We'd like to use custom delegate types to define factories instead of Func's. But we also use an obfuscator, which renames parameters. We'd like to tell the Autofac container to match by type instead of name as it does with Func's. Is this possible?
On the official documentation of Autofac, you have this information
By default, Autofac matches the parameters of the delegate to the parameters of the constructor by name. If you use the generic Func types, Autofac will switch to matching parameters by type.
http://docs.autofac.org/en/latest/advanced/delegate-factories.html
Could you customize the obfuscator to use the same name for the parameter name of the delegate and the constructor?
As mentioned, the way to do it right now is to use Func. Even if it were possible with just delegate factories, Autofac won't know what to do when there are two parameters of the same type. Here's my answer to a very similar question.
Thanks guys. We ended up creating a custom registration source using Autofac's source as a guide. In our testing, if there are two parameters of the same type, it appears to fall back to order.

SilverStripe: Creating form fields statically and via the 'new' keyword

I've been trying to find some info on difference between instantiating form fields through static method and the new keyword. Can somebody tell me what are the practical implications, limitations, between new MyFormField and MyFormField::create() esp. with regards to SilverStripe
Using the create factory method would check for overloads (set via Object::useCustomClass()) and return an instance of the custom class in that case.
This method first for strong class overloads (singletons & DB
interaction), then custom class overloads. If an overload is found, an
instance of this is returned rather than the original class. To
overload a class, use Object::useCustomClass()
So using the create method rather than instantiating the Object yourself would provide a possibility to overload the used Class without altering the code.
see
http://api.silverstripe.org/3.1/class-Object.html#_useCustomClass
http://api.silverstripe.org/3.1/class-Object.html#_create

Builder vs Factory Method pattern

I was reading about Builder pattern and as usual I got confused with Factory pattern.
I have seen a good article which shows the difference between Abstract Factory and Builder pattern.
http://champika-nirosh.blogspot.in/2008/04/what-is-difference-between-abstract.html
But my confusion is that, more than Builder pattern similar to Abstract Factory I feel like it is similar to Factory Method pattern. Not sure my understanding is correct. But in Factory Method also we are using a separate factory (Method in Concrete Factory) to create one particular object (not a family of product). In that scenario how Builder differs from Factory Method pattern. I knew that Builder requires more steps to create object, other than that is there any particular scenario we need to use one over another? Please guide me.
Thanks.
Your particular usage case will affect which (if either) you might choose. But to essentially reiterate what's on the link you reference:
Both abstract factory and factory method are creating instances of unknown types. Generally these will be returning a class that corresponds to an interface, but you don't know (and shouldn't care) what the concrete type will be. To use the picture from that link, it's using a WindowsFactory, meaning the abstract factory is returning an instance that is compatible with Windows. If your factory were instead a LinuxFactory, you might get an object back that works on Linux. Note, also, that you probably wouldn't know if you had a LinuxFactory or WindowsFactory, just that you had a Factory of a particular type.
So the abstract factory & factory method patterns are about building polymorphic types (including when you don't know or care what the concrete type will be). However, the call to get an instance of the type is usually trivial. To build from a factory you're probably just calling:
MyInterfaceType myInstance = myFactory.getTheItemOfMyInterfaceType();
The builder pattern is more about building complex objects that may or may not be (but probably are) of a known type. In this case you'd expect a complex series of calls to construct the type, often setting one parameter after another. Because there's a lot of known parameters and arguments, you generally know what type of object you're going to get back from it (don't have to, but it's more likely than with an abstract factory). Builder is used when constructing the object is complex, but not necessarily polymorphic (it might be polymorphic, but that's not the premise of the pattern). A builder call to construct something might be (see Android AlertDialog for some real examples):
Builder b = new Builder();
b.setSomeValueA(myChoiceForA);
b.setSomeValueB(myChoiceForB);
MyInterfaceType myInstance = b.build();
Hope that helps.

WP7 NavigationService.Navigate pass a pointer without using global variables?

I am navigating to page X in WP7. I have an object (let's call it banana) in my ViewModel, which is where the NavigationService.Navigate call is being made. Page X needs a reference to the banana. How can I do that?
The answer to this question recommends using the global App class. Not a good option for me because I might have multiple instances of the class of page X, and I wouldn't want to confuse other instances if they are later navigated to.
I would also prefer not to have to serialize the banana.
If there could be multiple instances of the page then you'll need to pass any parameters it needs as part of the querystring in the Uri you use for navigation.
You can either use the query string (to send the id, for example) as suggested by #Matt, and you could also send the object itself via a message for example, you can use the Messenger class from MVVM Light for that.
Hope this helps :)

Resources