Observer pattern in dotNet 2.0

A

Allan Ebdrup

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
 
D

Daniel O'Connell [C# MVP]

Allan Ebdrup said:
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.,
 
A

Allan Ebdrup

Daniel O'Connell said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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/t...104-8782132-6150328?v=glance&s=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/issues/03/03/ContextsinNET/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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top