I have Business Objects classes that need to know what connection string have to use.
I call/create those BO from my code passing the connection String to a connString property of the BO, and can be called/created too from framework controls, and this doesn’t allow me set connString property. (I have to call the method with one additional parameter for connectionstring)
I have something like this:
public class MyBOClass{
public FillMethodX(int ID)
{
//Fill the BO with data...
};
public FillMethodX(int ID, string connString)
{
SetConnString(connString);
FillMethodX(ID);
};
public FillMethodY(int ID)
{
//Fill the BO with data...
};
public FillMethodY(int ID, string connString)
{
SetConnString(connString);
FillMethodY(ID);
};
}
Can I avoid to replicate each method with a overload to pass a connString with a best approach? (optional parameter,generics, etc??)
Thanks.
edit: sorry I put “connection string” but really I passing a string that represents a database that my BOs use internally.
There are many ways you can do this, one of which you mentioned already – optional arguments.
Another way would be to expose a generic ConnectionString property that returns the current connection string, and then do away with the 1 parameter overload. Callers would be required to pass a connection string, and can just pass MyBOClass.ConnectionString. This may actually be a little clearer to readers of the code who wonder where the connection info comes from in the first place.