Getting a class instance from within the applied attribute

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I've started looking at Aspect Oriented Programming but I've stumbled at a
problem I can't solve.

I want to add an Aspect that would notify a UnitOfWork that the current
DomainObject has changed so that it can be marked as Dirty.

I've created an Attribute, MessageSink and ContextProperty and everything is
working.

So when a property is accessed a function in the MessageSink class is
executed and the only thing the DomainObject has to do is add [SaveChanges]
to the class declaration.

But how can I pass the current DomainObject to my UnitOfWork? The funciton
in the MessageSink doesn't know about the DomainObject where the attributed
is used and I don't know how to find out on which DomainObject the property
is accessed.

Thnx for your help!

Wouter de Kort
 
Wouter,

First, it should be noted that using context bound objects is not aspect
oriented programming. It offers method interception (which aspect oriented
programming has), but that's about it. AOP offers much more than just that.

In order to know ^which^ object is being intercepted, you will have to
implement IContributeEnvoySink or IContributeObjectSink. They both take
references to the MarshalByRefObject which is the ContextBoundObject that is
specific to that object. This allows you to set up an instance of your sink
which is tied to just that object (not all objects of the type).

The IContributeObjectSink will contribute a sink which is called in the
server context, while the IContributeEnvoySink will intercept the call in
the client context.

Additionally, you should ask if you truly want to use context bound
objects in this situation. You are going to take a performance hit (much
more than say, placing a method in each property which will set a flag
internally indicating that a change has been made) because of what is needed
to take the values on the stack and construct them into a message.

Hope this helps.
 
Thnx for your advice :)

I know AOP is much more that intercepting method calls but this was my first
atempt at using it.

I'm trying to implement a system which would subscribe a class instance
automatically with a UnitOfWork and which would notify the UnitOfWork if the
class changed so updates can be written to the database.

It's just a test of the whole AOP idea.. if it doesn't work out I will just
go with the 'call method on each property'.

Do you have any other suggestions as how to implement the functionally I
want? Is there another, faster, way to intercept the method calls?

Thnx for your help,

Wouter de Kort

Nicholas Paldino said:
Wouter,

First, it should be noted that using context bound objects is not aspect
oriented programming. It offers method interception (which aspect oriented
programming has), but that's about it. AOP offers much more than just that.

In order to know ^which^ object is being intercepted, you will have to
implement IContributeEnvoySink or IContributeObjectSink. They both take
references to the MarshalByRefObject which is the ContextBoundObject that is
specific to that object. This allows you to set up an instance of your sink
which is tied to just that object (not all objects of the type).

The IContributeObjectSink will contribute a sink which is called in the
server context, while the IContributeEnvoySink will intercept the call in
the client context.

Additionally, you should ask if you truly want to use context bound
objects in this situation. You are going to take a performance hit (much
more than say, placing a method in each property which will set a flag
internally indicating that a change has been made) because of what is needed
to take the values on the stack and construct them into a message.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Wouter de Kort said:
I've started looking at Aspect Oriented Programming but I've stumbled at a
problem I can't solve.

I want to add an Aspect that would notify a UnitOfWork that the current
DomainObject has changed so that it can be marked as Dirty.

I've created an Attribute, MessageSink and ContextProperty and everything
is
working.

So when a property is accessed a function in the MessageSink class is
executed and the only thing the DomainObject has to do is add
[SaveChanges]
to the class declaration.

But how can I pass the current DomainObject to my UnitOfWork? The funciton
in the MessageSink doesn't know about the DomainObject where the
attributed
is used and I don't know how to find out on which DomainObject the
property
is accessed.

Thnx for your help!

Wouter de Kort
 

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

Back
Top