How to use singleton pattern in React Native - react-native

I'm converting an Android application to React Native and I'm new to this react-native technology.
In my android application, I have used the Singleton object to store some data. I just want to create the same for the react-native application. Inside the Java singleton object, it has a list
eg : public List<PharmacyLocationChecklistsDTO> pharmacyLocationChecklistsDTOS;.
Scenario: In my java application has wizard this singleton object store all the data of that wizard view. I have already implemented the wizard in my react native application, but I don't know how to store data until the user completes the task.
How could I create this object in my react-native app or is there any other way to handle this scenario?
Response,
"pharmacyLocationChecklistsDTOS": [
{
"chelistid": 232,
"ireqstId": 5,
"checklistName": "E",
"validity": "Invalid",
"remark": null
}
]
Sample Java Object,
public class PharmacyLocationChecklistsDTO{
public int chelistid;
public int ireqstId;
public String checklistName;
public String validity;
public Object remark;
}
public class Root{
public List<PharmacyLocationChecklistsDTO> pharmacyLocationChecklistsDTOS;
}

This is the job of a state management system.
There are popular options like Redux, MobX, React's Context API, Recoil. They will allow multiple components to consume the same state.
I'd suggest to go through them and see which caters to your needs the most.

Related

How to create Vaadin flow MprRouteAdapter for registered Vaadin 8 view with parameterized constructor?

I have the following code snippet in Vaadin 8 to navigate to ShipmentView. The shipment view is registered with two parameter constructor:
navigator.addView("shipment", new ShipmentView("name", "shipmentId"));
navigator.navigateTo("shipment");
During the migration process, we decide to migrate ShipmentView later and use MprRouteAdapter first.
if the view does not require the two parameter constructor, we can have the following adapter and navigate with code UI.getCurrent().navigate(ShipmentViewRoute .class)).
#Route(value = "shipment", layout = MainLayout.class)
public class ShipmentViewRoute extends MprRouteAdapter<ShipmentView> {
public ShipmentViewRoute () {
this.setSizeFull();
}
}
With the two parameter "name"and "shipmentId", how can I create the Adapter?
Thanks you in advance.
I think the most straightforward way is just to hard-code those parameters in the constructor (or fetch them from wherever you would get them from when initializing the navigator in the old code).

Blazor Passing List<T> between components

I am having issue with the code, I have my page layout as below.
I am communicating to database to get data for Main Content. It is List<SomeClass> that I am getting from database. Now I want same List<SomeClass> to be available for RightContent. Both components are custom and have different layout but can share same List rather than making same call twice. (Sequence is MainContent Initialized() method gets called first)
I created a service class AppDataService with below property. Also added to IServiceCollection services in startup.
public List<SomeClass> sharedListOfSomeClass = new List<SomeClass>();
In MainContent I am injecting AppDataService and assigning sharedListOfSomeClass with database values.
Now if I am injecting AppDataService in Right Content and and trying to access sharedListOfSomeClass I am getting it as null.
I know I am missing binding here because both the components are different in terms of html and can't bind it to any html tags.
Could any one please help me out to achieve this. I want to make single call to database for both the components.
If you want to have some global state of the app and share it between different components, the most reasonable way to do it is to create a State class that will contain the global state data
public class State
{
public List<SomeClass> SomeClassObjectsCollection { get; set; } = new List<SomeClass>();
}
In your Startup (or Program if you use Blazor wasm) you should add a State object as a singleton
services.AddSingleton<State>()
and on every page, where you need an access to state (or even in _Imports if you want to access it often) add
#inject State State
After that on any page you can refer to State.SomeClassObjectsCollection and get the same data.
The key point is adding a state as a singleton. If you will add is as transient or even scoped, the dependency container will create new instances of State.
One option is to pass the list to the components as parameter. Define a parameter in the component's code.
[Parameter] public List<SomeClass> sharedListOfSomeClass { get; set; }
In the parent pass the set the parameter:
<MyCustomComponent sharedListOfSomeClass="#MyVariableHoldingTheListValues" />
Other way I can think of is to make a static list and reference the static list in the components.
The scenario of the injection gives you null because the service could be registered as transient or scoped servervice. Not as singleton.

DunglasApiBundle - Trying to get the bundle to use Named Constructors rather than public constructor

I'm using the Dunglas api-platform bundle (https://github.com/api-platform/api-platform) for a new app.
Setup and installation went fine, GET requests are working.
While trying to create new objects using POST requests, I received errors about having a private constructor. My models are all made using a private constructor, and using named constructors instead.
Ideally i'm either looking for a way to have the bundle call my Named constructors, ... or someone to tell me my approach is completely wrong.
Services.yml
services:
resource.player:
parent: "api.resource"
arguments: [ "Name\\Space\\Player" ]
tags: [ { name: "api.resource" } ]
Player Object
class Player
{
private $name;
private function __construct()
{
}
public static function withName($playerName)
{
$player = new Player();
$player->name = $playerName;
return $player;
}
public function getName()
{
return $this->name;
}
}
Settings are pretty much all out of the box, following the introduction and setup in the documentation. I've skimmed through the Factory thing briefly - hoping that i'd be able to use a factory to create the objects, allowing me to call my own named constructors - but that doesn't seem to do what i think it does.
Any input regarding the use, boundaries or the setup is well appreciated.
API Platform (like most Symfony and Doctrine related libraries) is not designed to work with immutable objects like this one.
I suggest to create a typical mutable Entity as suggested in the doc:
class Player
{
private $name;
public static function setName($playerName)
{
$this->name = $playerName;
}
public function getName()
{
return $this->name;
}
}
If you really want to keep your immutable model, you'll need to implement yourself the Symfony\Component\PropertyAccess\PropertyAccessorInterface and use a CompilerPass to make API Platform using your own implementation. You will probably need to submit a patch to API Platform and to the Symfony Serializer Component to update the reference of the given object too because currently, both serializers actually update the current object and will not use the new instance returned by your with method.
I strongly encourage you to switch to typical mutable entities.

