473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Observer pattern in dotNet 2.0

Is it possible to listen to when a constructor is called or when a property
is set on a object, without having to implement changes in the observed
objects class, in dotNet 2.0? It would be very effective for implementing a
Model View Control architecture.
Perhaps some stuff could be generated runtime using reflection?

Kind Regards,
Allan Ebdrup
Nov 17 '05 #1
3 1576

"Allan Ebdrup" <co****@ofir.com> wrote in message
news:eX**************@TK2MSFTNGP14.phx.gbl...
Is it possible to listen to when a constructor is called or when a
property is set on a object, without having to implement changes in the
observed objects class, in dotNet 2.0? It would be very effective for
implementing a Model View Control architecture.
Perhaps some stuff could be generated runtime using reflection?


Perhaps some could be. In cases where the method you wanted to listen to was
virtual and you could control its creation, yes, you could generate
subclasses and return them at creation time, but that means you have to use
factories for everything. You might also be able to do something with the
proxy support in the remoting subsystem, but I'm not awake enough right now
to really dig through that. Even if you can you will probably be stuck
deriving your objects from MarshalByRefObject, which can have some
performance constraints.

Unfortunatly, there is no generalized way that I am aware of. You'd be
better off simply designing your objects to work within an MVC system to
start with.,
Nov 17 '05 #2
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:On**************@TK2MSFTNGP09.phx.gbl...
Perhaps some could be. In cases where the method you wanted to listen to
was virtual and you could control its creation, yes, you could generate
subclasses and return them at creation time, but that means you have to
use factories for everything. You might also be able to do something with
the proxy support in the remoting subsystem, but I'm not awake enough
right now to really dig through that. Even if you can you will probably be
stuck deriving your objects from MarshalByRefObject, which can have some
performance constraints.


I've taken a look at System.Runtime.Remoting.Proxies.RealProxy to see if
that could be used, but I'm having a hard time finding some good examples
online, does anyone have some good links to artichels/tutorials about using
proxies?

Kind Regards,
Allan Ebdrup
Nov 17 '05 #3
Allan,

The only way you are going to be able to do this is through
ContextBoundObjects. It's a little-known feature of .NET which allows you
to intercept method calls and perform pre and post processing.

The feature itself is not documented, but I believe MS says that they
will not change it, or remove support for it either.

There is a detailed explaination of it (as a matter of fact, a whole
chapter) in Juval Lowy's book, "Programming .NET Components", 2nd edition.
You can find the book at Amazon (watch for line wrap):

http://www.amazon.com/exec/obidos/tg...books&n=507846

He also wrote an artile on it for MSDN magazine. You can find that here
(watch for line wrap):

http://msdn.microsoft.com/msdnmag/is...T/default.aspx

These should help. I think you can intercept property get/sets, but
they are channeled to the sink as a call to the get_<property>,
set_<property> methods.

Also, you can intercept construction calls as well with this
architecture.

The biggest caveat here is that you are remoting, in a sense, so there
is going to be some overhead while going through the sink chain.

If you are thinking of doing something like parameter validation, you
have to seriously weigh this option out, because the performance hit you are
going to get compared to calling a function or performing a check in the
beginning of your method is going to be significant.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Allan Ebdrup" <co****@ofir.com> wrote in message
news:eX**************@TK2MSFTNGP14.phx.gbl...
Is it possible to listen to when a constructor is called or when a
property is set on a object, without having to implement changes in the
observed objects class, in dotNet 2.0? It would be very effective for
implementing a Model View Control architecture.
Perhaps some stuff could be generated runtime using reflection?

Kind Regards,
Allan Ebdrup

Nov 17 '05 #4

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

Similar topics

0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
4
by: decrypted | last post by:
Since I couldn't find a OO design/architext forum, I thought I would post here... I have a win app with forms management. forms are grouped by category (pertains to company, pertains to project....
0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
5
by: Alan Isaac | last post by:
I have two questions about the "observer pattern" in Python. This is question #1. (I'll put the other is a separate post.) Here is a standard example of the observer pattern in Python:
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.