Question

I have 3 objects that have same properties with some differences each object have Id, Name, and TypeCode. In database we have a first table containing Id, Name and TypeCode fields, and there is 3 other tables containing each one the corresponding properties of each object:

Table 1: Id | Name | TypeCode

Table 2: Id | FunctionName

Table 3: Id | AnalysisName

Table 4: Id | PropertyName

We have a storedProcdure called "GetProperties" that returns the properties of an object according to its TypeCode. "GetProperties" takes one arguments which is the object Id. we don’t know the TypeCode before calling the stored procedure.

If TypeCode is a function is returns the following fields:

Id, Name, TypeCode, FunctionName

If TypeCode is an Analysis is returns the following fields:

Id, Name, TypeCode, AnalysisName

And etc...

I have created 4 classes: FunctionObject, AnalysisObject and PropertyObject that inherit from class MyObject.

public class MyObject
{
     public decimal Id;
     public string Name;
     public string TypeCode;
}

public class FunctionObject: MyObject
{
    public string FunctionName;
}

public class AnalysisObject: MyObject
{
    public string AnalysisName;
}

public class PropertyObject: MyObject
{
    public string PropertyName;
}

I have created a generic function that allows me to create objects dynamically:

    public T GetObject<T>(string storedProcedure, List<DataBaseParameteres> parameters) where T: new()
{
    T result = new T();
    OpenConnection();
    SqlDataReader reader = ExecuteSqlCommand(storedProcedure, parameters);
    PropertyInfo[] properties = typeOf(T).GetProperties();

    if(reader.Read())
    {
        for(int i = 0; i<properties.Count(); i++)
        {
            object value = Convert.ChangeType(reader[properties[i].Name], properties[i].PropertyType);
            properties[i].SetValues(result, value, null);
        }
    }
    reader.Close();
    CloseConnection();
    return result;
}

This function works well, i can call it as follow and it returns the created object:

MyObject obj1 = GetObject<MyObject>(obj1Id);

My problem now is that i want to know how to modify the generic method "GetObject" to create objects: FunctionObject, AnalysisObject and Propertyobject. I don't have any idea about TypeCode when calling the function, so I can’t do this unless I have the TypeCode:

FunctionObject obj2 = GetObject<FunctionObject>(obj2Id);
Was it helpful?

Solution

I dont think, as per your scenario, it is possible to create the objects of these types. The reason being you have embedded the call to the stored procedure and creation of the object in the same method and intermingled them.

My suggestion is to remove the call to the stored procedure from that method to another method, execute that stored proc to a datatable and pass that datatable to this method. In this way you will have your TypeCode with you in advance and then you can use factory pattern to create objects of required type.

Also in this way you can also reduce your dependency on reflection and hence your code might be a little better performance wise.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top