473,748 Members | 6,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OOP Aggregation

Let's say you're provided with an instance of a class. The instantiation
takes place in another module that you have no control over.

However, you've extended that class with your own value-added functionality.

In C#, given such an instance, and a derived class, there's no way to
'attach' the instance to the class -- you have to either change the way the
class was instantiated (not possible in this case), or wrap the class and
delegate each function (a concept known as aggregation).

I'm sure I've asked this question on the group before, and I settled for
aggregation. But I just don't like it.

I was wondering if anyone had any thoughts on this... ideas of more elegant
work-arounds / design patterns... solutions in other languages etc.

Thanks.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
Nov 16 '05 #1
5 3780
Let's assume for a moment we were real IL-assembler cracks and could bend
the CLR in any way we wanted: We could (in theory) reallocate the instance
of the old class we're given to make room for the additional members in the
new class and modify the virtual function table pointer, and the interface
pointers and metadata pointers to point to the new classes ones. If I got
you right, that's roughly what you had in mind?
First of all: This is (almost) possible. You could simply allocate an
instance of the new class, and copy each member (using reflection) from the
old class into the new one. Modify all pointers to the old instance to point
to the new one (probably the ugliest part) and you're done.
There are major drawbacks in this approach, namely that it's hard to
identify all pointers to the old instance (think of locks or event
subscriptions) and that you don't really know the internal state of the
"old" object when you "apply" your new class.
For your second question: COM had a concept called "aggregatio n" in which a
COM object could expose some other object's interfaces transparently as if
they were it's own ones, btu I don't know how to recreate that in the .net
environment.
I think I read somewhere that Smalltalk supported something like you
suggested, but that's all i know about it - maybe you can get some ideas
from looking at that language.
IMO the idea behind a OOP language *is* to restrict what is possible on
objects/classes/interfaces; Otherwise we could all build C structures with
function pointers - you could do *anything* with those...

That said, the decorator pattern (which you described) is probably by far
the most common and reliable solution to this problem.

Niki

"John Wood" <j@ro.com> wrote in
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Let's say you're provided with an instance of a class. The instantiation
takes place in another module that you have no control over.

However, you've extended that class with your own value-added functionality.
In C#, given such an instance, and a derived class, there's no way to
'attach' the instance to the class -- you have to either change the way the class was instantiated (not possible in this case), or wrap the class and
delegate each function (a concept known as aggregation).

I'm sure I've asked this question on the group before, and I settled for
aggregation. But I just don't like it.

I was wondering if anyone had any thoughts on this... ideas of more elegant work-arounds / design patterns... solutions in other languages etc.

Thanks.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

Nov 16 '05 #2
Thanks for your thoughts.

A long time ago I implemented something very hacky in COM where I would
synthetically add support for new interfaces into an existing instance (that
didn't support aggregation). Sounds similar to your suggestion re
reflection... I think I've matured enough not to try anything like that
again! :)

It's true, the decorator pattern is the most common (and probably best)
solution available today -- but it does seem like a common and legitimate
problem that would warrant native support in a popular language such as C#.

I really don't like the idea of manually delegating each method, because (a)
you lose the ability to cast to base classes of the original implementation
and (b) you have to maintain those delegates and keep the, in sync.

In my mind, it's one of the biggest stumbling blocks of an extensible object
oriented system. You create classes, you want to allow other vendors to
'plug-in' their extensions in the form of derived classes so they can
override various bits of functionality, but the 'plugging in' doesn't occur
until after instantiation when it's too late.

But there just isn't a way to do this neatly in C# (or C++ for that matter).
I think we really need native support for the decorator pattern in C#.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"Niki Estner" <ni*********@cu be.net> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Let's assume for a moment we were real IL-assembler cracks and could bend
the CLR in any way we wanted: We could (in theory) reallocate the instance
of the old class we're given to make room for the additional members in the new class and modify the virtual function table pointer, and the interface
pointers and metadata pointers to point to the new classes ones. If I got
you right, that's roughly what you had in mind?
First of all: This is (almost) possible. You could simply allocate an
instance of the new class, and copy each member (using reflection) from the old class into the new one. Modify all pointers to the old instance to point to the new one (probably the ugliest part) and you're done.
There are major drawbacks in this approach, namely that it's hard to
identify all pointers to the old instance (think of locks or event
subscriptions) and that you don't really know the internal state of the
"old" object when you "apply" your new class.
For your second question: COM had a concept called "aggregatio n" in which a COM object could expose some other object's interfaces transparently as if
they were it's own ones, btu I don't know how to recreate that in the .net
environment.
I think I read somewhere that Smalltalk supported something like you
suggested, but that's all i know about it - maybe you can get some ideas
from looking at that language.
IMO the idea behind a OOP language *is* to restrict what is possible on
objects/classes/interfaces; Otherwise we could all build C structures with
function pointers - you could do *anything* with those...

