Windsor Castle: How to get proxies for a specific instance?

I use Castle Windsor in my project. Some registered components are intercepted. Since components are registered through interfaces, Castle Windsor creates front-end proxies (Castle Windsor creates a stand-alone type that implements the interface and delegates the actual implementation using composition). Unfortunately, you cannot execute methods in the actual implementation of the interface, since the proxy server will be bypassed.

Is there a way to get a proxy instance that represents a real implementation in a real implementation?

Here is an example of what I would like to achieve. I want to always intercept the Get method. Please do not resort to alternative ways to refactor this sample, because this is not my production code, but just something invented for demonstration.

public interface IProvider
{
    bool IsEmpty { get; }
    object Get();
}

public class ProxyBypassingProvider : IProvider
{
    public bool IsEmpty
    {
        // Calls method directly, not through the proxy.
        get { return Get() == null; }
    }

    public object Get()
    {
        return new Object();
    }
}

public class InterceptedProvider : IProvider
{
    private IProvider _this; // Should hold the proxy instance.

    public bool IsEmpty
    {
        // Calls method through proxy.
        get { return _this.Get() == null; }
    }

    public object Get()
    {
        return new Object();
    }
}

How to set _this field in proxy instance?

Best regards - Oliver Hanappi

PS: Here is an example of the real world.

public interface IPresentationModel
{
    IView View { get; }
}

public interface IView
{
    void SetModel(IPresentationModel model);
}

public PresentationModel : IPresentationModel
{
    public IView View { get; private set; }

    public PresentationModel(IView view)
    {
        View = view;
        View.SetModel(this);
    }
}

. . , IView.SetModel(), .
, IPresentationModel -, SetModel . , , .

, , , , .

var model = _container.Resolve<IPresentationModel>();
model.View.SetModel(model);

, .

+3
1

" " , -.

, . Tuna OnCreateFacility - , v2.0.

+3

Source: https://habr.com/ru/post/1717426/


All Articles