Coming soon: RefGen, a real open source product from all my musings
August 30, 2006 on 12:16 am | In .NET Coding |I’ve been pounding out code now at 6 hour clips twice a day for the past oh… I don’t know how long, when did I start this blog? Finally, after all of my testing and positing, I have something to show for it and to share with the world.
So many people have helped me along the way, and I can’t thank everyone enough, so I figure I’ll do the next best thing, and open source the more universally useful part of my project.
It’s called RefGen, and what it does is cache reflection data about an object, and expose dynamic method based property accessors via a simple interface called the Introspector. It also features UI generation via this object mapping. Basically it allows you to mark up your classes with a couple of simple attributes and generate the UI that will allow you to edit one on your webpage. You get the data into it by just assigning your object to it, and the data back out by reading it. I’m still working on a way to infer control styling from objects that don’t have the attribute markup, so that’s coming soon.
I’m not quite ready to post the code, but in the meantime, here’s a sample of how it works with my beaten to death Person object as a sample. (First, without markup so you can see the Introspector works on any old class)
public class Person
{
string name;
int ssn;
DateTime dob;
public string Name
{
get { return name; }
set { name = value; }
}
public int SSN
{
get { return ssn; }
set { ssn = value; }
}
public DateTime DateOfBirth
{
get { return dob; }
set { dob = value; }
}
}
Nothing special about the class, just a data holder (Domain Object).
Now if I want to have fast dynamic access to the properties by a string name (something I need a lot for ORMappers, generating UI’s and whatnot) then you’ll now have them with the Introspector.
Here’s a sample clip:
Person po = new Person();
po.Name = "Dave Dolan";
po.SSN = 123456789;
po.DateOfBirth = new DateTime(1912, 3, 21);
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
for(int x = 0; x < 1000000; x++ )
{
Introspector.SetProperty(po, "SSN", x);
}
sw.Stop();
Console.Out.WriteLine("Setting the SSN property (boxing incurred) 1 million times took {0} milliseconds.", sw.ElapsedMilliseconds);
// something like 800 ms or so, depending on the machine you run it on
sw.Reset();
sw.Start();
string Foo = null;
for(int x = 0; x < 1000000; x++)
{
Foo = Introspector.GetProperty(po, "Name");
}
sw.Stop();
// something like 400 milliseconds, again depending on your host
Console.Out.WriteLine("Getting the Name property (no boxing) 1 million times took {0} milliseconds." sw.ElapsedMilliseconds);
foreach(KeyValuePair property in Introspector.PropertyDictionary(po))
{
Console.Out.WriteLine("Property {0} is of type {1}", property.Key, property.Value.Name);
Console.Out.WriteLine("po.{0} = ", Introspector.GetProperty(po, property.Key);
}
// bonus material: Fast creation by string name of the type
// this only works once the type has been "Learned" by Introspector, which
// happens automatically when you pass it an object for the first time to either
// list it's properties, get or set them. It is a ton faster than Activator.CreateInstance().
object NewPerson;
sw.Reset();
sw.Start();
for(int x = 0; x < 1000000; x++)
{
NewPerson = Introspector.CreateObject("Person");
}
sw.Stop();
More to come. This isn’t as useful by itself unless you do a lot of ORMapping or Grokking of objects based on runtime parameters, but add UI generation and it’s money. I’ll show and tell, and hopefully post some code (it’s already working, just not presentable yet) soon.
Edit: Wow, it’s really hard to get code formatting working in wordpress…
No Comments yet »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^
