L
Laban
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.Inventory.GetItemNumber(barcode);
Or to load a purchase order:
if (Database.PO.POExists(poNumber))
{
Database.PO.Load(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_STRING = "the conn. string...";
private static MySqlConnection _mySqlConnection1;
private static MySqlDataAdapter _mySqlDataAdapter1;
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 _mySqlConnection1; }
}
public static MySqlDataAdapter DataAdapter
{
get { return _mySqlDataAdapter1; }
}
public static DataSet DataSet
{
get { return _dataSet1; }
}
public static PurchaseOrder PO
{
get { return _purchaseOrder; }
}
public static Inventory Inventory
{
get { return _inventory; }
}
static Database()
{
_mySqlConnection1 = new MySqlConnection(CONNECTION_STRING);
_mySqlConnection1.Open();
_dataSet1 = new DataSet();
_mySqlDataAdapter1 = new MySqlDataAdapter();
_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.Inventory.GetItemNumber(barcode);
Or to load a purchase order:
if (Database.PO.POExists(poNumber))
{
Database.PO.Load(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_STRING = "the conn. string...";
private static MySqlConnection _mySqlConnection1;
private static MySqlDataAdapter _mySqlDataAdapter1;
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 _mySqlConnection1; }
}
public static MySqlDataAdapter DataAdapter
{
get { return _mySqlDataAdapter1; }
}
public static DataSet DataSet
{
get { return _dataSet1; }
}
public static PurchaseOrder PO
{
get { return _purchaseOrder; }
}
public static Inventory Inventory
{
get { return _inventory; }
}
static Database()
{
_mySqlConnection1 = new MySqlConnection(CONNECTION_STRING);
_mySqlConnection1.Open();
_dataSet1 = new DataSet();
_mySqlDataAdapter1 = new MySqlDataAdapter();
_purchaseOrder = new PurchaseOrder();
_inventory = new Inventory();
}
}
Thanks in advance,
Laban