Getting a class instance from within the applied attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wouter de Kort

    Getting a class instance from within the applied attribute

    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

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Getting a class instance from within the applied attribute

    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 IContributeEnvo ySink or IContributeObje ctSink. They both take
    references to the MarshalByRefObj ect which is the ContextBoundObj ect 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 IContributeObje ctSink will contribute a sink which is called in the
    server context, while the IContributeEnvo ySink 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]
    - mvp@spam.guard. caspershouse.co m

    "Wouter de Kort" <Wouter de Kort@discussion s.microsoft.com> wrote in message
    news:1A0CBFB8-2CE9-4CCD-8FA8-F889E0EA3071@mi crosoft.com...[color=blue]
    > 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
    >[/color]


    Comment

    • Wouter de Kort

      #3
      Re: Getting a class instance from within the applied attribute

      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 [.NET/C# MVP]" wrote:
      [color=blue]
      > 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 IContributeEnvo ySink or IContributeObje ctSink. They both take
      > references to the MarshalByRefObj ect which is the ContextBoundObj ect 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 IContributeObje ctSink will contribute a sink which is called in the
      > server context, while the IContributeEnvo ySink 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]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Wouter de Kort" <Wouter de Kort@discussion s.microsoft.com> wrote in message
      > news:1A0CBFB8-2CE9-4CCD-8FA8-F889E0EA3071@mi crosoft.com...[color=green]
      > > 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
      > >[/color]
      >
      >
      >[/color]

      Comment

      Working...