Search code examples
c#azuremobileazure-mobile-services

Same business logic for web app and mobile client using Azure App Service


I am doing research on an application that needs web application as well as mobile app and am considering to use Azure App Service.

In the current implementation, we need to write the same logic in mobile app and as well as in web application. I want to use the same repository and database for both the endpoints. But I don't want to write the same code for the business logic in two different places.

How can I mix web application and mobile app using the same repository, database, and business logic ?

If Web API is used I can create a service layer which can be used both in Web API and MVC 5 application. I want a similar system to that.


Solution

  • You've already got your answer, ie. use APIs. If you structure your solution in a decoupled architecture, you'll be able to write as many front ends as you like, reusing the same back end services. Consider a web app and mobile app to be front ends - they should be presentation layers only with minimal logic for controlling the UI, and no business logic. A typical solution that decouples in this way would look like the below. Use interfaces to describe the APIs, so you can easily mock, refactor or re-work back end services without effecting the apps which consume the APIs.

    sketch of decoupled architecture

    • Web App
    • Mobile App
    • API project (WebAPI/REST recommended)
    • Interfaces to the services
    • Business services (reusable, long lasting, robust and well tested/testable)
    • Repository (to abstract the db connection)
    • Other abstractions (web services, other services/systems)

    So your business logic can be written once, and fronted by the interfaces and/or the REST api, which is called by your separate web apps/mobile apps. If you use a web api as the mechanism to join the apps to the services, you will have one physical deployment of your business services; if you just use interfaces, you will need to deploy your binaries to each instance (web apps, mobile apps, etc)