Code Monkey home page Code Monkey logo

codesamples-1's Introduction

Sample code for Dependency Injection: Principles, Practices, and Patterns

This repository contains sample code for the book Dependency Injection Principles, Practices, and Patterns. You can find the code shown in the book in this repository, embedded in functioning sample applications.

Required components

All solutions are built using Visual Studio 2017 on .NET Core, although you should be able to use the solutions with Visual Studio 2019 and Visual Studio Code on Mac and Linux as well.

Some of the solutions themselves use various open source DI Containers to compile and run:

Database setup

In contrast to the book's code listings, the sample applications make use of SQLite, because they allow the application to run without having to setup SQL Server database locally. The SQLite databases are included in this download. You should be able to run the sample applications as-is.

If you wish to use SQL Server instead, the applications require that their databases are set up correctly. Each application requiring a database have associated T-SQL scripts.

After setting up the database, you need to add the connection string to the applicationsettings.json files of the applications, and change the CommerceContext to use SQL Server instead of SQLite.

Included code samples

The following code samples are included in this repository:

  • HelloDI: The "Hello DI" example of section 1.2 giving a first example of DI.
  • ElectricalAppliances: In section 1.1.2 "Understanding the purpose of DI," electrical wiring is compared to software design patterns, building analogies for Null Object, Decorator, Composite, and Adapter design patterns. This is done without showing any code. The ElectricalAppliances solution shows how this would map to code.
  • MarysECommerce: The "Mary's E-Commerce" example that runs through chapter 2, which demonstrates a tightly coupled web application that doesn't apply DI.
  • RightECommerce: The "Right E-Commerce" example that runs through chapter 3, which demonstrates how to create a loosely coupled code base using DI. This solution also includes:
    • The "Currency Convertions" example of section 4.2.4, demonstrating Constructor Injection.
    • The "Update Currency" example of section 7.1.1, demonstrating how to compose console applications.
    • The "Currency Monitoring" example of section 8.3.3, demonstrating both Scoped Lifestyle using a console application.
  • UWPProductManagementClient: The "Product-management rich client" example of section 7.2, demonstrating how to compose UWP applications. Note that WPF composition is very similar to UWP. This solution includes:
    • The "Circuit Breaker" example of section 9.2.1.
  • Greeter: The "Greeter" example of section 9.1.1, demonstrating the Decorator design pattern.
  • AopECommerce: The "SOLID as driver for AOP" example of chapter 10, which demonstrates how SOLID design can be applied to achieve Aspect-Oriented Programming. This solution also includes:
    • The "Refactoring from Constructor Over-Injection to domain events" example of section 6.1.3.
    • An example of wiring the AOP example using Pure DI (the Commerce.Web.PureDI project).
    • An example of wiring the AOP example using Pure DI (the Commerce.Web.PureDI project).
    • An example of wiring the AOP example using Autofac as discussed in chapter 13 (see the Commerce.Web.Autofac project).
    • An example of wiring the AOP example using Simple Injector as discussed in chapter 14 (the Commerce.Web.SimpleInjector project).
    • An example of wiring the AOP example using Microsoft.Extensions.DependencyInjection as discussed in chapter 14 (the Commerce.Web.MS.DI project).

Contributions

All code is released as open source, and we do take pull requests. We've left blank some implementation code not central to the topic of dependency injection, or at least in a state that could be improved.

There's also fewer unit tests than we'd like.

You can help improve the code, but with one important limitation:

The code that appears in the book musn't be changed.

While an open source code base is a dynamic entity, the printed book can't be changed, and even the eBooks are unlikely to see drastic changes. We'll do our best to mark the parts of the code that mustn't be changed.

For the rest of the code base, we take pull requests, but please follow these tips for good pull requests.

(Last updated 2020.05.02 with the list of included examples)

codesamples-1's People

Contributors

dotnetjunkie avatar ploeh avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.