That said, the decorator pattern (which you described) is probably by far
the most common and reliable solution to this problem.

Niki

"John Wood" <j@ro.com> wrote in
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Let's say you're provided with an instance of a class. The instantiation
takes place in another module that you have no control over.

However, you've extended that class with your own value-added

functionality.

In C#, given such an instance, and a derived class, there's no way to
'attach' the instance to the class -- you have to either change the way

the
class was instantiated (not possible in this case), or wrap the class and delegate each function (a concept known as aggregation).

I'm sure I've asked this question on the group before, and I settled for
aggregation. But I just don't like it.

I was wondering if anyone had any thoughts on this... ideas of more

elegant
work-arounds / design patterns... solutions in other languages etc.

Thanks.

--
John Wood
EMail: first name, dot, last name, at priorganize.com


Nov 16 '05 #3
They probably thought that many common applications of the decorator pattern
could be easily solved using events. (Add functionality at runtime by
overriding some event) Also, delegates are a lot cleaner and simpler than
good ol' function pointers, and if your "mother objects" contained
replaceable delegates instead of virtual functions, the kind of
"runtime-inheritance" you had in mind would be easily possible. If the
object's clients only access the object by a well-defined set of interfaces,
you could only replace one of those interfaces, leaving the others
unchanged, and introduce new behaviour this way.
Of course, all these solutions (which are the typical C# solutions) require
provisions on the parent class side, and some special design on the client
side.
Thanks to IL it's quite easy nowadays to create code at runtime, so creating
some proxy class on-the-fly from metadata would be entirely possible, too.

So, in the end I still think you have plenty of options to choose from; And
I do remember good old windows-message-handlers that did all kinds of "dirty
tricks" to re-route messages at runtime. That is, I do remember all those
debugging sessions...

Niki

"John Wood" <j@ro.com> wrote in
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Thanks for your thoughts.

A long time ago I implemented something very hacky in COM where I would
synthetically add support for new interfaces into an existing instance (that didn't support aggregation). Sounds similar to your suggestion re
reflection... I think I've matured enough not to try anything like that
again! :)

It's true, the decorator pattern is the most common (and probably best)
solution available today -- but it does seem like a common and legitimate
problem that would warrant native support in a popular language such as C#.
I really don't like the idea of manually delegating each method, because (a) you lose the ability to cast to base classes of the original implementation and (b) you have to maintain those delegates and keep the, in sync.

In my mind, it's one of the biggest stumbling blocks of an extensible object oriented system. You create classes, you want to allow other vendors to
'plug-in' their extensions in the form of derived classes so they can
override various bits of functionality, but the 'plugging in' doesn't occur until after instantiation when it's too late.

But there just isn't a way to do this neatly in C# (or C++ for that matter). I think we really need native support for the decorator pattern in C#.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"Niki Estner" <ni*********@cu be.net> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Let's assume for a moment we were real IL-assembler cracks and could bend
the CLR in any way we wanted: We could (in theory) reallocate the instance of the old class we're given to make room for the additional members in the
new class and modify the virtual function table pointer, and the interface pointers and metadata pointers to point to the new classes ones. If I got you right, that's roughly what you had in mind?
First of all: This is (almost) possible. You could simply allocate an
instance of the new class, and copy each member (using reflection) from

the
old class into the new one. Modify all pointers to the old instance to

point
to the new one (probably the ugliest part) and you're done.
There are major drawbacks in this approach, namely that it's hard to
identify all pointers to the old instance (think of locks or event
subscriptions) and that you don't really know the internal state of the
"old" object when you "apply" your new class.
For your second question: COM had a concept called "aggregatio n" in which a
COM object could expose some other object's interfaces transparently as

if they were it's own ones, btu I don't know how to recreate that in the ..net environment.
I think I read somewhere that Smalltalk supported something like you
suggested, but that's all i know about it - maybe you can get some ideas
from looking at that language.
IMO the idea behind a OOP language *is* to restrict what is possible on
objects/classes/interfaces; Otherwise we could all build C structures with function pointers - you could do *anything* with those...

That said, the decorator pattern (which you described) is probably by far the most common and reliable solution to this problem.

Niki

"John Wood" <j@ro.com> wrote in
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Let's say you're provided with an instance of a class. The instantiation takes place in another module that you have no control over.

However, you've extended that class with your own value-added

functionality.

In C#, given such an instance, and a derived class, there's no way to
'attach' the instance to the class -- you have to either change the way
the
class was instantiated (not possible in this case), or wrap the class

and delegate each function (a concept known as aggregation).

I'm sure I've asked this question on the group before, and I settled

for aggregation. But I just don't like it.

I was wondering if anyone had any thoughts on this... ideas of more

elegant
work-arounds / design patterns... solutions in other languages etc.

Thanks.

--
John Wood
EMail: first name, dot, last name, at priorganize.com



Nov 16 '05 #4
Yeah events are an option, but that sounds awfully like another
manifestation of the decorator pattern. Although it does have the benefit of
multicasting, but then I suppose you have to worry about the order in which
the delegates are invoked and such.

I agree that creating the proxy at runtime is an option -- and probably what
..net languages like Smalltalk and Eiffel would do to emulate some of the OOP
features not directly supported in the CLR.

Also, another idea I had, would be to use the contextboundobj ect features of
the CLR, intercepting method calls and giving 'plugins' a chance to handle
the method. The only problem is that the whole remoting infrastructure adds
significant performance penalties (method calls become something like 100
times slower), and wouldn't work on MBV objects, so it's not really an
option in serious apps.

And the biggest problem I have with the decorator pattern is that you lose
all of the class hierarchy parenthood of the object that the proxy is
encapsulating, so it's hardly a transparent solution.

Maybe I'll investigate some of the runtime reflection/codedom features to
see what kind of runtime options I might have for doing this
programmaticall y.

Thanks.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"Niki Estner" <ni*********@cu be.net> wrote in message
news:eZ******** ******@TK2MSFTN GP09.phx.gbl...
They probably thought that many common applications of the decorator pattern could be easily solved using events. (Add functionality at runtime by
overriding some event) Also, delegates are a lot cleaner and simpler than
good ol' function pointers, and if your "mother objects" contained
replaceable delegates instead of virtual functions, the kind of
"runtime-inheritance" you had in mind would be easily possible. If the
object's clients only access the object by a well-defined set of interfaces, you could only replace one of those interfaces, leaving the others
unchanged, and introduce new behaviour this way.
Of course, all these solutions (which are the typical C# solutions) require provisions on the parent class side, and some special design on the client
side.
Thanks to IL it's quite easy nowadays to create code at runtime, so creating some proxy class on-the-fly from metadata would be entirely possible, too.

So, in the end I still think you have plenty of options to choose from; And I do remember good old windows-message-handlers that did all kinds of "dirty tricks" to re-route messages at runtime. That is, I do remember all those
debugging sessions...

Niki

"John Wood" <j@ro.com> wrote in
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Thanks for your thoughts.

A long time ago I implemented something very hacky in COM where I would
synthetically add support for new interfaces into an existing instance (that
didn't support aggregation). Sounds similar to your suggestion re
reflection... I think I've matured enough not to try anything like that
again! :)

It's true, the decorator pattern is the most common (and probably best)
solution available today -- but it does seem like a common and legitimate
problem that would warrant native support in a popular language such as

C#.

I really don't like the idea of manually delegating each method, because

(a)
you lose the ability to cast to base classes of the original

implementation
and (b) you have to maintain those delegates and keep the, in sync.

In my mind, it's one of the biggest stumbling blocks of an extensible

object
oriented system. You create classes, you want to allow other vendors to
'plug-in' their extensions in the form of derived classes so they can
override various bits of functionality, but the 'plugging in' doesn't

occur
until after instantiation when it's too late.

But there just isn't a way to do this neatly in C# (or C++ for that

matter).
I think we really need native support for the decorator pattern in C#.

--
John Wood
EMail: first name, dot, last name, at priorganize.com
"Niki Estner" <ni*********@cu be.net> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Let's assume for a moment we were real IL-assembler cracks and could bend the CLR in any way we wanted: We could (in theory) reallocate the instance of the old class we're given to make room for the additional members in the
new class and modify the virtual function table pointer, and the interface pointers and metadata pointers to point to the new classes ones. If I got you right, that's roughly what you had in mind?
First of all: This is (almost) possible. You could simply allocate an
instance of the new class, and copy each member (using reflection)
from
the
old class into the new one. Modify all pointers to the old instance to

point
to the new one (probably the ugliest part) and you're done.
There are major drawbacks in this approach, namely that it's hard to
identify all pointers to the old instance (think of locks or event
subscriptions) and that you don't really know the internal state of
the "old" object when you "apply" your new class.
For your second question: COM had a concept called "aggregatio n" in

which
a
COM object could expose some other object's interfaces transparently as if they were it's own ones, btu I don't know how to recreate that in the .net environment.
I think I read somewhere that Smalltalk supported something like you
suggested, but that's all i know about it - maybe you can get some
ideas from looking at that language.
IMO the idea behind a OOP language *is* to restrict what is possible on objects/classes/interfaces; Otherwise we could all build C structures

with function pointers - you could do *anything* with those...

That said, the decorator pattern (which you described) is probably by far the most common and reliable solution to this problem.

Niki

"John Wood" <j@ro.com> wrote in
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
> Let's say you're provided with an instance of a class. The instantiation > takes place in another module that you have no control over.
>
> However, you've extended that class with your own value-added
functionality.
>
> In C#, given such an instance, and a derived class, there's no way to > 'attach' the instance to the class -- you have to either change the way the
> class was instantiated (not possible in this case), or wrap the
class and
> delegate each function (a concept known as aggregation).
>
> I'm sure I've asked this question on the group before, and I settled

for > aggregation. But I just don't like it.
>
> I was wondering if anyone had any thoughts on this... ideas of more
elegant
> work-arounds / design patterns... solutions in other languages etc.
>
> Thanks.
>
> --
> John Wood
> EMail: first name, dot, last name, at priorganize.com
>
>



Nov 16 '05 #5
Priceless words of wisdom there.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"Beeeeeeeeeeeev es" <Be************ **@discussions. microsoft.com> wrote in
message news:F8******** *************** ***********@mic rosoft.com...
The solution is normally to manhandle the module that you have "no control" over, such that you DO have control over it.

"John Wood" wrote:
Let's say you're provided with an instance of a class. The instantiation
takes place in another module that you have no control over.

However, you've extended that class with your own value-added functionality.
In C#, given such an instance, and a derived class, there's no way to
'attach' the instance to the class -- you have to either change the way the class was instantiated (not possible in this case), or wrap the class and delegate each function (a concept known as aggregation).

I'm sure I've asked this question on the group before, and I settled for
aggregation. But I just don't like it.

I was wondering if anyone had any thoughts on this... ideas of more elegant work-arounds / design patterns... solutions in other languages etc.

Thanks.

--
John Wood
EMail: first name, dot, last name, at priorganize.com

Nov 16 '05 #6

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

Similar topics

1
2916
by: Nice Chap | last post by:
Aggregation in COM was defined as 'Exposing an interface of an inner object by the outer object as though it were an interface of the outer object'. Is this type of aggregation possible in c#? For example... Class COuter - 'Contains' an instance variable of type CInner Class CInner - Implements IList Can COuter be made to look like its implementing IList?
4
10967
by: cmrchs | last post by:
Hi, how do I implement aggregation and how composition in C# ? When I say : an Airplane has a Pilot then I use aggregation but when I say : an Airplane has a Cockpit then I use composition. How do I implement the difference in C# ? Here's what I try : class Pilot
2
2626
by: Jozsef Bekes | last post by:
Hi, I would like to implement aggregation in C#, therefore I'd need to implement the queryinterface COM function of a class. I am not sure whether this can be done, and if yes where to start. If someone has an example, a hint or anything that might help, please share it with me. Thanks and Regards, Jozsi
4
14448
by: Frederik Vanderhaegen | last post by:
Hi, Can anyone explain me the difference between aggregation and composition? I know that they both are "whole-part" relationships and that composition parts are destroyed when the composition whole is destroyed. Under a "whole-part" relationship I understand the following: the whole can't exists without the parts, but can the parts exist without the hole? f.e.: a car can't exist without an engine private engine _Engine
23
2125
by: SenthilVel | last post by:
Hi Can any one let me know the websites/Pdf for learning Aggragation in C#?? Thanks Senthil
1
2032
by: ninjutsu28 | last post by:
hi im juz a student so pls bear with my terminologies..juz wana ask how to perform aggregation in vb.net code.. in my model, i have rectangle, shape, drawing classes..rectangle class inherits shape class (is - a relationship), and drawing class has shape class(aggregation) Shape Class: Public MustInherit Class CShape Rectangle Class: Public Class CRectangle
7
15196
by: Bruce One | last post by:
In C#, how would people implement a relationship between Customer class and Request class, considering a customer may have 0-n requests and a request must belong to 1 and only 1 customer...? Would about the following options? OPTION 1 -------------------------- public class Customer() { private int CustomerID;
0
1245
by: Karigar | last post by:
I have been so far developing COM servers and clients in C++. I am new to C#/NET way of doing COM and was wondering if it is possible to accomplish aggregation in .NET platform. By aggregation I mean classic COM aggregation in which the outer object controls which interfaces it of inner object it exposes, and to the outside world it appears as the inner object without having to implement all the interfaces. By aggregation I do not mean a...
6
7848
by: Jeff | last post by:
hey Can OO Aggregation be described as: - A system of objects that are built using each other any comments? Jeff
0
8987
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
8826
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
9366
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
9316
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
9241
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
8239
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...
1
6793
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
6073
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
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.