Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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# 如何使用Unity IoC解决泛型类型继承_C#_Wpf_Dependency Injection_Inversion Of Control - Fatal编程技术网

C# 如何使用Unity IoC解决泛型类型继承

C# 如何使用Unity IoC解决泛型类型继承,c#,wpf,dependency-injection,inversion-of-control,C#,Wpf,Dependency Injection,Inversion Of Control,我是DI和IoC的新手,我有一个接口解决问题 技术:WPF、C#、Unity容器 我尝试通过模型类型动态显示通用网格,这些模型将定义在筛选数据时要调用的正确服务的解析。我这样做是因为我登陆的项目几乎有+100个业务对象。我不会为每个视图/视图模型/界面创建视图/视图模型/界面 因此,在模型中,我有一个例子:CustomerModel/OrderModel/。。。 在我的服务中:CustomerService和ICustomerService/OrderService和IOrderService/

我是DI和IoC的新手,我有一个接口解决问题

技术:WPF、C#、Unity容器

我尝试通过模型类型动态显示通用网格,这些模型将定义在筛选数据时要调用的正确服务的解析。我这样做是因为我登陆的项目几乎有+100个业务对象。我不会为每个视图/视图模型/界面创建视图/视图模型/界面

因此,在模型中,我有一个例子:CustomerModel/OrderModel/。。。 在我的服务中:CustomerService和ICustomerService/OrderService和IOrderService/。。。我的所有服务也都继承自IBaseService,其中T是一个模型。 像这样:

//For the service call
public interface IBaseService<TModel>
{
    Task<IEnumerable<TModel>> GetWithFilter(string filterQuery);
}

public interface ICustomerService : IBaseService<CustomerModel>
{
    IEnumerable<CustomerModel> Get(string filterQuery);
    //...
}

public class CustomerService : ICustomerService
{
    // Inherit form IBaseService<TModel>
    public IEnumerable<CustomerModel> GetWithFilter(string filterQuery)
    {
        return Get(filterQuery); //
    }

    // Inherit from ICustomerService
    public IEnumerable<CustomerModel> Get(string filterQuery = null)
    {
        // some code
        // return IEnumerable<CustomerModel>
    }
}

//A part of the generic ViewModel
public class UC_GridViewModel<TModel> : BaseViewModel, IUC_GridViewModel<TModel>
{

    private IBaseService<TModel> _modelService;
    public IBaseService<TModel> ModelService
    {
        get { return _modelService; }
        set { Set(ref _modelService, value); }
    }

    private ObservableCollection<TModel> _sourceItem;
    public ObservableCollection<TModel> SourceItem
    {
        get { return _sourceItem; }
        set { Set(ref _sourceItem, value); }
    }

    [InjectionConstructor]
    public UC_GridViewModel(IEnumerable<TModel> sourceItem, IBaseService<TModel> modelService)
    {
        _sourceItem = new ObservableCollection<TModel>(sourceItem);
        _modelService = modelService;
    }

    // This is called by a command
    public void FilterGridCommand(string filterQuery)
    {
        _sourceItem = _modelService.GetWithFilter(filterQuery);
    }

}

//In IoC Container (Resolver layer bind to Unity Container)
DependencyResolver.RegisterType(typeof(IBaseService<CustomerModel>), typeof(CustomerService));
DependencyResolver.RegisterType<ICustomerService, CustomerService>();
DependencyResolver.RegisterType(typeof(IUC_GridViewModel<>), typeof(UC_GridViewModel<>));
//用于服务调用
公共接口IBaseService
{
任务GetWithFilter(字符串过滤器查询);
}
公共接口ICustomerService:IBaseService
{
IEnumerable Get(字符串过滤器查询);
//...
}
公共类CustomerService:ICCustomerService
{
//从IBaseService继承
公共IEnumerable GetWithFilter(字符串筛选器查询)
{
返回Get(filterQuery)//
}
//从ICCustomerService继承
public IEnumerable Get(字符串filterQuery=null)
{
//一些代码
//返回数
}
}
//通用视图模型的一部分
公共类UC_GridViewModel:BaseViewModel、IUC_GridViewModel
{
专用IBaseService(IBaseService)modelService ;;
公共IBaseService模型服务
{
获取{return\u modelService;}
set{set(ref _modelService,value);}
}
私有可观测集合_sourceItem;
公共可观测集合源项
{
获取{return\u sourceItem;}
集合{set(ref _sourceItem,value);}
}
[注入构造函数]
公共UC_GridViewModel(IEnumerable sourceItem,IBaseService modelService)
{
_sourceItem=新的ObservableCollection(sourceItem);
_modelService=modelService;
}
//这是由命令调用的
公共void FilterGridCommand(字符串filterQuery)
{
_sourceItem=\u modelService.GetWithFilter(filterQuery);
}
}
//在IoC容器中(解析器层绑定到Unity容器)
RegisterType(typeof(IBaseService),typeof(CustomerService));
DependencyResolver.RegisterType();
RegisterType(typeof(IUC_GridViewModel)、typeof(UC_GridViewModel));
我除了: 若我实例化一个UC_GridViewModel,我将从CustomerService类型获得一个ModelService,它将执行从IBaseService继承的方法

当我运行我的解决方案时,我得到:
ResolutionFailedException:解析失败,出现错误:没有可用于类型API\u调用方的公共构造函数。IBaseService`1

InvalidOperationException:没有可用于类型API\u Caller.IBaseService`1[BO\u Models.CustomerModel]的公共构造函数。


那么,在这种情况下,如何解决我的IBaseService以避免这些错误呢?

我会尝试对您的第一个registertype进行注释。您应该将ICCustomerService注册为customerservice。由于尚未注册customermodel,您可能还需要在解析中使用ParameterOverrides。但是我希望customerservice不需要任何参数就可以知道它是关于客户的。你好,安迪,谢谢你的回答。如果我删除第一行(输入错误)并注册我使用的每种类型,它就会工作:DependencyResolver.RegisterType();DependencyResolver.RegisterType()。。。我一直试图从一个抽象的BaseCaller继承所有服务:IBaseCaller,并像这样注册:dependencysolver.RegisterType(typeof(IBaseService),typeof(BaseService));但这并没有起到太大的作用,我在网上读到的答案是:客户服务是否真的会返回除客户模型之外的任何东西?