C# 依赖注入会导致不必要的实现

C# 依赖注入会导致不必要的实现,c#,.net,dependency-injection,C#,.net,Dependency Injection,我正在创建一个实现IAnimal接口的Animal类。然后,Cats和Dogs类都实现了IAnimal接口。目前,我在IAnimal中只保留了3个简单的方法,以便进行简短的演示。主要类动物是通过依赖注入(DI)构建的 当IAnimal有更多的方法时,例如:Cats类只实现SomethingOnlyCatsDo方法,Dogs类实现SomethingOnlyDogsDo方法,那么每个类内部都会有更多不必要的实现(如当前示例中的Cats().CatchDisk()或Dogs().CatchMouse(

我正在创建一个实现IAnimal接口的Animal类。然后,Cats和Dogs类都实现了IAnimal接口。目前,我在IAnimal中只保留了3个简单的方法,以便进行简短的演示。主要类动物是通过依赖注入(DI)构建的

当IAnimal有更多的方法时,例如:Cats类只实现SomethingOnlyCatsDo方法,Dogs类实现SomethingOnlyDogsDo方法,那么每个类内部都会有更多不必要的实现(如当前示例中的Cats().CatchDisk()或Dogs().CatchMouse()

我的问题是,有没有什么方法可以帮助我继续使用DI,但避免不必要的实现的增加

public interface IAnimal
{
    void Sound();
    void CatchMouse();
    void CatchDisk();

    // What if there are more methods here
    //string GetOwnerName();
    //void SomethingOnlyCatsDo();
    //void SomethingOnlyDogsDo();
}

public class Cats : IAnimal
{
    public void Sound()
    {
        Console.WriteLine("Meow meow");
    }

    public void CatchMouse()
    {
        Console.WriteLine("Catching mouse");
    }

    public void CatchDisk()
    {
        throw new NotImplementedException();
    }
}

public class Dogs : IAnimal
{
    public void Sound()
    {
        Console.WriteLine("Woof woof");
    }

    public void CatchDisk()
    {
        Console.WriteLine("Catching disk");
    }

    public void CatchMouse()
    {
        throw new NotImplementedException();
    }
}

// Main class
public class Animals
{
    private readonly IAnimal _animal;

    public Animals(IAnimal animal)
    {
        _animal = animal;
    }

    public void Sound()
    {
        _animal.Sound();
    }

    public void CatchADisk()
    {
        _animal.CatchDisk();
    }

    public void CatchAMouse()
    {
        _animal.CatchMouse();
    }
}
你可以用

接口隔离原则(ISP)规定,不应强制任何客户端依赖于它不使用的方法

您的
IAnimal
接口将只有
Sound()
,然后您创建一个名为
ICat
的新接口,该接口继承自
IAnimal
,该接口将有
CatchMouse()
。您的类
Cats
将继承自
ICat

一个实用的例子。

您可以使用

接口隔离原则(ISP)规定,不应强制任何客户端依赖于它不使用的方法

您的
IAnimal
接口将只有
Sound()
,然后您创建一个名为
ICat
的新接口,该接口继承自
IAnimal
,该接口将有
CatchMouse()
。您的类
Cats
将继承自
ICat


一个实际的例子。

如果遵循坚实的原则,尤其是I(界面分离,),
IAnimal
不应该有
CatchDisk
CatchMouse
方法。相反,您应该使用
Sound()
方法使用
IAnimal
,并使用独立的接口
ICatchesMouse
ICatchesDisk
。这样一来,任何动物都不必实施不必要的方法。

如果遵循坚实的原则,尤其是I(界面分离,),
IAnimal
就不应该有
CatchDisk
CatchMouse
方法。相反,您应该使用
Sound()
方法使用
IAnimal
,并使用独立的接口
ICatchesMouse
ICatchesDisk
。这样一来,任何动物都不必实施不必要的方法。

你是对的,你会有一大堆麻烦,而且做得很糟糕。您可能需要为猫或狗以及IAnimal接口携带适当方法签名的ICat和IDog接口。我试图创建ICat和IDog,但动物类构造函数也需要ICat和IDog的两个依赖项。在本例中,我试图将其简化为只使用一个依赖项(如IAnimal)。或者我在假设两个依赖项时弄错了吗?您在
CatchMouse
中抛出异常,这违反了“接口隔离原则”。阅读坚实的原则。对于实施IAnimal的事情,它们必须是所有动物都会采取的行动。你的问题与依赖性注射有什么关系?你是对的,你会有一个大混乱,而且做得很糟糕。您可能需要为猫或狗以及IAnimal接口携带适当方法签名的ICat和IDog接口。我试图创建ICat和IDog,但动物类构造函数也需要ICat和IDog的两个依赖项。在本例中,我试图将其简化为只使用一个依赖项(如IAnimal)。或者我在假设两个依赖项时弄错了吗?您在
CatchMouse
中抛出异常,这违反了“接口隔离原则”。阅读坚实的原则。对于实施IAnimal的事情,它们必须是所有动物共同的行为。你的问题与依赖性注射有什么关系?谢谢你的指导。关于上述相同的实现逻辑,我还有另一个问题。假设我在Animals类中创建了一个名为CatchSomething的方法,那么我必须检查_animal的类型(在构造函数中传递)是否是狗或猫的类型,以调用相应的CatchDisc或CatchMouse。有没有其他方法让CatchSomething()方法知道在不检查动物类型的情况下调用Dog或Cat中的哪个方法?要实现这一点,构造函数类型必须是
IAnimal
,并且此接口必须有
Catch()方法。继承自
IAnimal
的类必须实现此方法,猫仍然可以捕获鼠标,狗仍然可以捕获磁盘。如果您有一个动物没有实现
Catch()
,那么您再次违反了ISP(接口隔离原则),要修复它,您必须创建两个新接口,如
ICanCatchAnimal
ICanCatchAnimal
(不完全是这些名称,但您得到了!!),它们继承自
IAnimal
,这是隔离谢谢你的指导。关于上述相同的实现逻辑,我还有另一个问题。假设我在Animals类中创建了一个名为CatchSomething的方法,那么我必须检查_animal的类型(在构造函数中传递)是否是狗或猫的类型,以调用相应的CatchDisc或CatchMouse。有没有其他方法让CatchSomething()方法知道在不检查动物类型的情况下调用Dog或Cat中的哪个方法?要实现这一点,构造函数类型必须是
IAnimal
,并且此接口必须有
Catch()方法。继承自
IAnimal
的类必须实现此方法,猫仍然可以捕获鼠标,狗仍然可以捕获磁盘。如果你有一只动物没有实现
Catch()
,那么你又一次违反了ISP(接口隔离原则)来修复它