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!

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
scroll top