• NA
  • 3
  • 0

instantiate a Data Access Layer once and have it exposed to all Classes

Feb 11 2010 8:29 AM

new to C#
using VSpro
using Windows Forms
Built Classes for BusinessLogic, DataAccess,Customers, Suppliers, Parts etc.

What I need to do :
1) separate the Data Access(Da) Layer and Business Logic(Bl) Layer from the UI
2) Have only 1 global instantiation of the BL object and the DA object

Wjat I did:
On the Business Logic Side I have done so with the Following:
On MainForm I have the the following that will give me one entry point to the Business Logic (BL)Object.

Instantiate the BusinessLogic (Bl) Class:

public partial class MainForm : Form

public static BusinessLogic Bl = new BusinessLogic();

public MainForm()

I can address any Method in the BL Object using something like MainForm.Bl.SelectACustRecord(CustRec);.
This works fine for all of my forms.

I cannot think of a way to Instantiate the DataAccess Class once so I can address this class in all
of my other classes. "Customer.cs","Parts.cs" etc unless I instantiate the DA Object in each Class as in :

class Customer : Business
public DataAccess Da = new DataAccess();
double _creditLimit;

public Customer()


1)In the BL class I instantiate the Da class - no go
2)Instantiate the Da in each Class this allows me to address DA but
When I fire "public DataAccess Da = new DataAccess(); " the DataAccess Constructor fires
and the SQL Connection opens.
as in :

public class DataAccess
private string connectionString;
private SqlConnection connection;

public DataAccess()

connectionString = "Data Source=Server;Initial Catalog=Server.MyDB_DB.dbo;Integrated Security=True";
connection = new SqlConnection(connectionString);

Hope this makes sense.
Any suggestion would be greatly appreciate

Thanks in advance


Answers (4)