When I think of design, I think of simplicity. If it’s easy to use the API then the design is good. However, if using the API result with repeatedly scenario in which a common code is written. This should return you to redesign the entire API.
For example the Cryptography in the .NET framework. Cryptography means either to Encrypt/Decrypt or to Hash/ValidateHash. It should not be more complicated then that. EntLib cryptography’s API is a much better one. Moreover, to make use the .net framework cryptography in different project you always have to write some more code. Which again return to my point…a design Err.
To better demonstrate what I mean, here is a small example about Binding
Binding binding = MyControl.DataBindings[“Text”];
This is what the .net framework gives us as developer. In almost every Binding scenario I worked with, this was not enough. Projects I worked on usually need to do the binding again when the datasource changes. This result in a common code like :
Binding binding = control.DataBindings[controlProperty];
if (binding != null)
control.DataBindings.Remove(binding);
Which means we can simply add a helper class that encapsulate this code to a better API.
public
sealed class DataBinding
{
public static void Bind( Control control,string controlProperty,object dataSource,string sourceProperty)
{
Bind(control,controlProperty,dataSource,sourceProperty,null,null);
}
private static Binding ReBind(Control control,string controlProperty,object dataSource,string sourceProperty)
{
Binding binding = control.DataBindings[controlProperty];
if (binding != null)
control.DataBindings.Remove(binding);
binding = new Binding(controlProperty,dataSource,sourceProperty);
return binding;
}
public static void Bind(Control control, string controlProperty, object dataSource,string sourceProperty, ConvertEventHandler format, ConvertEventHandler parse)
{
try
{
Binding binding = ReBind(control,controlProperty,dataSource,sourceProperty);
if (format != null)
binding.Format+=format;
if (parse != null)
binding.Parse+=parse;
control.DataBindings.Add(binding);
}
catch(Exception e)
{
throw e;
}
}
public static void EndCurrentEdit(Control control, string controlProperty)
{
if (control.DataBindings[controlProperty] != null)
{
if (control.DataBindings[controlProperty].IsBinding)
control.DataBindings[controlProperty].BindingManagerBase.EndCurrentEdit();
}
}
}
Wouldn’t it be better to have these solved in framework level? Why do I need to write this code over and over again? This can goes also to a multi-threaded application that needs to pass objects between threads. Also to the same code we write over and over again to make use xml files, resource files, etc…