狗,猫和狗viewmodel和catviewModel的Ianimal界面具有Ianimal的依赖性。如何将正确的OBJ CAT注入CatViewModel
在访谈中,我被问到以下问题,以注射正确的依赖注入。我告诉我将使用工厂模式并在注入对象之前检查类型。然后面试官问我,所有课程都会有100个课程。然后他提示使用IEnumerable。但是我没有得到解决方案。下面解释了问题陈述。请帮助我了解以下问题的解决方案。
在WPF MVVM模式中 模型:有一个称为Ianimal的接口,两个类正在实现它。
public class Dog : IAnimal
{
}
public class Cat : IAnimal
{
}
在ViewModel中,有两个类CatViewModel和DogViewModel
public class CatViewModel
{
public CatViewModel(IAnimal animalObj)
{
}
}
public class DogViewModel
{
public DogViewModel(IAnimal animalObj)
{
}
}
在这里DogViewModel应该只采用DogModel对象,而CatViewModel只能采用CatModel对象。在这里,构造函数具有ianimal类型,因此可以将错误的对象注入错误。如何保护和注入正确的类型。
I was asked the below problem in interview of injecting the correct type in dependency injection. I told i will use factory pattern and check the type before injecting the object. Then interviewer asked me if there are 100 classes will you do for all the classes. then he gave hint to use Ienumerable. But I did not get the solution. Below explained the problem statements. Please help me to understand the solution for the below problem.
in WPF MVVM pattern
Model : Has an Interface called IAnimal and two classes are implementing it.
public class Dog : IAnimal
{
}
public class Cat : IAnimal
{
}
in viewmodel having two classes CatViewModel and DogViewModel
public class CatViewModel
{
public CatViewModel(IAnimal animalObj)
{
}
}
public class DogViewModel
{
public DogViewModel(IAnimal animalObj)
{
}
}
here DogViewModel should take only DogModel object and CatViewModel should take only CatModel Object. Here constructor is having IAnimal type so there can be posibility of injecting wrong object. How to protect and inject correct type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论