Well, I got this with this...
First I create a class proxy, passing my instance into the
interceptor:
public T Instance(T instance){
return proxyGenerator.CreateClassProxy<T>(new
RoundingInterceptor<T>(instance,defaultContext));
}

So I can make intercepted calls on the type.

My interceptor then just sniffs attributes on the method declarations
for rounding rules:
public void Intercept(IInvocation invocation)
                {
                        invocation.Proceed();

                        MethodInfo methodInfo = 
invocation.MethodInvocationTarget;
                        RoundAttribute att =
GetAttributeFrom<RoundAttribute>(instance,methodInfo);

                        if(att==null || 
!Array.Exists(att.Contexts,s=>s.Equals(context)))
                        {
                                return;
                        }
                        object val = methodInfo.Invoke(instance, 
invocation.Arguments);
                        invocation.ReturnValue= 
Math.Round(Convert.ToDouble(val),
att.Precision, MidpointRounding.AwayFromZero);
                }

My very primitive tests work with this so ...not sure how far I can go
with this though.

On Oct 8, 4:23 pm, "Ayende Rahien" <[EMAIL PROTECTED]> wrote:
> This is supported by the DynamicProxy, not NH, would probably require you to
> build your proxy factory, mind you.
> On Thu, Oct 9, 2008 at 1:20 AM, Mike Nichols <[EMAIL PROTECTED]>wrote:
>
>
>
> > Hm...thanks Ayende
> > I had considered that but is there a way to redirect the target of an
> > invocation to the decorated instance?
> > I found something like this here
> >http://hendryluk.wordpress.com/2008/05/28/roll_your_own_cop_part_i_mi...
> > but I felt like maybe I was thinking too heavy for this. I saw on an
> > NHibernate support request an interface you called
> > IProxyCanChangeTarget  but can't find it.
>
> > On Oct 8, 1:56 pm, "Ayende Rahien" <[EMAIL PROTECTED]> wrote:
> > > Create a proxy that would decorate an existing instance?
>
> > > On Wed, Oct 8, 2008 at 10:51 PM, Mike Nichols <[EMAIL PROTECTED]
> > >wrote:
>
> > > > I have a slough of domain entities (without interfaces) that largely
> > > > encapsulate various algorithms for calculation, returning a double
> > > > which needs to be rounded (or not).
> > > > I am using NHibernate so public methods/properties are marked virtual.
>
> > > > I now have a requirement where rounding is applied to many of the
> > > > methods according to a specific context. For example, when using the
> > > > entity for one agency, we'd apply specific rules or perhaps for a
> > > > certain report round according to a different set of rules.
>
> > > > The methods themselves are very focused in responsibility so the
> > > > entities are poised to do this fairly easily, and I think I can
> > > > leverage DynamicProxy to do something like:
>
> > > > double value = new Rounded().Instance(myInstance).MyCalculation();
>
> > > > Where 'Rounded' would generate a proxy and apply the appropriate
> > > > interceptor for rounding rules and return the proxy.
>
> > > > The only wall I have come up against is the inability to create
> > > > proxies of existing class instances. Like I mentioned I don't have
> > > > interfaces defined for the entities and before I go that route I
> > > > wanted to see if anyone has dealt with a similar issue for
> > > > contextualizing domain entities.
>
> > > > Any ideas on approaches that might engage castle voodoo?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to castle-project-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to