Problem trying to resolve type in Windsor
I have the following items in my solution:
public interface IHandle<TEvent>
{
void Handle(TEvent event);
}
public interface IPresenter<TView>
{
}
public abstract class Presenter<TView> : IPresenter<TView>
{
protected view;
public TView View
{
set { view = value;}
}
// snip...
}
public interface IMyPresenter : IPresenter<IChangeRequestReviewManagementView>
{
// snip...
}
public class MyPresenter : Presenter<MyView>, IMyPresenter, IHandle<CustomEvent>
{
// snip...
}
And have the following registration for Windsor to register the presenters:
container.Register(AllTypes.Of(typeof(IPresenter<>))
.FromAssemblyNamed("Project.Presentation")
.WithService.Select((type, baseType) => new Type[] {GetInterfaceFromConvention(type)})
.Configure(config => config.LifeStyle.PerWebRequest));
private static Type GetInterfaceFromConvention(Type type)
{
return (from found in type.GetInterfaces()
where found.Name.Equals("I" + type.Name)
select found).FirstOrDefault();
}
This works perfectly and I can resolve the presenters in my pages using:
public IMyPresenter Presenter
{
set
{
presenter = value;
}
}
Using this http://code.google.com/p/sneal/wiki/AspNetWindsorModule, the problem comes when I try to wire in the support for handling events supplied via a separate EventAggregator which calls implementers of IHandle<>. I added this registration:
container.Register(AllTypes.Of(typeof(IHandle<>))
.FromAssemblyNamed("Project.Presentation")
.BasedOn(typeof(IHandle<>))
.WithService.Base());
As this works for oth开发者_如何学编程er areas that implement IHandle<> but after adding this the container cannot no longer resolve IMyPresenter to MyPresenter, so I must be missing something?!
Ok, I now see what the problem is (or at least I suppose I do - didn't run the code myself).
The problem is that you break SRP by making your MyPresenter
do two jobs.
Then, when you register your components you try to register MyPresenter
twice, and the first registration wins (2nd time it won't get registered).
To fix this you should refactor MyPresenter
to do just one thing. This will most likely require you to introduce MyPresenterHandler
that will have dependency on MyPresenter
to do its job.
This way you will have no conflicting registrations and everithing should work just fine.
精彩评论