I have a static class that I use as my “Data Utils” for my project.
This project has several forms and classes, multiple of which make calls to this Data Utils class/the database.
I don’t want to violate the DRY principle, so I don’t want to have multiple OracleConnection components, one plopped onto each form.
I also don’t want to violate cohesion by having my Data Utils class gain “carnal” knowledge of my main form and access it for the OracleConnection.
I could create a dynamic OracleConnection inside each Data Utils method, but that, too, would violate DRY.
Is my best solution to convert my static class to non-static, give it an OracleConnection member, and instantiate that in the constructor?
UPDATE
For future generations, this is what I did, based on LukLed’s suggestion:
internal class GreatAmericanNovelistsData
{
private static OracleConnection oc;
static GreatAmericanNovelistsData()
{
oc = new OracleConnection();
oc.ConnectionString = "User Id=SCLEMENS;Password=HucKfiNn;Server=HANNIBAL;Pooling=True;Min Pool Size=0;Max Pool Size=10;Connection Lifetime=0;Direct=True;Sid=HANNIBAL;Service Name=HANNIBAL;";
oc.Direct = true;
}
You can define static constructor and initialize connection only once.
OracleConnectionobject can be static too. Details here.