Which Clean Architecture layer do factories belong to? - oop

I suppose the title speaks for itself but in which layer of Clean Architecture do you typically put factories?
If the factory interface does not go in the same layer as the implementation I would like to know this also.
Thanks!

A factory interface is considered to be an abstract concept for creating a domain entity, thus belongs to domain core circle.
A factory implementation deals with technical details of entity creation, thus belongs to infrastructure circle.
Often, the entity returned by a factory is represented by an interface or an abstract class placed within domain circle, whereas the concrete entity class is placed within infrastructure circle.

In which layer of Clean Architecture do you typically put factories?
A factory is a concept that does not belong to one architectural layer. You can use it whereever you want to decouple one layer or architectural boundary from another.
It is used when a client in one architecturaly context needs to instantiate objects of another architectural conext that it should not depend on. E.g. entities use framework or library objects.
It is also often used by the application startup or main component. For details take a look here.
If the factory interface does not go in the same layer as the implementation I would like to know this also.
The interface is often placed beside the client that uses it, because an interface is made for a client and not an implementor. A client needs someone to do something. Therefore the client tells what it wants with an interface. The implementor shows how it is done.
+--------+ ||
| client | ||
+--------+ ||
| ||
V ||
+-----------+ || +-------------+
| Interface | <-||--- | Implementor |
+-----------+ || +-------------+
|| = architectural boundary
Sometimes you want to have an interface that serves many clients in different architectural contexts. Then you can either separate the interface from both contexts.
+----------+ ||
| client 1 | ||
+----------+ ||
||
| ||
======|==========||=================
| ||
V ||
||
+-----------+ || +-------------+
| Interface | <-------- | Implementor |
+-----------+ || +-------------+
||
^ ||
| ||
======|==========||==================
| ||
||
+----------+ ||
| client 2 | ||
+----------+ ||
or you can use separate interfaces for each client and let the implementor implement both.
+----------+ ||
| client 1 | ||
+----------+ ||
| ||
V ||
+-----------+ ||
| Interface | <---------------+
+-----------+ || |
|| +-------------+
=================|| | Implementor |
|| +-------------+
+-----------+ || |
| Interface | <---------------+
+-----------+ ||
^ ||
| ||
+----------+ ||
| client 2 | ||
+----------+ ||
You can also use 2 implementors. One for each interface.

Related

Is there a way to alter all columns in a SQL table to have 'utf-8' format

I'm using Spark and I found that my data is not being correctly interpreted. I've tried using decode and encode built-in functions but they can be applied only to one column at a time.
Update:
An example of the behaviour I am having:
+-----------+
| Pa�s |
+-----------+
| Espa�a |
+-----------+
And the one I'm expecting:
+-----------+
| País |
+-----------+
| España |
+-----------+
The sentence is just a simple
SELECT * FROM table

index match in SQL

Sorry in advance. I am very new in SQL. I don't know how to do a simple task like excel index match equivalent in SQL.
I have 2 tables in SQL (Please ignore the dash, I was using it to align the columns)
Table1
| Name | Limit1 || Limit2 |
| First | A || 05 |
| Second | B || 10 |
| Third | || 10 |
Table2
| Limit1Key|| Limit1Value || Limit2Key ||Limit2Value|
| A || 20,000 || 02 ||2,000,000 |
| B || 50,000 || 05 ||5,000,000 |
| || || 10 ||10,000,000 |
I want to get a final table looking like below.
Result Table
| Name || Limit1 || Limit2 |
| First || 20,000 || 5,000,000 |
| Second || 50,000 || 10,000,000 |
| Third || || 10,000,000 |
If there is already another post similar to this, please guide me to it.
Thank you!
If I understand correctly, you just want two joins:
select t1.*, t2_1.limit1value, t2_2.limit2_value
from table1 t1 left join
table2 t2_1
on t2_1.limit1key = t1.limit1 left join
table2 t2_2
on t2_2.limit2key = t1.limit2;

How do I find out which exchange routed a message last?

