Search code examples
castle-windsor

What is the proper way to call proxied method inside concrete class?


For instance I have IRepository, Implemented by Repository with some Interceptors.

Inside the Interceptor I see that IRepositoryProxy basetype is Object and Not the Repository.

For example: I resolve IRepository and call GetFunctionalityPurposes, what I want is the call to GetAllFunctionalityPurposes to be also cached/proxied.

This does not work since the methods are invoked in Repository but not IRepositoryProxy.

IRepository

FunctionalityPurpose[] GetFunctionalityPurposes();
FunctionalityPurpose[] GetAllFunctionalityPurposes()

Repository

[Cached("Tender")]
public virtual FunctionalityPurpose[] GetFunctionalityPurposes()
{
    var model = GetAllFunctionalityPurposes()
        .Where(r => !r.IsHidden && !r.GroupId.HasValue);

    return model;
}

[Cached("Tender", "FunctionalityPurpose")]
public virtual FunctionalityPurpose[] GetAllFunctionalityPurposes()
{
    var model = UnitOfWork.GetSet<Model>().Select(f => f.FunctionalityPurpose)
        .Distinct().OrderBy(r => r.Id).ToArray();

    return model;
}

Solution

  • I've looked into https://github.com/castleproject/Windsor/blob/master/src/Castle.Windsor/Windsor/Proxy/DefaultProxyFactory.cs#L110 and I've done the registration the easy way: container.Register(Classes.FromThisAssembly().BasedOn(typeof(IRepositoryBase<,>)) .WithServiceAllInterfaces().WithServiceSelf().LifestyleTransient());

    Note .WithServiceSelf() call, this actually switches class-based proxying