Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • 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
    • Enhancing Performance with File Compression in Apex
    • Salesforce Spring ’25 Release: Top Flow Enhancements You Need to Know
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Saturday, May 10
    • 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 Single Responsibility Pattern – Problem and It’s Solution

    Understanding Single Responsibility Pattern – Problem and It’s Solution

    Dhanik Lal SahniBy Dhanik Lal SahniMay 2, 2018No Comments6 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Understanding Single Responsibility Pattern – Problem and It’s Solution
    Share
    Facebook Twitter LinkedIn Pinterest Email

    SRP states, “An object should have only one reason to change”. If an object has more than one reason to change then it has more than one responsibility and is in violation of SRP. An object should have one and only one reason to change.

    Let us take example of below code, where XML file with product information is parsed and displayed on screen.

        FileDialogReader.Filter = "XML Document (*.xml)|*.xml|All Files (*.*)|*.*";
        var result = FileDialogReader.ShowDialog();
        if (result == DialogResult.OK)
        {
            txtFileName.Text = FileDialogReader.FileName;
            lstProduct.Items.Clear();
            var fileName = txtFileName.Text;
            using (var fs = new FileStream(fileName, FileMode.Open))
            {
                var reader = XmlReader.Create(fs);
                while (reader.Read())
                {
                    if (reader.Name != "product") continue;
                    var id = reader.GetAttribute("id");
                    var name = reader.GetAttribute("name");
                    var unitPrice = reader.GetAttribute("unitPrice");
                    var discontinued = reader.GetAttribute("discontinued");
                    var item = new ListViewItem(
                    new string[] { id, name, unitPrice, discontinued });
                    lstProduct.Items.Add(item);
                }
            }
        }
    

    Let us refactor this code step by step.

    Step 1. Defining a Model

    Since we are importing product from XML File, we should introdue a entity model Product. By carefully analyzing above code we can create below model class.

        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public decimal UnitPrice { get; set; }
            public bool Discontinued { get; set; }
        }
    

    Step 2: Extracting the Parsing into a repository

    We also can recognize a distinct concern of loading a list of products from a data source. In this case the data source is a XML document. It will make sense to have a specific component concerned with loading a list of products from a data source and return a list of instances of type Product.

        public interface IProductRepository
        {
            IEnumerable GetByFileName(string fileName);
        }
    

    The repository is responsible to load, parse and map the XML document and return a list of Product items to the caller. Based on above steps our code will be like below

        private IProductRepository repository=null;
        private void btnLoadFile_Click(object sender, EventArgs e)
        {
            FileDialogReader.Filter = "XML Document (*.xml)|*.xml|All Files (*.*)|*.*";
            var result = FileDialogReader.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtFileName.Text = FileDialogReader.FileName;
                lstProduct.Items.Clear();
                var fileName = txtFileName.Text;
                lstProduct.Items.Clear();
                var products = repository.GetByFileName(fileName);
                foreach (Product product in products)
                {
                    var item = new ListViewItem(new[]
                    {
                         product.Id.ToString(),
                         product.Name,
                         product.UnitPrice.ToString(),
                         product.Discontinued.ToString()
                     });
                    lstProduct.Items.Add(item);
                }
            }
        }
    

    Above code is better than before, but still it does not look complete refactored.

    Above code is written in View and view should not have logic. View should only focused on presenting data and delegating requests to controller or presenter. So we have to seprate each responsibility or concern. we introduce a pattern which separates the concerns of a) visualization, b) orchestration and c) (data-) model. A pattern that perfectly fits our needs is the Model-View-Presenter pattern (MVP). The presenter is the component that orchestrates the interactions between model, view and (external) services. In this pattern the presenter is in command.

    Based on this, view should

    • delegate the user’s request to choose an XML document to the presenter
    • delegate the user’s request to load the data from the selected XML document to the presenter
    • provide the name of the selected XML document to the presenter
    • accept a file name (of a selected XML document) from the presenter
    • display a given list of products provided by the presenter (in a ListView control)

    Let us create each concern classes.

    ProductRepository class is responsible for loading file and converting each record into entity list. This class uses IFileLoader and IProductMapper to load and map record into entity.

      public class ProductRepository : IProductRepository
        {
            private readonly IFileLoader loader;
            private readonly IProductMapper mapper;
            public ProductRepository()
            {
                loader = new FileLoader();
                mapper = new ProductMapper();
            }
            public IEnumerable GetByFileName(string fileName)
            {
                var products = new List();
                using (Stream input = loader.Load(fileName))
                {
                    var reader = XmlReader.Create(input);
                    while (reader.Read())
                    {
                        if (reader.Name != "product") continue;
                        var product = mapper.Map(reader);
                        products.Add(product);
                    }
                }
                return products;
            }
        }
    

    FileLoader is resposible to load XML file.

        public interface IFileLoader
        {
            Stream Load(string fileName);
        }
    
        public class FileLoader : IFileLoader
        {
            public Stream Load(string fileName)
            {
                return new FileStream(fileName, FileMode.Open);
            }
        }
    

    IProductMapper is resposible to map XML element value into entity field. This will return list of products after parsing loaded XML File.

        public interface IProductMapper
        {
            Product Map(XmlReader reader);
        }
    
        public class ProductMapper : IProductMapper
        {
            public Product Map(XmlReader reader)
            {
                if (reader == null)
                    throw new ArgumentNullException("XML reader used when mapping cannot be null.");
                if (reader.Name != "product")
                    throw new InvalidOperationException("XML reader is not on a product fragment.");
                var product = new Product();
                product.Id = int.Parse(reader.GetAttribute("id"));
                product.Name = reader.GetAttribute("name");
                product.UnitPrice = decimal.Parse(reader.GetAttribute("unitPrice"));
                product.Discontinued = bool.Parse(reader.GetAttribute("discontinued"));
                return product;
            }
        }
    

    ViewForm is view component.

        public interface IProductView
        {
            void Initialize(ProductPresenter presenter);
            string GetFileName();
            void ShowProducts(IEnumerable products);
            void SetFileName(string fileName);
        }
    
        public partial class ViewForm : Form, IProductView
        {
            public ViewForm()
            {
                InitializeComponent();
            }
            private ProductPresenter presenter;
            private void btnBrowse_Click(object sender, EventArgs e)
            {
                presenter.BrowseForFileName();
            }
            private void btnLoad_Click(object sender, EventArgs e)
            {
                presenter.GetProducts();
            }
            public void ShowProducts(IEnumerable products)
            {
                lstView.Items.Clear();
                foreach (Product product in products)
                {
                    var item = new ListViewItem(new[]
                    {
                         product.Id.ToString(),
                         product.Name,
                         product.UnitPrice.ToString(),
                         product.Discontinued.ToString()
                     });
                    lstView.Items.Add(item);
                }
            }
            public string GetFileName()
            {
                return txtFileName.Text;
            }
            public void SetFileName(string fileName)
            {
                txtFileName.Text = fileName;
                btnLoad.Enabled = true;
            }
            public void Initialize(ProductPresenter _presenter)
            {
                presenter = _presenter;
            }
        }
    

    ProductPresenter is presenter and responsible to manage above component.

        public class ProductPresenter
        {
            private readonly OpenFileDialog openFileDialog;
            private readonly IProductRepository repository;
            private readonly IProductView view;
            public ProductPresenter()
            {
                view = new ViewForm();
                view.Initialize(this);
                repository = new ProductRepository();
                openFileDialog = new OpenFileDialog();
            }
            public IProductView View
            {
                get { return view; }
            }
            public void BrowseForFileName()
            {
                openFileDialog.Filter = "XML Document (*.xml)|*.xml|All Files (*.*)|*.*";
                var result = openFileDialog.ShowDialog();
                if (result == DialogResult.OK)
                    view.SetFileName(openFileDialog.FileName);
            }
            public void GetProducts()
            {
                BrowseForFileName();
                var products = repository.GetByFileName(view.GetFileName());
                view.ShowProducts(products);
            }
        }
    

    Below code need to put in Main method to run application.

        var presenter = new ProductPresenter();
        Application.Run((Form)presenter.View);
    

    These above classes are having very less code and each are responsible for one responsibility. In real application, there are complex structure and for that SRP is very important. With the aid of the SRP, a complex problem can be reduced to many small sub-problems which are easy to solve in isolation. Just remember the sample of the Roman empire I gave you in the introduction of this post.

    Complete Source code can be downloaded from here.

    architecture design principle solid
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleUnderstanding SOLID Design Principles using real objects
    Next Article Understanding Dependency Inversion Principle with C#
    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
    • 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
    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 (110) 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) custom metadata types (5) design principle (9) file upload (3) flow (14) 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 (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (139) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce GraphQL API (3) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) shopify api (3) 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
    • 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
    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 (110) 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) custom metadata types (5) design principle (9) file upload (3) flow (14) 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 (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (139) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce GraphQL API (3) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) shopify api (3) 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.