[英]MVC 5 / UnityConfig - Same Interface - Multiple Implementations
So I have this basic Interface:所以我有这个基本的界面:
public interface ISearchable
{
List<AutocompleteResult> Search(string term);
}
And two classes which implement this interface:以及实现此接口的两个类:
First Class:头等舱:
public class LandlordReader :ISearchable
{
public List<AutocompleteResult> Search(string term)
{
....
}
}
Second Class:第二类:
public class CityReader : ISearchable
{
public List<AutocompleteResult> Search(string term)
{
....
}
}
From this point on, I'm in the dark... In UnityConfig.cs I tried to register them by giving them a name:从这一点开始,我一头雾水……在 UnityConfig.cs 中,我试图通过给它们一个名字来注册它们:
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<ISearchable, CityReader>("City");
container.RegisterType<ISearchable, LandlordReader>("Landlord");
}
In the MVC Controller I have no idea on what to specify (IN THE CONSTRUCTOR) - to tell it that I want one or another:在 MVC 控制器中,我不知道要指定什么(在构造函数中) - 告诉它我想要一个或另一个:
public LandlordController(ISearchable searcher)
{
// I want the LandlordReader here, but in the CityController, I want the CityReader....
this.searcher = searcher;
}
I'm quite a noob, regarding DI and UnityInjector, so a full working example would be good.我是一个菜鸟,关于 DI 和 UnityInjector,所以一个完整的工作示例会很好。 I got lots of advices up to this point (use generic interface, use unity factory) - but trying them myself, none worked.
到目前为止,我得到了很多建议(使用通用接口,使用统一工厂)——但我自己尝试过,没有一个奏效。
I know in ASP.NET Core there's an attribute you can directly specify in the constructor's parameter list, but I am currently working in MVC5.我知道在 ASP.NET Core 中有一个可以直接在构造函数的参数列表中指定的属性,但我目前在 MVC5 中工作。
Thanks.谢谢。 Finished a long post.
写完一篇长文。 Phew.
呼。
You can try one of these options if suitable in your scenario.如果适合您的场景,您可以尝试这些选项之一。
Using Named Serivce Injection使用命名服务注入
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<ISearchable, CityReader>("City");
container.RegisterType<ISearchable, LandlordReader>("Landlord");
}
public LandlordController([Dependency("Landlord")]ISearchable searcher)
{
this.searcher = searcher;
}
public interface ISearchable<T> where T : class
{
//..............
}
public class LandlordReader : ISearchable<LandlordReader>
{
public List<AutocompleteResult> Search(string term)
{
....
}
}
public class CityReader : ISearchable<CityReader>
{
public List<AutocompleteResult> Search(string term)
{
....
}
}
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<ISearchable, CityReader>("City");
container.RegisterType<ISearchable, LandlordReader>("Landlord");
}
and then when using in the constructor do this然后在构造函数中使用时执行此操作
private readonly ISearchable<CityReader> _cityReadersearcher;
private readonly ISearchable<LandlordReader> _landLordsearcher;
public LandlordController(ISearchable<CityReader> cityReadersearcher,
ISearchable<LandlordReader> landLordsearcher)
{
_cityReadersearcher= _cityReadersearcher;
_landLordsearcher = landLordsearcher;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.