User Control Design

G

Guest

All,

I have a windows form application. The form will host various user controls
that will implement an interface so that was new controls can be added later
without recompiling the hosting program.

The various controls will be displaying data that is stored in a SQL database.

My question is what is the prefered way of having the controls access the
business layer that will in turn access the data layer? Does the control
access the business layer directly or does the control get access via the
hosting form? Any insight or reasons as to why one way is prefered would be
great.


Thanks for the help!

Dan
 
S

stork

Hi Dan,

Generally you will want to have a data access / business layer that is
distinct from the user interface. It makes the code cleaner and more
maintainable.

For example, consider the quintessential invoice entry application.
You have a form with a table that lists all of the items on the invoice
and some things to add items to the invoice.

With that in mind, you could have a business tier that looks something
like this:

namespace MyBusiness {

class Item {
private string partNumber;
public string PartNumber { get { return partNumber; } set { partNumber
= value; } }

public Item[] GetAllAvailableItems()
{
/* database stuff */
}

}

class Invoice {

private ArrayList items;

public void AddItem( Item _item );

public void Save()
{
/* do database stuff here */
}

public static Invoice Load( string _invoiceNumber )
{
/* do database stuff here */
}

public static Invoice CreateNew( )
{
/* do database stuff here */
}

public static string[] GetInvoiceNumberList()
{
/* do database stuff here */
}

}

This is a very small off the top of my head example, but clearly,
unencumbered by ui considerations, you can think about where your data
is going. In your form you could have a combo box that gets populated
by a call to GetInvoiceNumberList() to select invoices. Then you could
call create new Item objects and Add them to your invoice list.

The nice thing about this approach is that down the road you can move
your "middle tier" to be a physical middle tier, so all you'd have to
deploy is the client and it in turn would talk via .NET remoting to
your server. This would allow you manage your database connections
more readily, for example, and provide an additional layer of security.

Conversely, if you embedded all of your database statements into your
forms, you will find them getting very difficult to maintain. You will
find it more difficult to figure out bugs and such.
In any case, good luck!
 
E

Etienne

There are many ways you can get the user controls to access the business
layer. It's up to you to see what fits best your needs.

1. From the host form, you can set a reference to the business object to
each user control.

2. The host form can hold the business object and implement an interface to
return that business object. The user control can then get the host's
business object from the common interface.
ex: public interface IBusinessHost { public BusinessBase
tBusiness(); }

3. Each user control could initiate it's own Business object.


I have a few very complex forms that I divided into user controls. In the
host form's load event, I call a method called Bind on each user control. As
a parameter, I pass necessary references such as the business object.
Does the control access the business layer directly or does the control
get access via the hosting form?
Would it be usefull at all to get the user control communicate only with the
host form? If a user control have to work with data, it should communicate
with the object managing data; that is, the business object.

Etienne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top