Featured post
structuremap - What's the simplest way to intercept a method call for added functionality? -
suppose have repository returns list of post
s. repository interface has getall()
method suggests.
now in keeping theory shouldn't putting domain logic in repository, want intercept calls concrete getall()
method such can add following logic getall()
result:
return getall().orderbydescending(p => p.posted).tolist();
the reason want intercept because (1) don't want have client remember call extension method (orderbydescending
or useless wrapper of that), want called every time , (2) don't want have concrete implementations have remember order getall()
result - want logic in single place external repository.
what's easiest way this?
i'm using structuremap if can intercept might low cost option. don't think sm intercepts method calls, creation of object instance?
do need go proxy or mixin pattern? need go all-in castle dynamic proxy? or there another method should consider or perhaps combination?
i'm interested in concrete suggestion particular example above. i'm novice aop please gentle.
went dynamicproxy option. easier use thought.
all took using castle.dynamicproxy;
reference...
a bit of iinterceptor
...
public class postrepointerceptor : iinterceptor { public void intercept(iinvocation invocation) { invocation.proceed(); if (invocation.method.name.equals("getall", stringcomparison.invariantcultureignorecase)) invocation.returnvalue = this.getmodifiedgetallresult(invocation.returnvalue); } private object getmodifiedgetallresult(object getallresult) { return post.getorderedposts((ilist<post>)getallresult); } }
two new lines in structuremap config:
public reporegistry() { var pg = new proxygenerator(); for<ipostrepository>() .enrichallwith(z => pg.createinterfaceproxywithtarget<ipostrepository>(z, new postrepointerceptor())); }
..and it's done. getall()
behaves how want. can still use interfaces way i'm familar , i've kept dry , decoupled ddd.
- Get link
- X
- Other Apps
Comments
Post a Comment