1

I am trying to build a RESTful API (using Flask) that will be later consumed by a mobile application. I also have a requirement to build a Web Admin. The Web Admin will have functionality that will not be available for the mobile client. The mobile client and Web Admin will also share the same database.

I have a limitation:

  • I do not know any front-end framework, I will be building the Web Admin using Flask (combining the backend/frontend).

My questions are:

  • For the functionalities that are similar for the mobile and web admin, how can avoid duplicating the code?
  • How can I maintain the database? I will be using an ORM, does this mean I have to write the DB models twice? One for the RESTful API and one for the Web Admin that will be directly talking to the database.

EDIT: found similar questions

  • random downvotes. I wish people are decent to tell my why. – wasp8898 May 30 at 7:51
  • Frankly, I don't understand your questions at all. It's unclear why code duplication would even be a problem in your particular situation, and as the answers below have said, you maintain the database like you would any other database. – Robert Harvey May 30 at 14:41
  • @RobertHarvey its okay, the two answer below helped me with my duplication question. :) – wasp8898 May 31 at 3:02
3

Q: For the functionalities that are similar for the mobile and web admin, how can avoid duplicating the code?

Essentially, encapsulating the domain and the business logic in their own components and modules so the web layer acts as a driver or facade. This is what web interfaces are meant to be, an extension which allows our business expand up to the WWW.

How we do it? Following the principle of stable dependency. In a nutshell, we turn the domain and the business into our stable dependency so all the dependencies point out toward these two.

[Web] -----> [Business] ----> [Domain] 

This way we could add more facades or UIs

[MVC web]  --------+
[REST web] --------+-->[Business] ----> [Domain] 
[Command Line] ----+

Note: The arrows show the direction of dependency, not data flows.

Q: How can I maintain the database?

As you would in any other application, encapsulating and decoupling the persistence from the domain and the business. How? Making persistence to point out toward stable dependencies.

               [Domain] 
                  ^
                  |
[Web] ---> [Business] <---- [Persistence] 

Q: I will be using an ORM, does this mean I have to write the DB models twice? One for the RESTful API and one for the Web Admin that will be directly talking to the database.

No, if you decouple the persistence from anything else, you could have a single persistence data model for the business. This model, ideally should not be accessible to outer layers. It's a lot of work I know. Different data models for different layers is costly and involves several mappings.

As for the web layer, my advice is different. Implement two different view data models. One for the MVC interface and one for the REST interface. The reason is simple, both interfaces are going to evolve at different rates, time and in different directions due to different reasons and actors. Canonical data models rarely works in software engineering. They work for a while but they don't last for too long because UI layers are volatile. A single view model will constraint the way both web interfaces evolve.

Even if we start with a single view model, my experience on the field tell me that it will diverge eventually.

0

For the first answer, i'd suggest creating a "service layer" that can be called by both RESTful API methods and controllers methods for the web admin, if your plan was serve web pages directly by the server. However it's a better practice to separate back-end and front-end. In this case, the back-end will have only to expose RESTful API that will be called by both web admin and app.

For the latter question: in general, orm objects should be distinct from domain objects, but if want to use them as also for domain, then you won't have to duplicate them as the web admin and the app will interact only with the api (or the web controller)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.