给定以下基类:
public abstract class PurchaseSystemControllerBase<TController, TViewModel> : IInitializable
where TController : PurchaseSystemControllerBase<TController, TViewModel>
where TViewModel : PurchaseSystemViewModelBase<TController, TViewModel> {
protected TViewModel ViewModel { get; private set; }
...
}
public abstract class PurchaseSystemViewModelBase<TController, TViewModel> : ViewModelBase
where TController : PurchaseSystemControllerBase<TController, TViewModel>
where TViewModel : PurchaseSystemViewModelBase<TController, TViewModel> {
protected TController Controller { get; private set; }
...
}
具体实现如下:
public sealed class PurchaseSystemController : PurchaseSystemControllerBase<PurchaseSystemController, PurchaseSystemViewModel> {
...
}
public sealed class PurchaseSystemViewModel : PurchaseSystemViewModelBase<PurchaseSystemController, PurchaseSystemViewModel> {
...
}
有没有一种方法可以简化此操作,以便进行以下操作?
public sealed class PurchaseSystemController : PurchaseSystemControllerBase<PurchaseSystemViewModel> {
...
}
public sealed class PurchaseSystemViewModel : PurchaseSystemViewModelBase<PurchaseSystemController> {
...
}
不,不幸的是,至少在我不知道的情况下,没有丢失某些类型安全性。我的协议缓冲区端口中的消息类型与其对应的构建器类型之间的设置非常相似。
如果只用一些非泛型基本类型声明ViewModel
andController
属性,那很好-但是,如果您需要两者确切地知道相应的类型,那么您将一团糟。
(请注意,您可能要考虑ViewModel和Controller是否应该彼此了解。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句