Before going through this post, I suggest you go through my previous post for learning the basic concepts of Dependency Injection and Inversion of Control.
Demo on product
This demo is based on a traditional implementation approach. We have a Product class and it has a method which takes the discount and based on some business logic, it applies the discount to the product. The ProductService will interact with the database using ProductRepository. If you notice, the ProductService has a dependency on 2 concrete classes, i.e., Discount and ProductRepository.
- new Discount();
- new ProductRepository();
We can hence say that ProductService is tightly coupled because of these 2 dependencies.
We may require using different discounts based on several factors such as Product type, some festival, or any other factor. Based on different discounts, we may need to change ProductService class also. In future, you may change the database from SQL to Oracle. Again, it may require ProductService changes, which is the violation of SOLID principles. Just go through the implementation below. I will solve the issues discussed here, in the below section.
- using System;
- using System.Collections.Generic;
- namespace DependencyInjection {
- class Program {}
-
-
-
- public class Product {
- string Title;
- string SmallImage;
- double Price;
- public void PriceAdjustment(Discount discount) {
-
- }
- }
-
-
-
- public class Discount {
-
-
-
- public string CampaignId {
- get;
- set;
- }
-
-
-
- public string Description {
- get;
- set;
- }
-
-
-
- public virtual decimal DiscountAmount {
- get;
- set;
- }
-
-
-
- public virtual DateTime ExpirationDate {
- get;
- set;
- }
-
-
-
- public string AdjustmentType {
- get;
- set;
- }
-
-
-
- public string AdjustmentSubType {
- get;
- set;
- }
-
-
-
- public string CouponCode {
- get;
- set;
- }
- }
- public class ProductRepository {
- public IEnumerable < Product > GetAllProducts() {
- return new List < Product > ();
- }
- }
- public class ProductService {
- private Discount _discount;
- private ProductRepository _productRepository;
- public ProductService() {
-
- _discount = new Discount();
- _productRepository = new ProductRepository();
- }
- public IEnumerable < Product > GetProducts() {
- IEnumerable < Product > allProducts = _productRepository.GetAllProducts();
- foreach(Product p in allProducts) {
- p.PriceAdjustment(_discount);
- }
- return allProducts;
- }
- }
- }
Solution to the above discussed issues : DI (introduce abstraction)
Create an interface for Discount (IDiscount), and an interface for Repository (IProductRepository).
-
-
-
- public interface IDiscount {
-
-
-
- string CampaignId {
- get;
- set;
- }
-
-
-
- string Description {
- get;
- set;
- }
-
-
-
- decimal DiscountAmount {
- get;
- set;
- }
-
-
-
- DateTime ExpirationDate {
- get;
- set;
- }
-
-
-
- string AdjustmentType {
- get;
- set;
- }
-
-
-
- string AdjustmentSubType {
- get;
- set;
- }
-
-
-
- string CouponCode {
- get;
- set;
- }
- }
- public interface IProductRepository {
- IEnumerable < Product > GetAllProducts();
- }
- public class ProductService {
- private Discount _discount;
-
- private IProductRepository _productRepository;
Modify ProductService accordingly.
- public class ProductService {
-
- private IDiscount _discount;
-
- private IProductRepository _productRepository;
- public ProductService(IProductRepository productRepository) {
-
- _discount = new Discount();
-
- _productRepository = productRepository;
- }
- public IEnumerable < Product > GetProducts() {
- IEnumerable < Product > allProducts = _productRepository.GetAllProducts();
- foreach(Product p in allProducts) {
- p.PriceAdjustment(_discount);
- }
- return allProducts;
- }
- }
Notice that, after the above changes, PriceAdjustment will break; hence we would modify this also accordingly. Now, PriceAdjustment can work with any kind of Discount that implements IDiscount.
- public class Product {
- string Title;
- string SmallImage;
- double Price;
- public void PriceAdjustment(IDiscount discount) {
-
- }
- }
We have injected dependency through the constructor.
Discount dependency is still making the ProductService tightly coupled. Let's refine the ProductService class.
- public class ProductService {
-
- private IDiscount _discount;
-
- private IProductRepository _productRepository;
- public ProductService(IProductRepository productRepository) {
-
-
-
- _productRepository = productRepository;
- }
-
- public IEnumerable < Product > GetProducts(IDiscount discount) {
- IEnumerable < Product > allProducts = _productRepository.GetAllProducts();
- foreach(Product p in allProducts) {
- p.PriceAdjustment(discount);
- }
- return allProducts;
- }
- }
To be continued......