Unity3D embedded Mono with Unity

I've seen plenty of examples of calling static methods in my Unity C# code using C++. I haven't however seen any examples of how to call a single instance's method using C++. i.e rather than
public static void SomeMethod(
{
}
I really want to do:
public void SomeMethod()
{
}
I've managed to make the static implementation work by following some tutorials from but would love to know if the bottom method is possible. I've tried to add a definition for searching a method in a class.
MonoMethod* mono_method_desc_search_in_class (MonoMethodDesc *desc, MonoClass *klass);
But an implementation can't be found with the mono runtime that I was told to use from here: http://www.reigndesign.com/blog/unity-native-plugins-os-x/
Any guidance or knowledge of whether it's possible or how to do it would be appreciated.
Edit:
One other question. If I search for a gameObject, could I then use that to access the instance?
You don't say what platform you're developing for, but for iOS there's the UnitySendMessage function. I believe there are similar implementations for other platforms.
http://docs.unity3d.com/Documentation/Manual/PluginsForIOS.html
Calling C# / JavaScript back from native code
Unity iOS supports limited native-to-managed callback functionality via UnitySendMessage:
UnitySendMessage("GameObjectName1", "MethodName1", "Message to send");
The parameter must be a string, so I've used JSON to send more complex data.
Alternatively, everything that inherits from UnityEngine.Object has a GetInstanceID() method, which is guaranteed to be unique. Using this you could have a static method in C# that keeps a dictionary of recipient instances, and native code would always pass an integer ID to refer to the intended recipient.
static Dictionary<int, SomeClass> instanceDict = new Dictionary<...>();
void Awake() {
instanceDict.Add(GetInstanceID(), this);
}
void OnDestroy() {
instanceDict.Remove(GetInstanceID());
}
public static void SomeMethod(int recipientID, float someValue) {
instanceDict[recipientID].SomeMethod(someValue);
}

WCF RIA Services - loading data and binding

I've just been toying around with the new WCF RIA Services Beta for Silverlight this evening. So far it looks nice, but I've come across a few barriers when trying to retrieve data and exposing it to the UI via binding.
First of all, how am I able to get a single integer or string value from my service? Say if I have this method on my domainservice:
public int CountEmployees()
{
return this.ObjectContext.Employees.Count();
}
How am I able to make a call to this and bind the result to, say, a TextBlock?
Also, is there any way to make a custom layout for binding data? I feel a little "limited" to ListBox, DataGrid and such. How is it possible to, i.e., make a Grid with a stackpanel inside and have some TextBlocks showing the bound data? If it's possible at all with WCF RIA Services :)
Thanks a lot in advance.
To do custom methods you can use the Invoke attribute.
In the server side you declare in a domain service like this
[EnableClientAccess]
public class EmployeesService : DomainService
{
[Invoke]
public int CountEmployees()
{
return this.ObjectContext.Employees.Count();
}
}
And in your Client-side you can use it like this
EmployeesContext context = new EmployeesContext();
InvokeOperation<int> invokeOp = context.CountEmployees(OnInvokeCompleted, null);
private void OnInvokeCompleted(InvokeOperation<int> invOp)
{
if (invOp.HasError)
{
MessageBox.Show(string.Format("Method Failed: {0}", invOp.Error.Message));
invOp.MarkErrorAsHandled();
}
else
{
result = invokeOp.Value;
}
}
For the second question, you are not limited with binding. The object you get from your context can be binded with any elements you want.
You can name your class with schema classname.shared.cs and this code will also available in silverlight application.
Using Silverlight/WPF databinding engine you can build any fancy layout using datagrid / listbox containers and regular controls like textbox/label and apply your own style/skin - Example.
EDIT
Shared code cannot contain any database-related functions, only some plain calculations. If you want to retrieve this value from server then you need to make WCF method call.
At serverside you create DomainService implementation:
[EnableClientAccess()]
public class HelloWorld : DomainService
{
public string SayHello()
{
return "Test";
}
}
Then you can use this at client:
HelloWorld context = new HelloWorld();
context.SayHello(x => context_SayHelloCompleted(x), null);
void context_SayHelloCompleted(System.Windows.Ria.InvokeOperation<string> op)
{
HelloTextBlock.Text = op.Value;
}
All dirty work with making HelloWorld class available at Silverlight client is done by Visual Studio. Check hidden generated code folder.
[Invoke] attribute is obsolete in newest release of RIA services.