SERVICES, INTERFACES & SEPARATING ASSEMBLIES. One thing you realise the more you write code is that the less “moving parts” you have the better or to put it another way the more you put your moving parts into one contained place the better. Pushing items which change or where there will be change in the future to places you can manage is really useful.
Interfaces help a lot here and if you can talk interfaces and just pass the implementation of what you want to do to specific areas, you code is more maintainable.
The following is a brief tutorial in pushing the return of a string depending upon a value in a settings file. This way you can change certain aspects of your code base from a simple text switch.
We would like to return a SQL String but the content will vary depending on which area in the UK you are from (England, Scotland or Wales).
We want to push the logic for this into a separate core assembly which manages all the business.
In this case we want the application to know that we want to return a string which will contain a SQLQuery but we are not worried about what the content will be until it’s implementation at runtime. Interfaces define the core of the app and are visible by everything
Step 2 – Create your logic
Next we describe the implementation of Interfaces by inheriting them in the classes where we want to do the work in this case it will be English, Scottish or Wales specific query strings.
Example of the English Class which implements the query above.
Step 3 – Create your Service Layer
The service layer will contain 2 components and abstracts out connection between the Main Application and the Business. This useful in that in the future we can change Layers but because the interfaces stay the same, we won’t break much by doing so.
1. Where we resolve our dependency.
This is where we plumb in exactly which class in the logic layer we wish to instantiate. In this case we take text from the settings file which will either say “England”, “Scotland” or “Wales” and from this inform the complier which object, which implements the Interface, we need for this run.
2. We use dependency injection to pass the ISqlQueryable Object to return the String.
Step 4 – Plumbing it together in the Main.
For this console app, we first have to get the object we need which will implement the Interface. We then pass this Interface to the service to return the string we require.
You can find this example on my git repo
Hope this will help loosen up your code up. Having this here helps me too as seeing patients take up a lot of my time and I always need a reference to jog my memory of what I know now but frequently forget!