We are intending to build up a one way network of exchange-to-exchange bindings. Our requirement is to attach the route a message took to its header, but there seems to be no way to find out which exchange handled a message last.
I already tried looking up the information using the tracing functionality and there also exists a plugin that subscribes to the internal basic.publish event. Yet, all these ways just give me the exchange of first entry.
I even took a look at the rabbitmq-server source code and it seems like there is no possible extension point inside the routing function (see headers exchange routing for example). I am not an Erlang dev, so maybe there is an Erlang way of intercepting/extending functions to be called?
Example
+---------+
| |
| POE |
| |
+--+--+---+
| |
+-------+ | | +-------+
| | | | | |
| EX1 +---+ +----+ EX2 |
| | | |
+--+----+ +---+---+
| |
| |
| |
+--+----+ +---+---+
| | | |
| QU1 | | QU2 |
| | | |
+-------+ +-------+
For a message that ends up in QU2 we would like to have a header field like this:
{...
x-route, ["POE", "EX2"]
}
This could probably be accomplished with a RabbitMQ plugin but it would be difficult to do: a channel interceptor would have to effectively do a part of the routing to determine the "last" exchange. There could be more than one "last" exchange, as well.
NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.

SQL Column as a "View Column"

I have the following table security of securities:
id | bbg_ticker | ticker | currency | exchange | type
-------+------------------+----------+----------+----------+--------
1762 | 653 HK Equity | 653 | | HK | Equity
5734 | LO US Equity | LO | | US | Equity
6175 | H US Equity | H | | US | Equity
4563 | BDTY Index | BDTY | | | Index
6253 | MOA Comdty | MOA | | | Comdty
7414 | 1333 JP Equity | 1333 | | JP | Equity
7538 | 2377 TT Equity | 2377 | | TT | Equity
As you can guess, the Bloomberg ticker (column bbg_ticker) is actually the concatenation of:
ticker + exchange + type if exchange is not null
ticker + type else
This is obviously an irrelevant duplicating of data. For instance, one could one day change the exchange of a security without changing its bbg_ticker, which would lead to a failure in the data integrity.
As I am currently refactoring my database, I was wondering if I could do something like a "view" but with a column (not a whole table). This view would replace the bbg_tickercolumn (would be named the same), and would be defined for instance with (I know the following request is totally wrong):
CREATE COLUMN VIEW bbg_ticker
ON security AS (
ticker
|| (IF exchange IS NOT NULL THEN (' ' || exchange) ELSE '')
|| ' '
|| type
)
;
It seems than VIEWS cannot be defined this way, but would you see another solution which would lead to the same result?
I know I could use check constraints and regex, which would solved the integrity question, but not the duplicating one. My question is more about having a better solution.
I am on PostgreSQL 9.5.
Thank you
You can just create a view from the whole table:
CREATE VIEW improved_security AS
SELECT id, concat_ws(' ', ticker, exchange, type) AS bbg_ticker,
ticker, currency, exchange, type
FROM security;
The concat_ws() function does all the nasty string concatenation for you, with the appropriate separator (a space in this case) and ignoring NULLs.
If you need to maintain the name security in order not to break any applications, then you should rename the table (which will require a table lock for a brief period of time) and then create the view with the name security and grant appropriate permissions to the view and revoke the same from the table. Wrap this all inside of a transaction so you will not get any errors on the client side, at best a brief delay in putting or pulling data.

OOP inheritance issue

I have two classes, and the methods in them are shown below;
|----AVL----| |-----RB------|
| | | |
| | | |
| - insert | | -balance |
| | | |
| - balance | | |
| | | |
|-----------| |-------------|
inside "insert" method of AVL, it calls "balance".
RB inherits AVL, so I can use insert method of AVL. Now when I call RB::insert(), it calls AVL::insert() & then AVL::balance(), but I want it to call RB::balance() from AVL::insert(), when a RB object calls "insert".
This is a classic case for virtual methods: make AVL.balance virtual and override it in RB. The correct implementation will then be called depending on what type of object calls balance -- it doesn't matter that the code that calls balance will be written as part of AVL.