How to implement the Observer standard in practice, with database?

Asked

Viewed 416 times

4

I’m studying some design patterns, and right now I’m learning about the pattern Observer. I’ve read books, I’ve seen some classes on Youtube, I’ve done the examples and everything.

But now I would like to implement in practice inserting data into the database, to use in a project that I am currently working on.

My situation: I have a base with 3 tables(Setor, Pessoa, PessoaSetor). It is a relationship "many for many". Every sector can have more than one responsible, this information is in the table Personal.

What I need: every time a person is registered in a sector, send a notification to those responsible for the sector. In this case, the notification may be a table by table.

  • 1

    The best thing would be for you to give a situation, a problem you want to solve by default. It is possible to implement an Observer in banks in numerous ways, using stored procedures/functions or triggers, will depend on the case.

  • 1

    [Update] I put my problem I want to solve

  • This is the perfect situation to use triggers (which are essentially "standard" Observers of rdbms’s), if you specify which database you are using will facilitate because the use varies by type.

  • @Fábiolima The answer solved your problem? Do you think you can accept it? If you don’t know how, check out the [tour] how to do this. This would help a lot to indicate that the solution was useful to you and help everyone entdner it. You can also vote for anything on the entire site.

1 answer

3

The question doesn’t go into too much detail, it’s too generic, so I can’t give a very precise answer, but basically the standard Observer is applied in database using the triggers feature.

Every widely used relational database has the command CREATE TRIGGER where an action to be executed is defined when a database change occurs, exactly what determines this pattern. One of these actions is to call some routine, an executable and in some databases it is possible to only send a notification to the connected client(s). It depends on the bank to know if it will be simpler and more appropriate to do something like this. See the specific documentation from your database.

It may seem like a simple and obvious solution to use, but using triggers is full of tricks and even experienced developers can fall into traps. I’m passing the technique, but not saying it’s the best way to handle it. As always, it all depends on the case. Like any design pattern it can be abused.

Some Dbs may have other mechanisms that do something similar, for example: mysql event.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.