473,779 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OO debate: assuming type of containing object

I'm in a vigorous debate at my work regarding objects assuming knowledge of
the type their containing object.

This debate pertains specifically to ASP.NET, but I have decided to post in
the C# forum because this is where most of the OO gurus hang out, and I view
this as a fundamental issue of OO design.

In ASP.NET, objects of type WebForm and UserControl have an intrinsic Page
property which refers to their containing Page.

So, for an instance of UserControl1 "oUserContr ol1" located inside an
instance of WebForm1 "oWebForm1" , oUserControl1.P age = oWebForm1.

Nesting is also possible: for an instance of UserControl2 "oUserContr ol2"
located inside oUserControl1, oUserControl2.P age = oWebForm1.
By default, WebForms inherit from the base ASP.NET Page class :
System.Web.UI.P age.

One common pattern is to include common page functionality in a derived
class, and then derive all WebForms from it. For example, CorePage :
System.Web.UI.P age.

So if we define WebForm1 : CorePage , it is possible and convenient to write
following code inside UserControl1 :

((CorePage)Page ).CustomPropert y = "abcd";
((CorePage)Page ).CustomMethod( textBox1.Text );

But if you do write such code, instances of this UserControl can only be
located inside WebForms which derive from CorePage.

We can also write the above code inside UserControl2. But if we do, all
instances of UserControl2 must be ultimately contained in WebForms which
derive from CorePage.
The debate is as follows: I argue that there are other ways for
UserControls to trigger functionality provided by containing objects.
Events are one such mechanism. I do not think that portability of objects
should be compromised in favor of coding convenience.

Others argue that for our project there is no anticipated need to ever use
WebForms that do not derive from CorePage. Further they argue that making
such an assumption is non-agile ( "agile" being a buzzword of such import on
the current sw dev scene that those arguing against "agile" are branded as
witches and burned at the stake).

Nov 2 '07
11 1357
John A Grandy wrote:
The debate is as follows: I argue that there are other ways for
UserControls to trigger functionality provided by containing objects.
Events are one such mechanism. I do not think that portability of objects
should be compromised in favor of coding convenience.

Others argue that for our project there is no anticipated need to ever use
WebForms that do not derive from CorePage. Further they argue that making
such an assumption is non-agile ( "agile" being a buzzword of such import on
the current sw dev scene that those arguing against "agile" are branded as
witches and burned at the stake).
How the communication between controls is done, isn't only about
reusability. By keeping the communication channels as limited as
possible, they can be kept well defined and easily manageable.

For example, when a control wants access to a method in a common base
class for the page, it's not a bad idea to use an interface, for several
reasons:

:: You get a reference to an interface, with which you can only do what
was indended.

- The intellisense won't show you half a million other properties,
methods and events from the Page class that you don't care about.

- You can't use a method from the Page class instead of a method from
the base class by mistake, as they are simply not available.

:: It's flexible, as the interface can either be implemented by a base
class or by the page itself, and the control doesn't have to know which
it is. If one of the pages needs to implement
Creating pages and controls that depend on each other doesn't only
prevent reusability, it also makes the code harder to follow. You have
to be aware of all the code in the page and all the code in all
controls, to be able to tell how the code is working. Welcome back,
spaghetti code...

If a control instead uses interfaces and events, you can tell how the
code in the control is working without knowing anything about the code
in the page and the code in other controls on the page. This is how OO
programming should be.
--
Göran Andersson
_____
http://www.guffa.com
Nov 3 '07 #11
Göran Andersson wrote:
John A Grandy wrote:
>The debate is as follows: I argue that there are other ways for
UserControls to trigger functionality provided by containing objects.
Events are one such mechanism. I do not think that portability of
objects should be compromised in favor of coding convenience.

Others argue that for our project there is no anticipated need to ever
use WebForms that do not derive from CorePage. Further they argue
that making such an assumption is non-agile ( "agile" being a buzzword
of such import on the current sw dev scene that those arguing against
"agile" are branded as witches and burned at the stake).

How the communication between controls is done, isn't only about
reusability. By keeping the communication channels as limited as
possible, they can be kept well defined and easily manageable.

