Hi
Background:
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()
{
InitializeComponent();
}
I can address any Method in the BL Object using something like MainForm.Bl.SelectACustRecord(CustRec);.
This works fine for all of my forms.
PROBLEM:
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()
{
}
WHAT I HAVE DONE TO DATE:
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
Allan
Loading
Sam HobbsPosted Feb 12, 2010, 3:43 PM
AllanPosted Feb 13, 2010, 8:16 AM
Kirtan PatelPosted Feb 12, 2010, 3:59 PM
make Methods Static ...so you dont need to instantiate Class Every time you use ..and by making static methods you can call it directly
from any Form of Application ..
Example
-------------
We Have a Form and Business Logic Layer will be Like below
class BusinessLogic
{
public static void AddDataToDatabase(string data)
{
}
public static void DeleteDataFromDatabase(string id)
{
}
public static void UpdateData(string id)
{
}
}
Now Call Business Logic from anywhere as methods can be called without instantiating Class
like
BusinessLogic.UpdateData("test");
Sam HobbsPosted Feb 12, 2010, 12:52 PM
In the Program.cs for your project, there is a static class called Program. You can put static members there, and they will be available anywhere in the program. Do you understand how static members work? This will only work of course for data that is the same during the entire execution of your application. You can open the database and connect to it and such in code in the Program class in the Program.cs file.
Does that help?