Rob Smyth

Thursday 28 May 2009

For non-trivial applications I'm finding Visual Studio's designer to be a bit of honey pot for corruption. Great for a trivial app but it exposes just too many options. Once the application design has been defined I'm wondering if some think the following code is simpler and simpler to maintain than traditional designer generated code.

using NDependencyInjection.interfaces;
using UISpike.Application;
using UISpike.Controls;
namespace UISpike.Scenario3.InjectionApproach
{
public class PageBuilder : ISubsystemBuilder
{
public void Build(ISystemDefinition system)
{
using (var page = new ActionResultsPanelBuilder(system))
{
using (var actions = page.Actions)
{
actions.HasButton<IFrontCamera>("frontCameraHomeButton", "Home Camera");
}

using (var content = page.Content)
{
using(var splitPanel = content.HasSplitPanel())
{
using (var panel = splitPanel.LeftPanel)
{
panel.HasNumericLabel<IFrontCamera>("fronCameraPosition", "Front camera pos", "#,###");
panel.HasNumericLabel<IRearCamera>("rearCameraMotorPosition", "Rear camera pos", "#,###");
}

using (var panel = splitPanel.RightPanel)
{
panel.HasCustomControl(new BigGraphBuilder());
}
}
}
}
}
}
}


Storing a .Net C# Generic Type Parameter

From time to time I hit the problem where I have a call to a generic method where I need to resuse the generic type for later use. Today I think I found a simple pattern that solves the problem.

A code fragement explains all (I hope):
  public class MyRegister
{
private List<IToken> tokens = new List<IToken>();

public void HasService<T>()
{
tokens.Add(new MyToken<T>());
}

public object[] UseRegisteredGenericTypes()
{
List<object> results = new List<object>();

foreach (var token in tokens)
{
results.Add(token.DoSomethingWithMyType());
}

return results.ToArray();
}
}

public class MyToken<T> : IToken
{
public object DoSomethingWithMyType()
{
// Do the stuff here like ... return myApp.DoStuff<T>();
}
}