I have two classes. In each of them, an instance of another class is needed.

SQLite sqllite = new SQLite(config); FireBird fb = new FireBird(config, sqllite); 

those. I need

 SQLite sqllite = new SQLite(config,fb); 

But I can't do that. it has not been created yet.

I cannot change classes in some places - the same will happen. What one needs another, which has not yet been created.

Here is my SQLite class. It has a public UserData GetData() method public UserData GetData() The method takes data from SQLite and must transfer it to the fb.InsertData(ud) instance.

In turn, fb should do the same. It should be able to take data from FireBird and send it to the sqllite.InsertData(ud) instance sqllite.InsertData(ud)

  • give the code of the classes, those fragments that cause this dependence - rdorn
  • @rdorn I expanded the description, so it became clearer what the problem is or is class code needed anyway? - Dmitry Bubnenkov
  • understandably. overlooked with you classes. You need to decouple the logical data model from accessing the database. Do I understand correctly that all this is needed to synchronize FB and SQLite databases? - rdorn
  • Yes it is. There is even a good 3 databases (stupidly so necessary) and it is necessary that all three had copies of each other, so that the data could be sent - Dmitry Bubnenkov
  • Actually VladD already answered, this is exactly what you need. What if the customer will need another ten more synchronous databases to connect? - rdorn

1 answer 1

That is, you are trying to link the two classes responsible for different databases, and feed each instance of the other in the constructor? This does not work out, some of the instances you have to create first.

Of course, it would be possible to “pierce by force”, and inject the necessary copies after creation. But this is still a bad way: these classes in your architecture know too much about each other.

I would advise doing this:

  1. The SQLite and FireBird classes do not know about each other.
  2. The logic of receiving data from one and transmitting to another is in a class external to them, which has links to both, and works with them.
  • I once had a friend say that for this purpose IDBRepository is used in this steppe? - Dmitry Bubnenkov
  • @Suliman: I would say that this is just business logic. It can access databases either directly through SQLite and FireBird , or through repository, as you like. - VladD
  • so, and how to be. Suppose I create an interface in which I describe the Connect, GetData, InsertData methods, how to be if I create a new class GetInsertData and then implement all these methods there. They will not be tied to a specific database. It turns out that it is easier for each database to create its own methods and not to fool with instances of classes? GetDataFromPG InsertDataToSQLite, etc. - Dmitry Bubnenkov
  • @Suliman Well, you also have a list of methods that you need to implement, describe them with the DBProvider interface. In the data model, put for example List <DBProvider> and call interface methods in a loop for each database, well, or what is your logic of these calls. You need to change the database engine - you add the provider to it and change it, you need to add a new one - the same way. There is more code, yes, but the benefits will be noticeable earlier than you will face the impossibility of adding another new crutch - rdorn