For example, when a control wants access to a method in a common base
class for the page, it's not a bad idea to use an interface, for several
reasons:

:: You get a reference to an interface, with which you can only do what
was indended.

- The intellisense won't show you half a million other properties,
methods and events from the Page class that you don't care about.

- You can't use a method from the Page class instead of a method from
the base class by mistake, as they are simply not available.

:: It's flexible, as the interface can either be implemented by a base
class or by the page itself, and the control doesn't have to know which
it is. If one of the pages needs to implement
Lost it in the middle of a thought there... ;)

If one of the pages needs to implement the methods differently, that can
be done without changing the base class to handle it.
>

Creating pages and controls that depend on each other doesn't only
prevent reusability, it also makes the code harder to follow. You have
to be aware of all the code in the page and all the code in all
controls, to be able to tell how the code is working. Welcome back,
spaghetti code...

If a control instead uses interfaces and events, you can tell how the
code in the control is working without knowing anything about the code
in the page and the code in other controls on the page. This is how OO
programming should be.


--
Göran Andersson
_____
http://www.guffa.com
Nov 3 '07 #12

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

Similar topics

54
2905
by: Stefan Arentz | last post by:
I was just reading through some old articles in the 'Why not develop new language' thread and came across the finally debate. Everytime I mention 'finally' to C++ programmers I get almost emotional responses about why it is not needed in C++. I don't get that. For example, consider the following code. Please note, I can only use heap allocated objects in my current project (new/delete). //
1
2237
by: KK | last post by:
Two quick questions: 1.) Given only a string containing fully qualified type name (e.g. "System.Drawing.Rectangle"), is there a consistent way to get the Type object that the string corresponds to that will work with most types? 2.) If no to question 1, then given the string containing the fully qualified type name, is there a way to get the fully qualified assembly containing that type?
3
2115
by: KRC | last post by:
I am using an external object called WebZinc to help parse web pages in VB.net. I am trying to use a particular method following the example in the help reference but am unable to get VB.net to recognize it. The code example is: Dim containsWordA As Boolean = New TextProcessor("my string containing words").ContainsWord("containing") where TextProcessor is the object and ContainsWord is the method. VB.net underlines the TextProcessor...
16
1915
by: Dennis | last post by:
I have a class named "myclass" and an arraylist containing elements of type "MyClass". I want to get the value of a property of "MyClass" (a string type) for one of the arraylist elements. I can get this using: dim b as string b = DirectCast(myarraylist(0),myclass).myproperty However, I want to use an object to define the type "MyClass" like: dim C as type = type.GetType(myarraylist(0))
2
1862
by: Don | last post by:
Is there anyway to convert an object from one type to another using either a System.Type object containing the desired type to convert to or a string variable containing the name of the type to convert to? - Don
77
4759
by: berns | last post by:
Hi All, A coworker and I have been debating the 'correct' expectation of evaluation for the phrase a = b = c. Two different versions of GCC ended up compiling this as b = c; a = b and the other ended up compiling it as a = c; b = c. (Both with no optimizations enabled). How did we notice you may ask? Well, in our case 'b' was a memory mapped register that only has a select number of writable bits. He claims it has been a 'C...
4
1840
by: andychambers2002 | last post by:
I'm working on a "TempFile" class that stores the data in memory until it gets larger than a specified threshold (as per PEP 42). Whilst trying to implement it, I've come across some strange behaviour. Can anyone explain this? The test case at the bottom starts a TempFile at size 50 and prints its type. It then increases the size to the threshold at which point "self" is changed to being a TemporaryFile. It seems that the next call...
10
3608
by: J055 | last post by:
Hi I have an OrderedDictionary object where the key is an enum. Is there an easy way to cast it to an integer? Examples/document appreciated. Thanks Andrew
24
2785
by: Frederick Gotham | last post by:
There is a thread currently active on this newsgroup entitled: "how to calculate the difference between 2 addresses ?" The thread deals with calculating the distance, in bytes, between two memory addresses. Obviously, this can only be done if the addresses refer to elements or members of the same object (or base objects, etc.). John Carson and I proposed two separate methods.
0
9633
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
9474
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
10305
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10074
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
9928
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...
0
8959
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6724
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();...
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2867
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.