473,503 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

looking for a best practice regarding code reuse

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?

Nov 19 '05 #1
5 1581
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.

<ap********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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?

Nov 19 '05 #2

Marina wrote:
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?

Nov 19 '05 #3
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

<ap********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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?

Nov 19 '05 #4
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.

Nov 19 '05 #5
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/de...mscontrols.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/de...imesupport.asp

--
HTH,

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

<ap********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
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?

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
2602
by: Swartz | last post by:
Hi all. I'm working here on a small project of mine. I'm not new to programming, but I'm new to PHP. You have to understand that I'm coming from C++, OOP world, so my code might seems a little...
4
314
by: Chris | last post by:
I have a question on whether or not this is good practice. I have a fairly complex web user control (a datalist embedded in a datalist with lots of controls) that I will call Usercontrol1 and a...
136
9199
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
2
3638
by: Joe Bloggs | last post by:
I have a general question on best practice regarding data access. I have the code below, a static method defined in a class that I use in a data layer dll. The method takes a string as its...
0
1021
by: jg | last post by:
If I need to reuse regex pattern or let non dotnet com client to reuse the pattern to match against different strings, what is the best way? How should I free up the resources held by regex? ...
10
2961
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
6
1328
by: Ethan V | last post by:
I have a few options regarding populating the state combo box 1. On page load, get the 50 states from the database 2. On application start, get the 50 states from the database and cache them in...
11
2470
by: Benny | last post by:
I just wanted to throw the discussion out there on what the best practice people feel is for using large objects in a foreach loop. For example if you are reusing an Image object in a loop like...
52
3317
by: burgermeister01 | last post by:
First, let me say that this question is a rather general programming question, but the context is PHP, so I figured this group would have the most relevant insight. Anyways, this is also more of...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.