Hi,
I find myself using static methods more than I probably should, so I am
looking for some advice on a better approach.
For example, I am writing an app that involves quite a bit of database
operations on purchase orders and inventory. I have created a PurchaseOrder
class and Inventory class to encapsulate operations like creating POs,
finding items, etc. These two classes are used extensively from different
parts of the app.
In order to not have to create instances of these classes from all classes
that use them, I created a Database class with static members holding
instances of the PurchaseOrder and Inventory classes (and some database
objects used in various places).
Using this approach, looking up an item number is very easy, and the code is
easy to read:
itemNumber = Database.Invent ory.GetItemNumb er(barcode);
Or to load a purchase order:
if (Database.PO.PO Exists(poNumber ))
{
Database.PO.Loa d(poNumber);
}
Is there a better approach that would be about as easy to use? Other
advice?
--- Here is the Database class:
public class Database
{
private const string TASK_ID = "purchasing ";
private const string CONNECTION_STRI NG = "the conn. string...";
private static MySqlConnection _mySqlConnectio n1;
private static MySqlDataAdapte r _mySqlDataAdapt er1;
private static DataSet _dataSet1;
private static PurchaseOrder _purchaseOrder;
private static Inventory _inventory;
public static string TaskId
{
get { return TASK_ID; }
}
public static MySqlConnection Connection
{
get { return _mySqlConnectio n1; }
}
public static MySqlDataAdapte r DataAdapter
{
get { return _mySqlDataAdapt er1; }
}
public static DataSet DataSet
{
get { return _dataSet1; }
}
public static PurchaseOrder PO
{
get { return _purchaseOrder; }
}
public static Inventory Inventory
{
get { return _inventory; }
}
static Database()
{
_mySqlConnectio n1 = new MySqlConnection (CONNECTION_STR ING);
_mySqlConnectio n1.Open();
_dataSet1 = new DataSet();
_mySqlDataAdapt er1 = new MySqlDataAdapte r();
_purchaseOrder = new PurchaseOrder() ;
_inventory = new Inventory();
}
}
Thanks in advance,
Laban
I find myself using static methods more than I probably should, so I am
looking for some advice on a better approach.
For example, I am writing an app that involves quite a bit of database
operations on purchase orders and inventory. I have created a PurchaseOrder
class and Inventory class to encapsulate operations like creating POs,
finding items, etc. These two classes are used extensively from different
parts of the app.
In order to not have to create instances of these classes from all classes
that use them, I created a Database class with static members holding
instances of the PurchaseOrder and Inventory classes (and some database
objects used in various places).
Using this approach, looking up an item number is very easy, and the code is
easy to read:
itemNumber = Database.Invent ory.GetItemNumb er(barcode);
Or to load a purchase order:
if (Database.PO.PO Exists(poNumber ))
{
Database.PO.Loa d(poNumber);
}
Is there a better approach that would be about as easy to use? Other
advice?
--- Here is the Database class:
public class Database
{
private const string TASK_ID = "purchasing ";
private const string CONNECTION_STRI NG = "the conn. string...";
private static MySqlConnection _mySqlConnectio n1;
private static MySqlDataAdapte r _mySqlDataAdapt er1;
private static DataSet _dataSet1;
private static PurchaseOrder _purchaseOrder;
private static Inventory _inventory;
public static string TaskId
{
get { return TASK_ID; }
}
public static MySqlConnection Connection
{
get { return _mySqlConnectio n1; }
}
public static MySqlDataAdapte r DataAdapter
{
get { return _mySqlDataAdapt er1; }
}
public static DataSet DataSet
{
get { return _dataSet1; }
}
public static PurchaseOrder PO
{
get { return _purchaseOrder; }
}
public static Inventory Inventory
{
get { return _inventory; }
}
static Database()
{
_mySqlConnectio n1 = new MySqlConnection (CONNECTION_STR ING);
_mySqlConnectio n1.Open();
_dataSet1 = new DataSet();
_mySqlDataAdapt er1 = new MySqlDataAdapte r();
_purchaseOrder = new PurchaseOrder() ;
_inventory = new Inventory();
}
}
Thanks in advance,
Laban
Comment