[英]Implementing CastleWindsor with Model View Presenter (MVP)
I would like to implement CastleWindsor with the MVP pattern, but I keep getting an 'Object Reference Not Set to an Object reference on the Presenter when the repository is called to obtain some data. 我想用MVP模式实现CastleWindsor,但是在调用存储库以获取一些数据时,我一直在Presenter上获得“未设置为对象引用的对象引用”的信息。
This is how I did it and I am wondering if there is anything wrong, so please let me know if you can: 这是我的操作方式,我想知道是否有什么问题,所以请告诉我您是否可以:
public class CategoryPresenter
{
ICategoryRepository categoryRepository;
ICategoryView categoryView;
public CategoryPresenter(ICategoryView _categoryView, ICategoryRepository _categoryRepository)
{
categoryView = _categoryView;
categoryRepository = _categoryRepository;
}
//public CategoryPresenter(ICategoryView _categoryView) : this (_categoryView, new CategoryRepository())
//{ }
public CategoryPresenter(ICategoryView _view)
{
categoryView = _view;
}
public IEnumerable<object> GetActiveCategories()
{
return categoryRepository.GetActiveCategories();
}
}
public static class IoC
{
public static IWindsorContainer windsorContainter { get; set; }
}
class IoCConfig { IoCConfig类{
public static IWindsorContainer RegisterCastleWindsorContainer()
{
IWindsorContainer windsorContainer = new WindsorContainer()
.Install(new RepositoryInstaller())
IoC.windsorContainter = windsorContainer;
return windsorContainer;
}
} }
public class RepositoryInstaller: IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<ICategoryRepository>().ImplementedBy<CategoryRepository>).LifestyleTransient());
}
}
Finally in Global.ascx file I am doing this at App_Start: 最后,在Global.ascx文件中,我在App_Start上执行此操作:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
IoCConfig.RegisterCastleWindsorContainer();
}
With this, the error message is as said above; 这样,错误消息如上所述。 the error happens at the presenter's method: GetActiveCategories(); 该错误发生在演示者的方法上:GetActiveCategories();
As you see at no where in code I invoke the resolve method on the container. 如您所见,我在代码中的任何地方都没有调用容器的resolve方法。
Please let me know if if you have any suggestions. 如果您有任何建议,请告诉我。
Thank you. 谢谢。
I have resolved this to the IoC Class 我已经解决了这个在国际奥委会类
public static T Resolve<T>()
{
try
{
return windsorContainer.Resolve<T>();
}
catch (Exception e)
{
throw e;
}
}
And then add this to the presenter: 然后将其添加到演示者:
ICategoryRepository categoryRepository = IoC.Resolve<ICategoryRepository>();
ICategoryView categoryView = IoC.Resolve<ICategoryView>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.