0

I have a requirement where system sends email. Currently the system sends user's first name in email and it's in production and working fine.

Now my client asked to add last name as well in email, so I extended the send(User userinfo) method and this is also working fine.

Now client again asked to add email in email, later on again client asked to add mobile and so on more user's information in email.

How to manage this with open-closed principle of solid principle while client is frequently asking for changes in the same feature? Code

` class User
    {
        public int userId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Mobile { get; set; }
    }

    interface Email
    {
        void send(User userInfo);
    }

    class UserEmail : Email
    {
        public void send(User userInfo)
        {
            // Sends users firstname
            //Email Send code 
        }
    }

    class NewUserEmail : Email
    {
        public void send(User userInfo)
        {
            // Sends users Lirstname + Lastname
            //Email Send code 
        }
    }
`
chrisis
  • 1,983
  • 5
  • 20
  • 17
  • When client ask to change the email feature the old feature should be working or the old feature becomes obsolete? – Chetan Apr 18 '20 at 08:34
  • Sending an email is the main requirement, and the mail template and what goes inside the placeholders as dynamic content is a separate concern. Review your design. If you had a design like "get template / fill template (dictionary) / send mail", you would just change your configuration, or the client would just add the last name from a back-office – Oguz Ozgul Apr 18 '20 at 09:28
  • @ChetanRanpariya client wants to obsolete the feature. – Anrorathod Apr 18 '20 at 10:02
  • @All, this is an interview question. – Anrorathod Apr 18 '20 at 10:03

1 Answers1

0

The content creation need not be the responsibility of send method. It can be like send any content that is passed to it. So instead of Email interface, you can have IEMailContent interface. The implementation of this interface shall handle mail content preparation. This can be extended as and when content requirements change. Open for extension.

The send method of Email shall have IEMailContent as an argument. Then it would not require change when content requirements change. Closed for modification.

send method can have other arguments like subject, to mail id list, cc mail id list, bcc id list with the default values. Then if any requirement change in these parameters, Email class need not be modified.

Chandru
  • 9
  • 2
  • Hi Chandru, I understand, now lets change the statement, if client want to add new fields in Product table by today, after one week another field, after one week another field and so on for the year. so how to manage this database structure and code as well. – Anrorathod Apr 18 '20 at 10:59
  • This question is more about Requirements Analysis and Data Design: this link might help [link](https://stackoverflow.com/questions/4822753/what-is-the-best-way-to-allow-a-user-to-add-columns-to-their-database-on-the-fly) – Chandru Apr 18 '20 at 14:03