Dan Kendall
Dan Kendall

Reputation: 703

Ambient Context Where Members Have Dependencies

Hi this is probably just a lack of thought but I can't see how to solve this one "nicely". I have a component responsible for auditing that is used across the system, such:

public class RemoteAuditLogger: AuditLogger{}

where AuditLogger is an interface. As it stands I'm making use of constructor injection across several classes (this list of classes is growing). So I'm looking at implementing Ambient Context. The problem I have is that RemoteAuditLogger has dependencies of its own (in this case the remote audit service proxy).

So I want:

public abstract class SomeContext{
    private static SomeContext _currentCtx;
    public static SomeContext Current{
        get{
           if(_currentCtx == null){
              _currentCtx = new DefaultContext();
           }
           return _currentCtx;
        }
        set{
           _currentCtx = value;
        }
    }

    public abstract AuditLogger AuditLogger{ get; }
}

In this scenario DefaultContext will return a RemoteAuditLogger. Is there a way I can retain the decoupled nature of the context whilst satisfying the dependencies of RemoteLogger? I don't want SomeContext depending on the remote audit proxy, for e.g., so it "feels" like I need to keep a default constructor on DefaultContext.

Upvotes: 3

Views: 503

Answers (0)

Related Questions