SOLID-principles | Code examples of the SOLID principles

 by   harrymt C# Version: Current License: MIT

kandi X-RAY | SOLID-principles Summary

kandi X-RAY | SOLID-principles Summary

SOLID-principles is a C# library. SOLID-principles has no bugs, it has no vulnerabilities, it has a Permissive License and it has low support. You can download it from GitHub.

Code examples of the SOLID principles. A class should take care of a Single Responsibility. This Add method does too much, it shouldnt know how to write to the log and add a customer. Good Way, not violating the single responsibility principle. Now we abstract the logger, so its writing the error. Prefer extension over modification. Violating the Open Closed Principle This is bad, because at the moment, there are 2 types of customer, if we want to add another customer type we have to add a if else below. So Modifying the existing code. This is better, because we structure the code so its easier to extend and hard to modify. Parent class should be able to refer child objects seamlessly during runtime polymorphism. BAD: Violating Liskov substitution principle The parent should easily the replace the child object and not break any functionality, only lose some. e.g. here, we don't want this to add an enquiry so we have to throw a new exception, that is violating the principle. Client should not be forced to use an interface, if it doesn't need it. If we want to add more functionality, don't add to existing interfaces, segregate them out. Just create another interface, that a class can ALSO extend from. High level modules should not depend on low-level modules, but should depend on abstraction. Bad: We are relying on the customer to say that we are using a File Logger, rather than another type of logger, e.g. EmailLogger. Good: We pass in a Logger interface to the customer so it doesnt know what type of logger it is.
Support
    Quality
      Security
        License
          Reuse

            kandi-support Support

              SOLID-principles has a low active ecosystem.
              It has 42 star(s) with 18 fork(s). There are 3 watchers for this library.
              OutlinedDot
              It had no major release in the last 6 months.
              SOLID-principles has no issues reported. There are no pull requests.
              It has a neutral sentiment in the developer community.
              The latest version of SOLID-principles is current.

            kandi-Quality Quality

              SOLID-principles has 0 bugs and 0 code smells.

            kandi-Security Security

              SOLID-principles has no vulnerabilities reported, and its dependent libraries have no vulnerabilities reported.
              SOLID-principles code analysis shows 0 unresolved vulnerabilities.
              There are 0 security hotspots that need review.

            kandi-License License

              SOLID-principles is licensed under the MIT License. This license is Permissive.
              Permissive licenses have the least restrictions, and you can use them in most projects.

            kandi-Reuse Reuse

              SOLID-principles releases are not available. You will need to build from source code and install.
              Installation instructions are not available. Examples and code snippets are available.

            Top functions reviewed by kandi - BETA

            kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
            Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of SOLID-principles
            Get all kandi verified functions for this library.

            SOLID-principles Key Features

            No Key Features are available at this moment for SOLID-principles.

            SOLID-principles Examples and Code Snippets

            No Code Snippets are available at this moment for SOLID-principles.

            Community Discussions

            QUESTION

            What is the best way to use an Interface with multiple classes where classes don't implement the same subinterfaces?
            Asked 2020-Jul-05 at 13:39

            I'm trying to create an generic service-interface per logic-class to communicate with the database. See code examples for an explanation better than words.

            For example:

            Foo

            ...

            ANSWER

            Answered 2020-Jul-05 at 13:39

            Keep it simple! Create FooService that implements IFooService. FooLogic should be removed. You can implement the logic inside the method CreateFoo.

            Since FooService will impement all the methods, you can call ReadBar() instead of _barService.ReadBar(), there is no need for composition since you already have IFooService inheriting from all other interfaces.

            This way, we are still respecting the dependency injection pattern.

            Source https://stackoverflow.com/questions/62740896

            QUESTION

            Refactoring - Breaking Dependencies
            Asked 2018-Oct-06 at 17:59

            Currently I am working on the Racing-Car-Katas. The goal is to refactor a peach of code so that it follows the solid-principles.

            I try to add the Dependency Inversion Principle. Where I can pass a dependency through the constructor.

            Initial Situation

            Insid the class Alarm is the dependency Sensor which generates a psiPressureValue.

            ...

            ANSWER

            Answered 2018-Oct-06 at 00:11

            Ok you want to break the direct coupling between Alarm and Sensor.

            Your proposed solutions shows two constructors, one injecting the Sensor object (created externally) and one creating Sensor object directly. You should drop:

            Source https://stackoverflow.com/questions/52672869

            QUESTION

            not fully grasping why i'd have an interface in a class, rather than inherit from it
            Asked 2018-May-23 at 22:09

            I'm looking over the SOLID principles and came across some bits of code for SRP.

            I have this code, but I do not have a clue why I'd have an interface, declared in the way that I do below? What good does this do to me? I found here, it shows a solution, but it doesn't really explain how the code works better or why things are the way they are.

            The thing I don't understand is:

            IGateUtility _gateUtility; In public class ServiceStation, and right below it is the constructor with IGateUtility as parameter. Why is this written like that? What's the parameter I would have to pass.

            ...

            ANSWER

            Answered 2018-May-23 at 20:48

            The constructor parameter is an example of dependency injection, specifically the technique known as "constructor injection" (which is usually the preferred technique).

            The ServiceStation isn't supposed to contain the logic of a IGateUtility because it doesn't have anything to do with the gate (Single Responsibility Principle). It does need to use the gate however so you pass an object implementing IGateUtility in.

            Generally speaking I don't think inheritance would make sense in this instance regardless; but there is a principle that states:

            Prefer Composition to Inheritance

            Which basically means; inject (compose) objects to gain access to their behavior instead of inheriting from them.

            Source https://stackoverflow.com/questions/50497108

            QUESTION

            Why weakening a precondition does not violate Liskov substitution principle
            Asked 2018-Jan-19 at 09:06

            I am learning about LSP in detail and I do understand why strenghtening preconditions violates the principle (using the example from http://www.ckode.dk/programming/solid-principles-part-3-liskovs-substitution-principle/#contravariance):

            ...

            ANSWER

            Answered 2018-Jan-18 at 20:30

            If you weaken a precondition, the subtype is still compatible with places expecting the supertype. It may not throw an exception where the base class does normally, but that is okay, because throwing less exceptions should not break the consuming code. If the calling code is built around the assumption that exceptions get thrown in certain places, and uses that for primary control flow of the application, the consuming code should probably be rewritten.

            Also, I think your second code sample is wrong.

            If the preconditions of the base class really must be enforced all the time, a better implementation would be to create a data type that encapsulates those rules, and pass that as a parameter. That way it is not in the hand of subclasses, it is part of the new class's constructor.

            Ex:

            Source https://stackoverflow.com/questions/48329231

            Community Discussions, Code Snippets contain sources that include Stack Exchange Network

            Vulnerabilities

            No vulnerabilities reported

            Install SOLID-principles

            You can download it from GitHub.

            Support

            For any new features, suggestions and bugs create an issue on GitHub. If you have any questions check and ask questions on community page Stack Overflow .
            Find more information at:

            Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items

            Find more libraries
            CLONE
          • HTTPS

            https://github.com/harrymt/SOLID-principles.git

          • CLI

            gh repo clone harrymt/SOLID-principles

          • sshUrl

            git@github.com:harrymt/SOLID-principles.git

          • Stay Updated

            Subscribe to our newsletter for trending solutions and developer bootcamps

            Agree to Sign up and Terms & Conditions

            Share this Page

            share link