Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC 5/UnityConfig-同一接口-多个实现_C#_Asp.net_Asp.net Mvc_Dependency Injection_Unity Container - Fatal编程技术网

C# MVC 5/UnityConfig-同一接口-多个实现

C# MVC 5/UnityConfig-同一接口-多个实现,c#,asp.net,asp.net-mvc,dependency-injection,unity-container,C#,Asp.net,Asp.net Mvc,Dependency Injection,Unity Container,所以我有一个基本的界面: public interface ISearchable { List<AutocompleteResult> Search(string term); } 关于DI和UnityInjector,我是一个不折不扣的人,所以一个完整的工作示例会很好。到目前为止,我得到了很多建议(使用通用接口,使用unity factory)——但我自己尝试了一下,没有一个奏效 我知道在ASP.NET Core中有一个属性可以直接在构造函数的参数列表中指定,但我目前正

所以我有一个基本的界面:

public interface ISearchable
{
    List<AutocompleteResult> Search(string term);
}
关于DI和UnityInjector,我是一个不折不扣的人,所以一个完整的工作示例会很好。到目前为止,我得到了很多建议(使用通用接口,使用unity factory)——但我自己尝试了一下,没有一个奏效

我知道在ASP.NET Core中有一个属性可以直接在构造函数的参数列表中指定,但我目前正在使用MVC5


谢谢。完成了一篇长文章。呸。

如果适合您的场景,您可以尝试这些选项之一

1. 使用命名Serivce注入

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;
}
公共静态无效注册表类型(IUnityContainer容器)
{
容器。注册类型(“城市”);
容器。注册类型(“业主”);
}
公共房东控制器([Dependency(“房东”)]ISearchable searcher)
{
this.searcher=搜索者;
}
2.
公共接口是可启动的,其中T:class
{
//..............
}
公共类LandorReader:可实现
{
公共列表搜索(字符串术语)
{
....
}
}
公共类CityReader:可启用
{
公共列表搜索(字符串术语)
{
....
}
}
公共静态无效注册表类型(IUnityContainer容器)
{
容器。注册类型(“城市”);
容器。注册类型(“业主”);
}
然后在构造函数中使用时,执行以下操作

    private readonly ISearchable<CityReader> _cityReadersearcher;
    private readonly ISearchable<LandlordReader> _landLordsearcher;
    public LandlordController(ISearchable<CityReader> cityReadersearcher, 
     ISearchable<LandlordReader> landLordsearcher)
    {
         _cityReadersearcher= _cityReadersearcher;
        _landLordsearcher = landLordsearcher;
    }
private readonly可访问\u cityReadersearcher;
私有只读可访问的搜索器;
公共土地控制员(ISearchable cityReadersearcher,
ISearchable(搜索器)
{
_城市领导者研究者=_城市领导者研究者;
_landordsearcher=landordsearcher;
}

最好将接口分开。即使它们具有相同的签名,也不意味着它们是相同的接口。使用ICitySearchable和ILANDRORDSearchable,您就可以开始了。为什么?我将在CityReader中实现两个接口(ISearchable+ICityReader)。如果我从多个接口实现,不是更好的类设计吗?仅仅因为它们具有相同的签名并不意味着它们在做相同的事情。搜索房东和搜索城市不仅仅是不同的实现,它们是两件完全不同的事情。它们应该驻留(或继承或扩展)不同的接口。谢谢,它起作用了(我使用了第一个选项)-第二个,我自己已经解决了,但它似乎有点不成熟。再次感谢!
public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<ISearchable, CityReader>("City");
    container.RegisterType<ISearchable, LandlordReader>("Landlord");
}
public LandlordController(ISearchable searcher)
{
    // I want the LandlordReader here, but in the CityController, I want the CityReader....
    this.searcher = searcher;
}
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");
}
    private readonly ISearchable<CityReader> _cityReadersearcher;
    private readonly ISearchable<LandlordReader> _landLordsearcher;
    public LandlordController(ISearchable<CityReader> cityReadersearcher, 
     ISearchable<LandlordReader> landLordsearcher)
    {
         _cityReadersearcher= _cityReadersearcher;
        _landLordsearcher = landLordsearcher;
    }