Creating different POCO's from a single type
I'm writing a wrapping library around a SCADA database and I've got a small 'hippy-code-design' question. It involves how I'm converting the supplied DB objects into POCO's.
I'm calling the DB using the vendors .NET libraries.
When I make calls the DB, it passes back an object of type DBObject
. This can represent anything in the database, and as such is quite generic in the information that it holds. Any custom information is accessed from the DB with a DBObject.GetProperty()
method call, which requires the connection to the DB to be active.
I'm looking to convert this basic DBObject
into a variety of different POCO's so that I can pass them around via WCF and other tech.
Currently I have a abstract base class called ScadaObject
that has a number of base properties that I want all the POCO's to contain and a protected virtual method called InternalFromDBObject(DBObject obj)
which performs this creating from the DBObject
into the POCO.
In my derived types I override this method, call base.InternalFromDBObject(obj)
as the first call, then p开发者_StackOverflowroceed to load any custom/specific information. I then have a static method on the POCO called FromDBObject()
which returns a new instance of that POCO.
Is this a good way to do things? I'm not hugely experienced in using factories so I'm not sure if they would fit. Is there any other design patterns I could use?
You can create your poco classes without deriving from nothing, just add to your poco public properties matching name and type of the returned DBObject. Then you can create an external static class taking one ( and a collection of ) DBobject and returning one ( or a collection of ) your poco.
static class ScadaTOPoco
{
public static TPoco Create<TPoco>(DbObject sb){...}
public static IEnumerable<TPoco> Create<TPoco>(IEnumerable<DbObject> sb){...}
}
Each method internally can:
- Instantiate the poco
Use reflection to obtain a list of public properties on the POCO
Loop on each property and by use DBobject.GetProperty assign the value in the target poco object. Make sure to convert the value by calling Convert.ChangeType to convert to the properly in the target property type.
If the property names in the scada does not fit the .NET naming convenction, you can embed some convention to map from scada to .NET...
精彩评论