我正在为我的 MVC 4 项目构建(尝试构建)通用存储库、任务和控制器,并使用它来学习泛型和接口。我已经走了很远,但坚持这个错误。CustomerContactsListViewModel 确实继承自 IViewModelList,这就是我感到困惑的原因。
错误:
The type 'OTIS.AppServ.OrderMgmt.ViewModels.CustomerContactsListViewModel' cannot be used as type parameter 'TViewModelList' in the generic type or method 'OTIS.AppServ.BaseAppServGrid<TRepository,TViewModelSingle,TViewModelList>'. There is no implicit reference conversion from 'OTIS.AppServ.OrderMgmt.ViewModels.CustomerContactsListViewModel' to 'OTIS.AppServ.IViewModelList<OTIS.domain.OrderMgmt.Customer,OTIS.AppServ.OrderMgmt.ViewModels.CustomerContactsListViewModel>'.
界面:
public interface IViewModelList<TClass, TViewModelList>
{
IEnumerable<TClass> ConvertViewModelToClass(IEnumerable<TViewModelList> entityList);
IEnumerable<TViewModelList> ConvertClassToViewModel(IEnumerable<TClass> entityList);
}
继承接口的视图模型:
public class CustomerContactsListViewModel : IViewModelList<CustomerContact, CustomerContactsListViewModel>, IEntity
被继承的通用任务(应用程序服务)类:
public class BaseAppServGrid<TRepository, TViewModelSingle, TViewModelList> : BaseAppServ<TRepository, TViewModelSingle>
where TRepository : class, IEntity, IAuditStamps, new()
where TViewModelSingle : class, IViewModelSingle<TRepository, TViewModelSingle>, new()
where TViewModelList : class, IEntity, IViewModelList<TRepository, TViewModelList>, new()
尝试继承 Base 的特定 Task/App 服务类会引发错误:
public class ManageCustomersAppServ : BaseAppServGrid<Customer, CustomerViewModel, CustomerContactsListViewModel>