POPULAR DESIGN PATTERN

 1. There are two popular design pattern:

- Repository

- Service layer


2. Why we should use design pattern ?

There are 5 reasons to finally learn design patterns:

- They give the developer a selection of tried and tested solutions to work with

They are language neutral and so can be applied to any language that supports object-orientation

- They aid communication by the very fact that they are well documented and - can be researched if that is not the case.

- They have a proven track record as they are already widely used and thus reduce the technical risk to the project

- They are highly flexible and can be used in practically any type of application or domain


3. The meaning and benefits of Repository Design Pattern

Meaning:

- RD is a intermediate layer between Business Logic (BL) and Data Source (DB). The intermediate layer’s objects is Repository. They bring the standardization to output and separate absolutely the handle between business logic and data access logic, make BL do not concern the DB work absolutely.

Benefits:

- Make your code cleaner and clear
- When you change to user MongoDB instead of using Mysql before, you only need to change query execution in the repository file instead of changing query execution in each controller which ralates to database. So it is more simple and easy.


4. How to use Repository design pattern

Normal controller


Usually, with a view to displaying data on view, we simply write a contronller and make query execution in order to get data from database in this controller.

Repository




However, with repository design pattern is different. When you have a request which calls to controller,  controller will call to repository and after that repository will call to model in order to get data and handle. Lastly, if  controller want to get  data, it only needs to call to repository.


5. The meaning and benefits of service layer

Meaning

- A "service layer" exists between the UI and the backend system that store data. It is in charge of managing the business rules of transforming and translating data between those two layers.

- Service layer, also called command handler, is the right place to compose and handle all the business logic responsible for fulfilling a user's ‘intent’ (end user's goals for taking an action in your application).

I- t allows you represent user's actions, as intents that are understood by all stakeholders and can be passed to our application as commands that should be handled. It allows us have a single point where all business entities (domain models) that are responsible for fulfilling user’s goals can converge and fulfill those goals.


Benefits

Makes our application easy to manage, maintain and test.


6. How to use Service Layer design pattern

- I create Post CRUD application which use service layer and repository
- I have two models Post Model and Tag Model

Post Model


Tag Model


PostRepositoty


TagRepository



PostService

For example, when I crate a new post, I will allow the users stick the tags for post, the param which convert to server is names variable with array type. I will check the tags which have already been in database and only save the new tags.



PostController

When you have already handled the logic in PostService, so your controller is extremely clean


If you liked this article

Let's subscribe the updates of Scuti!
Share on Google Plus

About Tuan Thanh

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 Comments:

Post a Comment