Introduction
In medium to large scale IT project you will end up with quite a lot of business
logic objects. To test each one of them you generally create some kind of test
window that will accept initial data, run some logic, and then display new
object states. So we will have window with a nice [GO!] command button and a bunch
of labels to display properties and fields of a business entity after processing,
and a couple of dozens lines of boring code to copy data from object instance to
these labels. If your object contains collection or other objects, your
choices will be:
-
Test it in debugger.
-
Spend a working day or two just to create a decent test window.
In mine opinion, a programmer should be lazy enough to spend 9 hours 59 minutes to
create tool that can do something that can be done manually in 10 hours, and
than have 1 minute to enjoy while the magic happens.
Instead of making copy-paste functionality for each business entity we
want to check, let's build a generic black box which can accept objects and
display everything it has inside. Using reflection, there is no object we
cannot disassemble!
Our Windows Control Library contains user control that you can use as part of
your test window and a complete "Display object state" window. Object properties are
presented in a ListView. You can double-click on any non-primitive field or
property to display its state (drill down).
Feel free to use this code; it will be great if it will save you some boring
coding time. As usual any suggestions will be appreciated.
Using the Code
-
Add reference to the WindowsUtils assembly to your test project.
-
Add the using case:
using ClalBit.Infrastruct.WindowsUtils;
-
Add the following wherever you want to display your object state:
frmObjectProperties frm = new frmObjectProperties(objectToExplore, "ObjectName");
frm.Show();
In case you want to create a more complicated test window, you can use the ObjectProperties control instead.
Just drop it onto your form and call the SetObject
method on it. See frmObjectProperties
for an example.