1

I am new to the Xamarin technology, and I am working on an Asp.NET Core Web application which basically delivers micro-services which will be consumed by a Xamarin.Forms application.

So my Web API will offer quite a few operations, and each of them would use their own model class to represent their response and their parameters.

What bothers me is that I will have a lot of model class to duplicate from the API to the Xamarin application. That's not the end of the world, but I was wondering if there wasn't some kind of mechanism to do that smoothly ? I initially thought about putting all my models in a single library which would be referenced by both the web API and the app but it seems to go against the Xamarin good practices.

I was thinking about something like swagger-codegen, which is capable of generating a SDK based on the different endpoints exposed by an API, but from what I saw it doesn't seem suitable for Xamarin.

Any insight would be much appreciated!

Toto
  • 143
  • 3
  • *` I initially thought about putting all my models in a single library which would be referenced by both the web API and the app but it seems to go against the Xamarin good practices.`* - One of the reasons libraries exist is the ability to share code across multiple projects. Which good practices do you think it violates? What specific concerns do you have about doing this? – Ben Cottrell Nov 12 '17 at 16:23
  • My concern doing this is that from what I understood about the recommended designs of Xamarin applications, there should be the platform-specific projects (as many as supported platforms obviously), and the shared-code, in my case a PCL, where the shared-code and the Xamarin Forms go. The thing is, PCL are described as the place where the Forms should be added, so I figured it was going against the design to add another PCL which would contain only models, which would be shared by both API and Xamarin App, correct me if I'm wrong? – Toto Nov 12 '17 at 21:10

1 Answers1

2

Defining a contract (datamodels, parameters, etc.) that both the API and the Xamarin application can adhere to is not a bad practice, it's a must. The only place this contract can reside without the API referencing the Xamarin App or vice versa is in its own library. In your case, the library type seems to be a portable class library (PCL) which is consumable by both parties. Note that this is only the contract, the internal models (if needed) can live within the App/API. The contract and the internal models are not always a one-to-one mapping that's why the "if needed" is in parentheses.

Jay Elston
  • 2,680
  • 22
  • 30
Iza Atsou
  • 36
  • 1
  • 4
  • 1
    Yes. I would only add that I think .net standard might be appropraite for the models lib? Plus I would supply a client library as well for the xamarin app to use – Ewan Nov 17 '17 at 08:48