如何实现SOLID原则到现有项目 [英] How to implement SOLID principles into an existing project

查看:194
本文介绍了如何实现SOLID原则到现有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这个问题的主观性道歉,但我有点卡住,我将不胜感激任何人谁是有来处理这个问题之前的一些指导和建议:

I apologize for the subjectiveness of this question, but I am a little stuck and I would appreciate some guidance and advice from anyone who's had to deal with this issue before:

我有(什么是成为)一个非常大的RESTful API项目写在C#2.0和我的一些课程已经成为可怕的。我的主要API类就是这样的一个例子 - 有几十个成员和方法(可能接近上百个)。你可以想象,这已经成为一个小的噩梦,不仅要保持这种代码,但哪怕只是导航中的代码已经成为一个苦差事。

I have (what's become) a very large RESTful API project written in C# 2.0 and some of my classes have become monstrous. My main API class is an example of this -- with several dozen members and methods (probably approaching hundreds). As you can imagine, it's becoming a small nightmare, not only to maintain this code but even just navigating the code has become a chore.

我有理由新来的SOLID原则,我的设计模式大规模风扇(但我仍然在这个阶段,我可以的实施他们,但不是很够,知道什么时候为使用他们 - 在情况下它不是那么明显)

I am reasonably new to the SOLID principles, and I am massive fan of design patterns (but I am still at that stage where I can implement them, but not quite enough to know when to use them - in situations where its not so obvious).

我需要打破我的课下来的大小,但我在如何最好地的丧失去这样做。我的同胞StackOverflow'ers可以请建议,他们已采取现有的代码巨石方式和削减他们的威风?

I need to break my classes down in size, but I am at a loss of how best to go about doing it. Can my fellow StackOverflow'ers please suggest ways that they have taken existing code monoliths and cut them down to size?

推荐答案

单一职责原则 - 一个类应该只有一个理由去改变。如果你有一个单片类,那么它可能有不止一个理由去改变。简单地定义你的一个原因改变,要像颗粒状的合理的。我建议启动大。重构代码的三分之一出来到另一个类。一旦你有了这一点,那么与新类从头开始。从一个类直接将20望而生畏

Single Responsibility Principle - A class should have only one reason to change. If you have a monolithic class, then it probably has more than one reason to change. Simply define your one reason to change, and be as granular as reasonable. I would suggest to start "large". Refactor one third of the code out into another class. Once you have that, then start over with your new class. Going straight from one class to 20 is too daunting.

打开/关闭原理 - 一个类应该对扩展开放,但收盘变革。合理的地方,你的成员和方法标记为虚拟的或抽象的。每个项目应该是在自然界中相对较小,给你一些基本功能或行为的定义。但是,如果您以后需要更改的功能,您将能的添加的代码,而不是的变更的代码以引入新的/不同的功能。

Open/Closed Principle - A class should be open for extension, but closed for change. Where reasonable, mark your members and methods as virtual or abstract. Each item should be relatively small in nature, and give you some base functionality or definition of behavior. However, if you need to change the functionality later, you will be able to add code, rather than change code to introduce new/different functionality.

里氏替换原则 - 一个类应该是替代其基类。这里的关键,在我看来,是做正确的继承。如果你有一个巨大的case语句,或者如果语句检查派生类型的对象,那么你违反​​了这一原则,并需要重新思考你的方法的两页。

Liskov Substitution Principle - A class should be substitutable for its base class. The key here, in my opinion, is do to inheritance correctly. If you have a huge case statement, or two pages of if statements that check the derived type of the object, then your violating this principle and need to rethink your approach.

接口隔离原则 - 在我看来,这个原理非常类似于单一职责原则。它只是专门适用于较高水平(或成熟)类/接口。在一个大的类中使用这个原则的一种方法是使你的类实施的的接口。其次,改变这一切,用你的类是接口类型的类型。这会破坏你的代码。但是,它会指出你究竟是如何消费类。如果你有三个实例,每用自己的方法和属性的子集,那么现在你知道你需要三种不同的接口。每个接口代表​​一个集体的功能集,一个理由去改变。

Interface Segregation Principle - In my mind, this principle closely resembles the Single Responsibility principle. It just applies specifically to a high level (or mature) class/interface. One way to use this principle in a large class is to make your class implement an empty interface. Next, change all of the types that use your class to be the type of the interface. This will break your code. However, it will point out exactly how you are consuming your class. If you have three instances that each use their own subset of methods and properties, then you now know that you need three different interfaces. Each interface represents a collective set of functionality, and one reason to change.

依赖倒置原则 - 父/子寓言让我明白了这。想想一个父类。它定义的行为,但不关心细节脏。它是可靠的。子类,然而,是所有有关的细节,因为它经常改变,不能依赖于。你总是想依靠父母,负责班级,从不周围的其他方法。如果你有这取决于一个子类的父类,你会当你改变一些东西得到意想不到的行为。在我看来,这是SOA的相同的心态。服务合同定义了输入,输出和行为,没有细节。

Dependency Inversion Principle - The parent / child allegory made me understand this. Think of a parent class. It defines behavior, but isn't concerned with the dirty details. It's dependable. A child class, however, is all about the details, and can't be depended upon because it changes often. You always want to depend upon the parent, responsible classes, and never the other way around. If you have a parent class depending upon a child class, you'll get unexpected behavior when you change something. In my mind, this is the same mindset of SOA. A service contract defines inputs, outputs, and behavior, with no details.

当然,我的观点和理解可能不完整或错误。我建议从谁掌握这些原则,像鲍勃大叔的人学习。对我来说一个很好的出发点是他的书,敏捷原则,模式和做法在C#。另一个很好的资源是叔叔鲍勃Hanselminutes

Of course, my opinions and understandings may be incomplete or wrong. I would suggest learning from people who have mastered these principles, like Uncle Bob. A good starting point for me was his book, Agile Principles, Patterns, and Practices in C#. Another good resource was Uncle Bob on Hanselminutes.

当然,作为 Joel和杰夫指出时,这些原则,而不是规则。他们是工具,以帮助指导你,土地的不规律。

Of course, as Joel and Jeff pointed out, these are principles, not rules. They are to be tools to help guide you, not the law of the land.

编辑:

我刚刚发现这些的SOLID截屏看起来真的很有趣。每间大约10-15分钟。

I just found these SOLID screencasts which look really interesting. Each one is approximately 10-15 minutes long.

这篇关于如何实现SOLID原则到现有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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