In our project we use a castle container for resolving dependencies. Basically we provide soap services for performing different tasks.
public interface IServiceA
{
public ServiceResponse Process(ServiceRequest request);
}
public class ServiceA : IServiceA
{
public ServiceResponse Process(ServiceRequest request)
{
/////process stuff
}
}
public interface IServiceB
{
public ServiceResponse ReProcess(ServiceRequest request);
}
public class ServiceB : IServiceB
{
private IServiceA _svcA;
public ServiceB ()
{
_svcA= Container.Get<IServiceA>();
}
public ServiceResponse ReProcess(ServiceRequest request)
{
////stuff
_svcA.Process(new ServiceRequest());
}
}
I can reuse the process method of svcA in order to not have bloated duplicated code, but for this I need to tell svcA when I call its process method , that the call is from within svcB’s Reprocess method, so that svcA’ process method can look something like
public ServiceResponse Process(ServiceRequest request)\
{
if (calledFromSvcB)
{
//do stuff
}
//process
}
The restriction is I cannot modify the contract meaning the signature of the methods offered by serviceA, or its types.
The only idea i came up with is :
public class ServiceA : IServiceA
{
public bool IsCalledFromSvcB {get; set;}
public ServiceResponse Process(ServiceRequest request)
{
/////process stuff
}
}
public class ServiceB : IServiceB
{
private IServiceA _svcA;
public ServiceB ()
{
_svcA= Container.Get<IServiceA>();
}
public ServiceResponse ReProcess(ServiceRequest request)
{
_svcA.GetType().GetProperty("IsCalledFromSvcB ").SetValue(this, true);
////stuff
_svcA.Process(new ServiceRequest());
}
}
which is really ugly. Any other ideas to inject this?
Setting a property or field on the inner service won’t work if you can have multiple requests being processed at the same time.
A second interface would probably be a better idea. Something like: