Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
C# replacement for VFP code
Message
De
06/11/2006 00:40:56
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
01167122
Message ID:
01167242
Vues:
20
Jojo,

I'm not even sure where to start, but maybe I should start out by saying that you need to learn a little more about architecture.

To simplify, think of three different projects/dlls in your solution. MyUI, MyBiz, and MyDataAccess (I'd actually throw in a 4th one, MyWebService, but let's not complicate matters at this point. Oh wait, I'd have a separate DataSet project too, but as I said, let's keep it simple for now). In your MyUI project, you'd have all the different forms that you plan to use in your UI. The same for MyBiz and MyDataAccess projects, all the different Biz classes and DataAccess classes.

So, to start, your form would be similar to this (to get your data when the form first opens):
using MyCompany.Business.MyBiz;

namespace MyCompany.WinUI.MyUI
{
  public class MyForm : MyBaseForm
  {
    private long          CustomerKey;
    private MyDataSet     dsData;
    private MyBizCustomer oBiz;
    
    public MyForm(long key)
    {
      this.CustomerKey = key;
      InitializeComponent();
      this.FillData();
    }

    public void FillData()
    {
      // To simplify, I'm directly calling a Biz class.
      // In reality, I use a Web Service here instead
      // which in turn calls the Biz class.

      oBiz = new MyBizCustomer();
      dsData = oBiz.GetCustomer(this.CustomerKey);
    }
  }
}
Now in your MyBiz project, you'd have a Biz class:
using MyCompany.DataAccess.MyDataAccess

namespace MyCompany.Business.MyBiz
{
  public class MyBizCustomer
  {
    private MyDataSet dsData;

    public MyDataSet GetCustomer(long CustomerKey)
    {
      MyDataAccessCustomer oDA = new MyDataAccessCustomer();
      this.dsData = oDA.GetCustomer(CustomerKey);

      // if you have other Biz things to do to this customer
      // do it here before returning the DataSet

      return this.dsData;
    }
  }
}
And, lastly, in your MyDataAccess project, you'd have this class:
namespace MyCompany.DataAccess.MyDataAccess
{
  public class MyDataAccessCustomer
  {
    public MyDataSet GetCustomer(long CustomerKey)
    {
      // Here's where you'd put all the SqlCommand and DataAdapter stuff
      // and fill your DataSet.

      return dsData;
    }
  }
}
Now, a word or two about DataBinding. This is what makes using DataSets worthwhile. When you bind all your TextBox objects to a DataSet, when the user is done entering data and wants to save, you simply call Save method of your Business class (or, as I mentioned in the beginning, the Save method in your Web Service, which calls the Save method in your Business class), which calls the Save method in your DataAccess class which is the ONLY class that actually talks to the database. The DataSet is passed to these calls and the DataAccess class takes the changed data and makes the appropriate calls to the database.

DataBinding a TextBox is basically nothing more than issuing a simple statement:
MyTextBox.DataBinding.Add("Text", MyDataSet.Tables["MyTable"], "MyColumn");
Although, I've sub-classed all my base controls and included a DataBind method in each one. Here's a sample for my TextBox subclass:


m_BoundTable and m_BoundColumn are fields that are exposed as
public properties, so they can be set in the Property sheet if
you wish.
public virtual void DataBind(System.Data.DataTable Data, string Path)
{
	for (int i = 0; i < this.DataBindings.Count; i++)
	{
		if (this.DataBindings[i].Equals(this.oBinding))
		{
			this.DataBindings.Remove(this.oBinding);
			break;
		}
	}

	this.Text = "";
	this.m_BoundTable = Data;
	this.m_BoundColumn = Path;
	this.oBinding = new Binding(this.m_BoundProperty, Data, Path);
	this.oBinding.Format += new ConvertEventHandler(this.FormatHandler);
	this.oBinding.Parse += new ConvertEventHandler(this.ParseHandler);
	this.DataBindings.Add(this.oBinding);
}
Hopefully, this will give you a lot of "food for thought".

~~Bonnie
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform