1: Software architecture with real time scenario – Solution for Customer and logger class

1: Software architecture with real time scenario – Solution for Customer and logger class

Before we go ahead and code the solution Lets do a 5 minute analysis of how to approach this solution As we said in a problem definition that we wold like to add new login functionality without disturbing the customer class That’s our end goal Currently the login functionality is completely entangle into the customer class. In other words Customer class is violating SRP principle i.e Single Responsibility Principle SRP says that the class or a module or a code should have only one and one reason to change For example Our customer class should only change when there is some kind of validation changes in the customer last name or probably in the customer first name Other than that the customer class should not change But currently because login functionality is a part of this customer class Whenever you change this login functionality. You have to also compile the customer class In other words Your customer class is doing work which he is not suppose to do The solution is pretty simple As we said the customer class is not following the SRP principle i.e the Single Responsibility Principle. In other words it is doing too many things If some how we move the error handling routine to some other class or some other project Then whenever we add any kind of class into that project We don’t have to change the customer project In other words we have to move the error handling routine completely away form the customer class Here’s how the solution would look like. Currently our solution is three tier architecture. We have the user interface. We have the middle tier then we have the data access layer Our solution would be that we are going to create one more project here called as a exception project and this exception project will be referred in the middle tier Whenever we want to add a new exception logic or exception class what we can do is we can add this class over here And we don’t need to change in the middle tier Lets code this solution Lets go ahead and add one more project here called as exceptions Add a new project This will be basically our class library Whenever we want to add new exception logics we are going to add over here Lets move the exception logic i.e a log error function logic to the exception project Let me go to the exception project Because this logic is related to login the exception in the file Give a name to this saying File Exception We have used StreamWritter over here We need to ensure that we import system.IO namespace that error has gone off We also have some error here because this exception is a reserved namespace and our project is also named like exception Let say this as exception Rename everyone to exception namespace I can use here exeception We have created a simple file exception. We have moved the logic from the business object into this file exception class We are going to get some couple of errors here The first error is that currently in the business object there is no log error exception the log error function let me remove this Build this There is no log error function We can reference this exception project into a business object Add a reference to the exception project class project The exception project is now added to the business object Use our exception namespace Wherever we want to log the errors we can create the object of the exception class. The file exception class FileException obj=new FileException obj. I think we have made the function as private make it public obj.LogError I can make this as a class level variable. I don’t need to instantiate again and again It is not accessible outside the customer class Wherever I want to log the error I can say obj.LogError build this We have moved the exception logic into a different project all together If I want to add event viewer error handling routine Take this Control+C Control+V and Event Viewer Exception I can write my logic for adding errors in to event viewers We have solved the problem partially but not fully Our main goal was whenever we add a new class here we don’t want to make any kind of changes in the customer object or in the middle tier When I add a new class like event viewer exception I have to create a reference of the event viewer exception over here In other words if I add a new class I have to go and create the object over here Again I have to change in the middle tier or in my business object If you watch closely the customer class The change in customer class is happening because you are directly referencing the class names here Because you are referencing the file exception Tomorrow when you add a new class over there. For example the event viewer exception you have to again refer the same in the customer class If somehow we can get rid of this direct names of the classes here we are in better position In other words we need to refer a interface We have to some how get rid of this exception class which is the exception project which is nothing but this project We have to get rid of this references what is happening in the BO and the business object and the middle tier will refer this exception class via a interface If we can replace this concrete classes by some generic interfaces that would solve our problem First add a simple interface called as IExceptions Create a simple function here called as LogError exceptrion Lets reference this interface in our exception class and implement this interface in all our classes all our exception classes Reference this interface in my exception project here import this IException and let me implement IEx in all of my exception classes Now in our business object i.e in our middle tier will remove this concrete class reference and will add a reference to IException now we will only have a reference through IException We will only have reference to our IEx interface which is been implemented by all of our exception classes This IEx interface is a generic interface. Tomorrow if you add any kind of new classes over here you don’t need to worry anything about it in your customer classes. In other words any kind of new changes or any kind of new class you add into your exception namespace You don’t need to change in your customer class The next question would be this is a interface How do we create the object of the interface In order to create the object we will use a factory pattern create a new project called as Factory This factory class will reference both the exception class as well as the interface He will refer both the interface as well as the concrete exception classes and in this factory class we have This factory class will return back the objects the strong type object. For example we can create a simple Create Exception function which will accept integer values We will say if i==0 then return back new cls new FileException else return back new Event ViewerException The factory class takes all the trouble of creating the objects But he always returns back a very generic interface In our business object in our middle tier we will use the factory here to create the exception objects In other words this business object will now refer the factory class to puchout objects Let me refer the factory class here using factory Let me make this as static If I give 0 it will give a file exception If I give 1 it will give a event viewer exception Tomorrow if I add more new classes I need to add over here and add the exception class into the exception project I don’t need to make any kind of change into my customer class If I want to create the file exception I’ll pass 0 here This value 0, 1, 2, 3 fetch from your web config file In your web config file you can make a entry like this Excpetionkey or something then you can read those values over here In other words This is a integer values and it will convert into a int Build this project first If you want to add a new exception you will change only in two places one is the exception project itself Add a new class here EmailException logic to send a email then you will return Go to your factory class and you will say if somebody passes me else if i==1 EventViewer and anything other than 0 and 1 are return new EmailException You have changed only in two places one is the factory class and the other one is in your concrete class In a web config file you can easily use EmailException you don’t have to do anything other than this If we rebuild this project If you run this There is my project running Currently there is a exception I have not created any database so that’s why this data access leyer logic is failing over here I have not debug through that It has actually invoke the the exception class You can see because we have provided the value as 2 here it is now using the EmailException Initially we had a three tier architecture we had the business object We had the UI and we had the data access layer The problem was that the logging error logging utility or the error logging logic was in the business object so we created a different project all together called as exception project There was a problem this exception project go directly referencing the business object We provided a interface in between Again we had problem How do we create a object of the interface For that we used the factory class In other words we started with the problem of the SRP violation where the customer class was doing lot of work then by using factory patterns and interfaces we came out with the complete solution where the exception logic is completely decoupled into a different project all together and the customer project is completely into a different project all together Now the customer project has only one reason to change or the customer class has only one reason to change when there is a change in the customer code property or the first name property or the last name property while the exception logging utility has only one logic to change when you add a new exception logic In this way by using and following SRP principle we have completely decoupled both of these classes When you add When you change logic in one project or in one class it doesn’t affect the other class Thank you very much

6 thoughts on “1: Software architecture with real time scenario – Solution for Customer and logger class

  1. Great video to point out the concepts. The naming of the LoggingManagers and Interface could have been better. Calling them exceptions when they are not exceptions could be misleading. ie: Is there any .net framework type that ends in exception that is not an Exception?

  2. No, He should not do that. Because, He is trying to create a new object. The factory pattern is falling into creational. It will hide to complexity of creating objects. Hence, He is correct

  3. I have a question, instead of using the web.config to specify the value, would it be ok to use an Enum when instantiating the exception object with the static function?  The use of unknown variable numbers isn't very self documenting but I want to make sure I'm not creating more coupling by using an Enum.  Thank you for your example.

Leave a Reply

Your email address will not be published. Required fields are marked *