mercredi 24 août 2016

Designing a generic query builder interface (Design Pattern)

This going to be a very long question.

Introduction

I want to design a service for my project. I want to design it properly using the right design patterns and following good coding techniques wherever required and make it such that it is easy to understand, read, modify, extend..... in short it should comply with the design principles in general. I am a beginner programmer and have recently started reading the OOP concepts and design.

Brief details of what we are doing...

In our project, we want to show graphs related to various information that we gather. We are designing various types of graphs like the hour's graph, the day's graph, the month's graph and the year's graph. Each graph shows aggregated data for the respective granularity and each graph being generated using different queries. As we move forward, more and more types of graphs would be needed with further analytic.....

Data storage

The data is stored simply in one table ---> with the details and a column for the date, a column for the hourOfTheDay and the value. This way, we have hourly data for various things. We also have one more table: aggregatedThingData_month. As the name says at the end of each month, we run a cron(we may move to more sophisticated scheduling tools like quartz etc further ) job aggregating the data for the month and storing the value in this table.....

What I want to design & the problem in short

A service/ API for the generation of the graphs. I want to serve different types of requests for different graphs. The request will have the required granularity of the graph and then I have to parse the request, build the appropriate query, get the result set (each type of graph will include different information), process the result set, and send the response(JSON) in the required format.

What I want to achieve (From whatever I understand about a good design)?

From what I understand about good design, it is basically that the code should be re-usable, it should be generic, it should easily be extended and easy to read/ understand (clear....), it should not be tightly coupled etc etc.

From the tasks mentioned above, I think my should have following things:

  1. Depending upon the granularity level, the query should be appropriately generated, dynamically. As we grow with different types of graphs, it should be easily extensible. There should be a class like Query which will have some details and then the QueryBuilder or something like that. There must be concrete QueryBuilders. (may be a Factory/ Builder and Strategy pattern can be used here)....

How can I dynamically build queries and make these interfaces generic.....?(Code to interface, not to implementation....)

  1. Currently we are using Postgres, but there is a high possibility that we move over to MongoDb I want to design it into layers, such that just changing the underlying logic about building the queries, obtaining db Connections etc for different types of db without affecting upper layers.... I need not change everything if need arises to switch among the dbs.

When db is switched, appropriate queries for that db are built -> executed -> resultset processed -> response sent

I want to model such that, no matter what query we require, just providing the appropriate parameters like required fields, joins, conditions, grouping etc will be able to build the necessary query. So a generic Interface for Query Generation and their concrete implementations.....

i.e

"building the specific query" will be a concrete class?.

  1. Just as it is with building query and executing it, I want it to be generic with processing of the result set. How should I define the interface. How should It implement concrete classes for processing of result sets different graphs? Response format may also change from JSON to XML dynamically or may be any other format.

As I am new to programming and have just started to learn designing I need a bit of help and guidance in application of what I have learnt. Simple hints or pointers will help me a lot. This exercise will help me improve my designing skills and help me understand how to think and take decisions and considerations while designing an application.

Aucun commentaire:

Enregistrer un commentaire