Commerce Server

Commerce Server

Bindable Data Entity Objects with Designer support.

The ADO.NET DataSet is a great way to represent data in .NET, but I often find it more easy and efficient to use simple entity object and collections. Entity objects are often more efficient and easier to use while coding, and populating them with data is faster since I use DataReader instead of the much larger DataAdapter class.

Combining entity objects with designer support creates a powerful toolbox when building applications. Especially when working with ASP.NET Controls and data binding in Visual Studio.NET. 

If you use the Visual Studio.NET designer and property window, binding data to controls should be easy as drag and drop. First you build an assembly containing the entity objects, and then add them to your Visual Studio Toolbox. You then just drag the entity object into your form, and bind them to your controls using data binding in the property window.


Building entity objects with designer support are very easy. All you have to do is to build a simple entity class to hold your business data, and then implement the IComponent interface and implement serialization.

 

using System;

using System.Runtime.Serialization;

using System.ComponentModel;

 

// Default constructor.

[System.ComponentModel.DesignerCategory("Code"),Serializable]

public class Customer : IComponent {

 

private int customerId;

private string customerName;

ISite site = null;

 

// Default constructor.

public Customer() {

}

 

public int CustomerId {

get { return this.customerId; }

set { this.customerId = value;}

}

public int CustomerName {

get { return this.customerName; }

set { this.customerName = value;}

}

// Disposed EventHandler.

public event System.EventHandler Disposed;

// Site provides support for binding a Component to a Container and

// enable communication between them, as well as

// provide a way for the container to manage its components.

public ISite Site {

get { return site;}

set { site = value;}

}

// Implementation of EntityBase Dispose functionality

public void Dispose() {

if (Disposed != null)

Disposed(this,EventArgs.Empty);

}

}

When your entity class is ready you often also need a collection class to store collections of entities. To build a custom CustomerCollection, simply inherit from CollectionBase and implement IComponent.

 

[System.ComponentModel.DesignerCategory("Code"),Serializable]

public class CustomerCollection : CollectionBase, IComponent {

 

// Default constructor

public CustomerCollection() {

}

 

// Add new entity to collection

public int Add(Customer entity) {

return base.InnerList.Add(entity);

}

 

// Remove entity from collection

public void Remove(Customer entity) {

base.InnerList.Remove(entity);

}

 

// Collection indexer.

public Customer this[int index] {

get {return base.InnerList[index] as Customer;}

set { base.InnerList[index] = value; }

}

 

 

public event System.EventHandler Disposed;

 

private ISite site;

public ISite Site {

get { return site; }

set { site = value;}

}

 

public void Dispose() {

if (Disposed != null)

Disposed(this,EventArgs.Empty);

}

}

 

Compile your project and add the entity objects to the toolbox.

Hope this article is usefull for som of you out there..