looking for a best practice regarding code reuse

A

apandapion

I'm working with csharp and .net for the first time, and I've had a
fair amount of luck. I started with the MSDN "Walkthrough : Creating a
Distributed Application" tutorial and expanded from there, adding state
management and other assorted things.

So I have a lot of code that looks like this:

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
/* snip lots of code that I've written */
}

It wraps a dataset obtained through a pretty standard two function
GetTable/UpdateTable web service.

I want to reuse this code, and not in the braindead
cut/paste/alter/suffer manner. Under the .net architecture, what's the
best way to go about it? Write my own class that inherits from
DataGrid? I tried that, and it encapsulates the code nicely, but it
also causes lots of errors from the aspx file that I'm not sure how to
handle. Do I have to write a web custom control? Do I lose access to
the designer in doing so?
 
M

Marina

I am not too familiar with writing custom web controls.

However, an easy way to reuse code is to create a method that accepts a grid
and other arguments, and does what is appropriate.

Then create handlers for your grids, and in the handlers just call your
method.

This does require you to write some code writing on your part, since you
will have the same event handler/forwarding to real method code. But that
will just be the same few lines - the real meat will be in that method you
wrote.
 
A

apandapion

Marina said:
However, an easy way to reuse code is to create a method that accepts a grid
and other arguments, and does what is appropriate.

Then create handlers for your grids, and in the handlers just call your
method.

This does require you to write some code writing on your part, since you
will have the same event handler/forwarding to real method code. But that
will just be the same few lines - the real meat will be in that method you
wrote.

A method to the webform object? That sounds sensible, and will let me
at least create my 3 instances of similar tables. Is there a way to do
it that doesn't restrict reuse to the current project?
 
R

Robbe Morris [C# MVP]

private void DataGrid1_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
/* don't put logic here. only lines to call your logic elsewhere */
}

You should not business logic in your controls. You should
not put business logic (no matter how small) in your event
handling sections or even form methods.

You'll be much better off enhancing your application if
your business rules are in classes that are not tied directly
to your UI or business layers outside of the parameters
passed in.


--
Robbe Morris - 2004/2005 Microsoft MVP C#

Earn money answering .NET Framework
messageboard posts at EggHeadCafe.com.
http://www.eggheadcafe.com/forums/merit.asp
 
A

apandapion

Morris, that's pretty much what I said. We agree on the theory. I'm
looking for a good way to reencapsulate the code, as in, "what language
construct should I use?" Inherit from DataGrid? Write a helper
object? Write my own macro preprocessor?

It's a little hard to encapsulate anything coming off of the XML soap
interface, as the objects are not objects but just a pair of get/put
function calls. And the ASPX side of my module seems to react very
strangely to me inheriting from DataGrid.

I had no trouble working out how to plug COM objects together, back in
the day, as life was just a matter of locating, implementing and
invoking interfaces. Dotnet is a lot easier to work with, so far, but
it's not abundantly clear how I'm supposed to both encapsulate my own
code and interact with the twisty maze of microsoft objects that I have
to talk to on all sides. I would like to think that dotnet is good for
more than toys, but I haven't seen real evidence of that so far.

Can you recommend some books, or technical articles, that demonstrate
the right way to do things? I don't want clouds with lines between
them... I want code. It would be nice to do things "the right way",
from the beginning, but right now I'm just a seeker after the way.
 
K

Kevin Spencer

Good questions all. It does sound like you want to create some custom
Controls, and there are several different types to choose from. I think you
will find the following section and related articles from the Microsoft .Net
SDK helpful:

http://msdn.microsoft.com/library/d...uide/html/cpcondevelopingwebformscontrols.asp

You don't have to lose the Designer when using them either. You can
implement your own custom Designers for any Controls you create. See:

http://msdn.microsoft.com/library/d...ide/html/cpconenhancingdesign-timesupport.asp

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Expect the unaccepted.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top