473,803 Members | 4,192 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 "Walkthroug h : 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_Updat eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs 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 1601
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********@gma il.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.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 "Walkthroug h : 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_Updat eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs 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_Updat eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs 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********@gma il.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.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 "Walkthroug h : 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_Updat eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs 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********@gma il.com> wrote in message
news:11******** *************@g 47g2000cwa.goog legroups.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 "Walkthroug h : 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_Updat eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs 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
2669
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 too "object"-ified. Anyways, I've created a wrapper class for MySQL connectivity. It's barebones for now, no error checking or anything. So now I'm trying to create user/session-handling class that would work with the data stored in the database...
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 lot of code in the codebehind of the user control for setting/getting properties of the controls. What I would like to do is create a class like this: public class MyClass { UserControl1 _userControl
136
9461
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 code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
2
3658
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 parameter, connects to the database , executes the string (in this case SQL) in the form of a SqlDataReader and then returns the SqlDataReader. Here's the method... public static SqlDataReader SQLServerExecuteSQL(string SQLstr) {
0
1035
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? Right now, I am leaning towards this way Private Dim RegexCollection as New Microsoft.VisualBasic.Collection() Private Function makekey(ByVal strPrefx As String) As String return strPrefx + ToString(Microsoft.VisualBasic.DateAndTime.Timer) +
10
3005
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? public class MyClass private _variableName as integer public property VariableName as integer
6
1340
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 application cache. On page load, get the states from application cache 3. Create a class in App Code folder, declare a public string array property, hard code the 50 states. On page load, call this property to get the 50 states 4. Create a states...
11
2506
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 this (letters above snippets are for reference purposes): A foreach ( string s in myList ) { Image img = Image.FromFile( s ); // operate on img
52
3396
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 an opinion based question than one seeking a definite answer. Recently, while maintaining a rather large system. I needed to add an array class member to an object. It was exactly the same as another class member, except that one array stored...
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10316
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6842
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.