固体原理的例子地方? [英] Solid Principle examples anywhere?

查看:97
本文介绍了固体原理的例子地方?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们所有的写代码的一些模式,即使我们不意识到这一点。我想真正了解一些在 SOLID 的原则,以及如何在现实世界中应用这些原则。



我挣扎<强> D



我有时会混淆的依赖倒置的用的依赖关系注入的。这是否意味着,只要你保持取决于抽象的东西(IE:接口)。你完成



没有任何人有甚至解释了它<小C#示例? / p>

感谢。


解决方案

有一个看看马克·西曼的博客,或者,更妙的是,买他的书。它涵盖了这么多的不仅仅是DI多。我很欣赏你可能只是想要一个简单的示例得到一起去。然而,这是一个主题,很多谁声称理解不和,因此值得学习很好。



这是说,这里是一个非常简单的例子。据我了解的术语是控制和依赖注入的反转。控制反转是指你给一个类的依赖其他一些类,反对类的控制来控制的依赖本身,通常是通过在关键字的事实。这种控制是通过在那里的一类给定,或注射,其依赖依赖注入发挥。这可以通过一个IoC框架或代码来实现。注射可以在类的构造函数通过属性或方法的参数进行。 。依赖关系可以是任何类型,它们不必是抽象的。



下面是一个类,列出谁没有掺杂环法自行车赛获奖名单:

 类CleanRiders 
{
名单,LT;骑士> GetCleanRiders()
{
变种riderRepository =新MsSqlRiderRepository();

riderRepository.GetRiders.Where(X => x.Doping ==假);
}
}

本类是依赖于MsSqlRiderRepository。类需要创建实例的控制权。问题是,这种依赖性是不灵活的。 ,很难将其更改为一个OracleRiderRepository或TestRiderRepository



IOC和DI解决这个问题对我们来说:

 类CleanRiders 
{
私人IRiderRepository _repository;

公共CleanRiders(IRiderRepository库)
{
_repository =库;
}

名单,LT;骑士> GetCleanRiders()
{
_repository.GetRiders.Where(X => x.Doping ==假);
}
}

现在的类只有在接口上的依赖。依赖控制已放弃了对类的创建者,必须通过它的构造被注入:

 无效的主要()
{
变种C =新CleanRiders(新MsSqlRepository());

VAR车手= c.GetRiders();
}



可以说,一个更加灵活,可测试性和固体的方法。


We all write code with some patterns even when we dont realise it. I am trying to really understand some of the S.O.L.I.D principles and how you apply these principles in the real world.

I am struggling with "D".

I sometimes confuse Dependency Inversion with Dependency Injection. Does it mean that as long as you keep things depending on abstraction (IE:interfaces) you are done.

Does anybody have even a small C# example that explains it?

Thanks.

解决方案

Have a look at Mark Seeman's blog or, even better, buy his book. It covers so much more than just DI. I appreciate you probably just want a simple sample to get going with. However, it's a subject that many who claim to understand don't and therefore worth learning well.

That said, here's a very simple example. The terminology as I understand it is Inversion of Control and Dependency Injection. Inversion of Control refers to the fact that you give control of a class's dependencies to some other class, opposed to the class controlling the dependency itself, usually via the new keyword. This control is exerted via Dependency Injection where a class is given, or injected, with its dependencies. This can be done via an IoC framework or in code. Injection can be performed in the class's constructor, via a property or as a method's parameter. Dependencies can be any type, they don't have to be abstract.

Here's a class that lists Tour de France winners who haven't doped:

class CleanRiders
{
    List<Rider> GetCleanRiders()
    {
        var riderRepository = new MsSqlRiderRepository();

        riderRepository.GetRiders.Where(x => x.Doping == false);
    }
}

This class is dependent on the MsSqlRiderRepository. The class takes control of the creation of the instance. The problem is that this dependency is inflexible. It's hard to change it to a OracleRiderRepository or a TestRiderRepository.

IoC and DI solve this for us:

class CleanRiders
{
    private IRiderRepository _repository;

    public CleanRiders(IRiderRepository repository)
    {
        _repository = repository;
    }

    List<Rider> GetCleanRiders()
    {
        _repository.GetRiders.Where(x => x.Doping == false);
    }
}

Now the class has only a dependency on an Interface. Control of the dependency has been given up to the class's creator and must be injected via its constructor:

void Main()
{
    var c = new CleanRiders(new MsSqlRepository());

    var riders = c.GetRiders();
}

Arguably, a more flexible, testable and SOLID approach.

这篇关于固体原理的例子地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