Abstract Factory Pattern of Creational Design Patterns for .Net Core

Samet Çınar
3 min readNov 1, 2021

--

Problem:

Our application works with Oracle database. But after an agreement made by the company, after a year, it is necessary to switch to SQL instead of Oracle. At the same time, the transition to SQL may result in a return to Oracle after a few years. A different database may come one day tomorrow. That is a separate problem for us.

Solution :

Each database has a unique “connection, command” structure. In other words, a SQL provider first needs a “connection” for the “command” it will process. Since there will be no “command” without a “connection”, we consider them as a whole.
This is where the “Abstract Factory Pattern” comes into play.

Product can be called DB Provider for us.

We will have two DB providers. If we take our Oracle provider, we will see that it shows a formation with “OracleConnection, OracleCommand” in it.

Let me give you another example to make it come alive in your mind. Think of a living room set, it contains a three-person, a double-seat and a single-person seat.

When you buy this product family under a single name, it contains sub-products. Abstract Factory does this for us, we request the main product. It brings us related sub products.

Commands, Connections for Providers

Our main product is a DB provider (Oracle or SQL), we planned that a DB provider will emerge with connection and command.

Let’s take creating a SQLProvider here as an example.

SQLProvider has to inherit “BaseProvider” and override “CreateCommand” and “CreateConnection” methods, we make sure that the provider creates a connection and the command that uses that connection.

So we created SQLProvider and OracleProvider, where do we decide which one will respond to our queries at runtime?

We will find a solution by using the “Factory Method” design pattern that we described in our previous article.

We created a class named ProviderFactoryMethod and according to the parameter that will come to this class, is it SQLProvider or OracleProvider ? We will decide this. Reflection use or swich case use can be preferred.

At the same time, if the incoming parameters do not correspond to any value, it is a point that will help us in creating the default provider selection of the application.

Finally, we need a class that will talk to Provider Factory Method and manage our queries. This class will find the parameter to decide the provider, talk to Provider Factory Method and create the provider.

When the ProviderManager select query is created, it creates the relevant link, makes the command run depending on this link, and finalizes the operation.

Query test

The query structure here will of course vary according to the provider. These customizations need to be made within their own classes.

What I wanted to show was how to solve such a problem with “Abstract Factory Pattern”..

Repo url :
https://github.com/gsmtcnr/DesignPattern.Creational.AbstractFactory

Yazının Türkçe kaynağı için :
http://www.sametcinar.com/abstract-factory-pattern-of-creational-design-patterns-for-net-core/

--

--