Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Friday, May 30
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Architecture»Understanding Dependency Inversion Principle with C#

    Understanding Dependency Inversion Principle with C#

    Dhanik Lal SahniBy Dhanik Lal SahniMay 2, 2018No Comments5 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Understanding Dependency Inversion Principle with C#
    Share
    Facebook Twitter LinkedIn Pinterest Email

    According to Wikipedia the Dependency Inversion Principle (popularized by Robert Martin) states that:

    • High Level Modules should not be depend upon low level modules. Both should depend upon abstractions.
    • Abstraction should not depend upon details. Details should depend upon abstraction.

    In application architecture, UI is always on top level. All request goes from UI to business layer and then database layer. Database layer connects with database and fetch required data.
     
    When we define Dependency Inversion Principle in above architectute, The presentation layer defines the abstractions it needs to interact with an business layer and the business layer defines the abstractions it needs from a data access layer. The higher layer defines the abstractions and lower layers implement those abstractions.

    The second part of the principle stating that abstractions should not depend upon details rather details should depend upon abstractions. It means that if the details change they should not affect the abstraction. Application should keep working as it was working earlier.

    Let us take, we have account opening module where notification is sent after opening an account. A printed letter is sent to customer using Notification class.

    Here is some model classes for account opening module.

        public class Customer
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public DateTime DateOfBorth { get; set; }
            public Contact Contact { get; set; }
            public Address Address { get; set; }
        }
    
        public class Address
        {
            public string Address1 { get; set; }
            public string Address2 { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Zip { get; set; }
        }
    
        public class Contact
        {
            public string Email{ get; set; }
            public string Fax { get; set; }
        }
    

    Notification class which send letter to customer.

        public class Notification
        {
            public Notification()
            {
            }
            public bool SendNotification(Customer customer)
            {
                return Send(customer.Address);
            }
            private bool Send(Address address)
            {
                //Letter Print Logic 
                return true;
            }
        }
    
        public class Account
        {
            public void OpenAccount()
            {
                Customer customer = new Customer()
                {
                    FirstName = "Dhanik",
                    LastName = "Sahni",
                    DateOfBorth = new DateTime(1981, 07, 05),
                    Address = new Address
                    {
                        Address1 = "SVF",
                        Address2 = "Lal Kuan",
                        City = "Ghaziabad",
                        State = "UP",
                        Zip = "201002"
                    },
                    Contact = new Contact
                    {
                        Email = "dhanik.sahni@yahoo.com",
                        Fax = "3234234242"
                    }
                };
                //concrete  Object 
                Notification notification = new Notification();
                notification.SendNotification(customer);
            }
        }
    

    This code does not have issue at all. Now we have high volume of customer and sending printed letter to each of them is difficult and wast of paper also in some case. Many customer even not opening letter also. So instead of sending printed notification, company decide to send email notification. Now we face problem in our code. Let us problems

    • We have to change code of Account class. As per SRP (Single Responsibility Principle), we should change account class when there will be change required in Account bahaviour. But in case we have to change due to notification, which is wrong.
    • We are using concrete object. We have to change it to abstraction to make code more maintainable. Code should not create object of other dependent object instead that should be passed from caller.
    • There is Interdependence of the modules. Ideally modules should not be interdependent.
    • Changing in existing code may create problem to other parts which is already tested.
    • Account class should not dependent on Notification class. We can overcome this issue by passing object at runtime.

    Let us do changes as per DIP principle. We have to create interface for sending notification.

        public interface INotificationSender
        {
            bool SendNotification(Customer customer);
        }
    

    To classes, EmailNotification and LetterNotification is implementing interface INotificationSender. Later on, if any other way of notification is required, we have to simple implement that interface and pass to account class.

        public class EmailNotification : INotificationSender
        {
            public bool SendNotification(Customer customer)
            {
                return Send(customer.Contact);
            }
            private bool Send(Contact contact)
            {
                //Send Email Logic
                return true;
            }
        }
    
        public class LetterNotification : INotificationSender
        {
            public bool SendNotification(Customer customer)
            {
                return Send(customer.Address);
            }
            private bool Send(Address address)
            {
                //Letter Print Logic 
                return true;
            }
        }
    

    Now we will pass notification object through construction injection to account class.

        public class Account
        {
            INotificationSender emailSender = null;
            public Account(INotificationSender sender)
            {
                //object is passed through constructor
                emailSender = sender;
            }
            public void OpenAccount()
            {
                Customer customer = new Customer()
                {
                    FirstName = "Dhanik",
                    LastName = "Sahni",
                    DateOfBorth = new DateTime(1981, 07, 05),
                    Address = new Address
                    {
                        Address1 = "SVF",
                        Address2 = "Lal Kuan",
                        City = "Ghaziabad",
                        State = "UP",
                        Zip = "201002"
                    },
                    Contact = new Contact
                    {
                        Email = "dhanik.sahni@yahoo.com",
                        Fax = "3234234242"
                    }
                };
                //abstract Object 
                emailSender.SendNotification(customer);
            }
        }
    

    We can pass any notification class at run time. That’s way to create maintainble code.

    Summary

    Dependency Inversion Principle, give control to pass object to caller object. That help in loosely coupled code. We can change abstraction at runtime based on condition. This also help us in cleaner and managable code.

    Complete code sample is available here.

    architecture design principle solid
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleUnderstanding Single Responsibility Pattern – Problem and It’s Solution
    Next Article Understanding Open Closed Principle
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni17 Mins Read

    How to Elevate Your Career to Salesforce Architect

    September 8, 2024
    By Dhanik Lal Sahni8 Mins Read

    Understanding the Salesforce Well-Architected Framework to Enhance Business Outcome

    August 25, 2024
    By Dhanik Lal Sahni8 Mins Read

    Streamlining Authentication: Custom Login Flow in Salesforce

    June 2, 2024
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.