0
votes

I am a new Xamarin developer and i am now working on simple application that needs a database to store the application data.

My needs: Simple application, MVVM design pattern, One development. The application need to work in IOS and Android

My Solution structure:

  1. Xamarin.Forms PCL project (Contains Views, ViewModels, Models, Services)

  2. Xamarin.Droid - A default Xamarin Android project.

  3. Xamarin.IOS - A default Xamarin IOS project.

I want to use NoSQL database. I know about MongoDB, that is a nice one. BUT after searching in Google, there are no any tutorial to use MongoDB with Xamarin.Forms PCL project.

So, my Questions:

  1. It is possible to use MongoDB with Xamarin.forms?

  2. (If the answer for (1) is "Yes") - MongoDB are really recommended to use with Xamarin.Forms PCL project?

  3. (If the answer for (2) is "No") - What the best NoSQL database option in my case?

Attention: I am already tried to use this article: http://www.c-sharpcorner.com/UploadFile/093731/xamarin-with-mongodb/

But not working.

I am using: Visual Studio 2017, Xamarin.Forms (2.3.4.247).

Thank a lot!

3
What do you mean with "no working"? If you have already tried some code, it would be better to share the error message and work on it.Luis Beltran
OK Luis, In the above article, after adding the MongoHelper class, i got this errors: image.ibb.co/j6514v/Untitled.jpg And i does not found any solution to this issue. Thanks for your help. **** Of course i was add the referances: image.ibb.co/jvzHcF/Untitled2.jpgMatan Marciano
I think you forgot to add the namespaces with the "using" keyword at the top of your class.Luis Beltran
I didnt forgot. But i am just fixed this issue. But after finish the web api project. How can i deploy it to url or somthing? Thank you LuisMatan Marciano
You can publish it to Azure. Right-click on your project and you'll get that optionLuis Beltran

3 Answers

4
votes

Xamarin Forms PCL has nothing to do with MongoDB.You need a service to access the MongoDB or any other db .Ideally you could use a RESTful service to access a database

The article you have referred explains how to access MongoDB through a Web API(It is a RESTful web service). Only thing you have to worry about Xamarin is , how to call a web api in pcl. Again based on your article they use HttpWebRequest to access the web api(You can use different clients such as httpclient,restsharp etc to do this).

By the way you need to add Mongodb driver using nuget(https://www.nuget.org/packages/MongoDB.Driver/) to your web api project.

Seems you didn't properly install the driver to web api project.Also i wonder MongoCollection is a custom model class which they created for keeping the db instances.

4
votes

If your looking for a NoSQL Offline Database that works with Xamarin PCL that you can access through Models and Query using LINQ.

Realm might be what your looking for:

public class Dog : RealmObject {
    public string name {get;set;}
    public Human Owner {get;set}
}

public class Human : RealmObject {
    public IList<Dog> dogs {get;}
    public string FirstName {get;set;}
}

Realm realm = Realm.GetInstance();
Human man = new Human();
man.Firstname = "Matan";

Dog dog = new Dog();
dog.name = "Jimmy";
man.dogs.Add(dog);

using (var trans = realm.BeginWrite()){
    realm.Add(man):
    trans.Commit();
}

var people = realm.All<Human>();

foreach(var person : people){
    Debug.WriteLine(person.FirstName);
    Debug.WriteLine(person.dogs[0].name);
}

// [Output]
// Matan
// 

To use MongoDB directly you'll need to create an API that you post data to with REST

0
votes

You need to create REST server and use restful service using Nodejs or Asp.Net core application.

  1. Asp.net : https://www.c-sharpcorner.com/UploadFile/093731/xamarin-with-mongodb/
  2. Nodejs : https://www.npmjs.com/package/mongodb