Castle Windsor register closed - open generic

629 views Asked by At
public interface IProcess {}

public interface IProcessHandler<in TProcess> where TProcess : IProcess {
    void Handle(TProcess message);
}

public class AccessMessage<TProcess> : IProcess where TProcess : IProcess {
    public virtual string Username {get;protected set;}
    public virtual TProcess InnerProcess { get; protected set; }
}

public class AccessMessageHandler<TProcess> : IProcessHandler<AccessMessage<TProcess>> where TProcess : IProcess {
    public AccessMessageHandler(IProcessHandler<TProcess> innerHandler){}
    public void Handle(AccessMessage<TProcess> message){
      // access control
      _innerHandler.Handle(message.InnerProcess)
    }
}

public class JustDoIt : IProcess {}

public class JustDoItHandle : IProcessHandler<JustDoIt> {
    public void Handle(JustDoIt message) {
      // handle
    }
}

container.Register(Classes.FromAssemblyInThisApplication()
                               .BasedOn(typeof(IProcessHandler<>))
                               .If(t => t.Name.Contains("AccessMessageHandler") == false)
                               .WithService.AllInterfaces()
                               .LifestyleTransient()
                               .AllowMultipleMatches());

// container.Register(Component.For(typeof (IProcessHandler<AccessMessage<>>)).ImplementedBy(typeof(AccessMessageHandler<>))); // compiler not allowed this this 
// container.Register(Component.For(typeof (IProcessHandler<AccessMessage<IProcess>>)).ImplementedBy(typeof(AccessMessageHandler<>))); // error when resolving (No component for supporting the service)

var handler = Container.Resolve<IProcessHandler<AccessMessageHandler<JustDoIt>>>();
    handler.Handle(new AccessMessage(new JustDoIt(),"username"));

how can I register IProcessHandler> ?

0

There are 0 answers