Policy injection question

Topics: Policy Injection Application Block
Nov 20, 2007 at 8:09 AM
Hi all,
I started using the policy injection application block. I see that we can use it with back end business objects. When we create the object by the PolicyInjection.Create<TObject>();

But , what about using it in the UI layer. I mean , If I want to use it with code into the controls events.
void Button_Click (object sender , EventArgs e)
{
CallMethod();
}

void CallMethod()
{
......
}

How Can use it in this case.

Thabks in advance...
Nov 20, 2007 at 1:34 PM
Edited Nov 20, 2007 at 1:34 PM
You will need to create an interface that declares the CallMethod method, have the UI implement the interface, get a wrapped reference to this for the new interface and invoke the method through this wrapped reference.

Fernando
Nov 21, 2007 at 7:35 AM
Could you please post a small example .

Regards...
Nov 22, 2007 at 11:01 AM
Here you are. This is not production code; use this for illustration purposes only.

namespace WindowsApplication15
{
public partial class Form1 : Form, MyFormInterface
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.DoSomething();

}

private void button2_Click(object sender, EventArgs e)
{
WrappedThis.DoSomething();
}

MyHandler
public void DoSomething()
{
MessageBox.Show("Doing something");
}

MyFormInterface WrappedThis
{
get { return PolicyInjection.Wrap<MyFormInterface>(this); }
}
}

public interface MyFormInterface
{
void DoSomething();
}

public class MyHandler : ICallHandler
{
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
MessageBox.Show("In the handler!");

IMethodReturn msg = getNext()(input, getNext);
return msg;
}
}

AttributeUsage(AttributeTargets.Method)
public class MyHandlerAttribute : HandlerAttribute
{
public MyHandlerAttribute()
{ }

public override ICallHandler CreateHandler()
{
return new MyHandler();
}
}
}