Component Library

What better way to start a blog then to start with the beginnings of any .Net project and the fundamental building blocks of a good project. The Component Library.

I will start with a pretty cool component I wrote a few years back, and slowly start to build the component library to something useful, which can be plugged into and application to produce RAD projects that are consistent and maintainable.

In my view a component library needs to be 100% generic. It needs to be very simple to deploy
Step1 - Add Project Reference.
and the functionality needs to be simple enough not to warrant months of study, and experimentation.

To that end, all my components are relatively simple. Sometimes even overkill, but hopefully I will be able to convince you otherwise.

The first part to my component Library is the MailParser. In a few of the project I have worked on, there was a need to sent automated emails via some template driven device. The norm is to write your mail templates with some sort of placeholder which can be replaced at runtime.

Dear [surname]

Here is my example mail template.

Regards
[Departmentname]


In this case we use [surname] and [Departmentname] and do a replace on the string with the current users surname and managers department name.

This method is ok. but what if you instead wanted to change the surname to first name? well, we'll need to re-write the code and re-compile to incorporate this new request.

This seems quite a small change, but what if you have other changes in the code which are still being tested? what if someone else has written code that need to undergo testing that you don't know about? I could go on, but I'm guessing you get the idea.

The second reason this is not such a good idea, is because it is creating a strong relationship between your string text and your code. This is probably the best reason not to use this method, it's kind of like flying a plane with a blindfold on. If someone new comes on board and doesn't know that [airplane] is a "Special" word which was taken out of the string, but not from the binding then they could potentially be adding some dynamic element which causes problems down the track. You laugh now, but trust me, people are lazy and forgetful and things like this always fall through the cracks until they reappear and nobody knows why this has happened and how long it has been happening.

Instead of the code binding to the string value, we bind the string value to the object model. Object models are typically pretty static once finalised and have a fairly structured nature to them. In comes reflection. What a wonderful concept, code that analyzes code and OOD, Object Oriented design.

so if we look at the example above again, but this time put our reflective goggles on see how different it looks.

Dear %CurrentUser.Surname%

Here is my example mail template.

Regards
%CurrentUser.Manager.Department.Name%


All of a sudden, we can manipulate this template to say anything we want, and if our object are designed properly, the limitations are practically zero. We don't have to rebuild the assembly, and the templates are easily understandable and readable.

The basic concept of this object is that a new instance is created passing the objects you want to bind to the string. Then you call ParseText passing in the template string value, and back comes your bound string. easy.

download the project and try it yourself.
Component.Library.zip

Comments

Popular posts from this blog

What good looks like!!

A microservice journey - part 2: what type of micro service are you?

Validation Rules