views:

15

answers:

1

Hi All

I use Ninject for DI in Silverlight app. Now I am trying to implement interception and having issue. My methhod is not getting intercepted. Below is sample implementation

public class InfrastructureModule : NinjectModule
{
    public override void Load()
    {
        Bind<IGlobalEventManager>().To<GlobalEventManager>().InSingletonScope();
        Bind<IViewModel>().To<ViewModel>().InSingletonScope();
    }
}

In App.xaml --> Application_Startup event, i do the binding and call method i expect to be intercepted:

private void Application_Startup(object sender, StartupEventArgs e)
{
    IKernel kernel = new StandardKernel(new InfrastructureModule());

    IViewModel vm = kernel.Get<IViewModel>();
    vm.SomeMethod();

    this.RootVisual = new MainPage();
}

Implementation of ViewModel:

public class ViewModel : IViewModel
{
    [LockUI]
    public virtual void SomeMethod()
    {
        MessageBox.Show("Hello");
    }
}

LockUI attribute:

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method, AllowMultiple = false,
    Inherited = true)]
public class LockUIAttribute : InterceptAttribute
{
    public override IInterceptor CreateInterceptor(IProxyRequest request)
    {
        return request.Context.Kernel.Get<LockUIInterceptor>();
    }
}

LockUIInterceptor implementation:

public class LockUIInterceptor : SimpleInterceptor
{
    private IGlobalEventManager _globalEventManager;

    public LockUIInterceptor(IGlobalEventManager globalEventManager)
    {
        _globalEventManager = globalEventManager;
    }

    protected override void BeforeInvoke(IInvocation invocation)
    {
        _globalEventManager.OnLockUI(this, EventArgs.Empty);
    }
}

GlobalEventManager implementation:

public class GlobalEventManager : IGlobalEventManager
{
    private static int _asyncCallCount;

    public event EventHandler UnLockUI;

    public event EventHandler LockUI;

    public void OnLockUI(object sender, EventArgs e)
    {
        if (LockUI != null)
        {
            _asyncCallCount += 1;
            LockUI(sender, e);
        }
    }

    public void OnUnlockUI(object sender, EventArgs e)
    {
        if (UnLockUI != null)
        {
            _asyncCallCount -= 1;

            if (_asyncCallCount == 0)
                UnLockUI(sender, e);
        }
    }
}

I have added reference to Castle.Core, Castle.DynamicProxy2, Ninject, Ninject.Extensions.Interception, Ninject.Extensions.Interception.DynamicProxy2

Could someone please help. Thanks