I have two objects that are similar but not quite the same:

```
public class ObjectA
{
    public string PropertyA { get; set; }
}

public class ObjectB
{
    public string PropertyA { get; set; }
    public string PropertyB { get; set; }
}

```

In both objects `PropertyA` is used for database lookup via a stored procedure, however, the stored procedure used differs between the objects. Assuming the stored procedure returns successfully, the response is used to create the relevant object.

Currently I have two methods that perform the same logic of calling the relevant stored procedure and then creating the object:

```
public ObjectA QueryObjectA(string propertyA)
{
    // shared code

    var result = CallToStoredProcedureA(...);
    var obj = new ObjectA
    {
        // use result here
    };

    return obj;
}

public ObjectB QueryObjectB(string propertyA)
{
    // shared code

    var result = CallToStoredProcedureB(...);
    var obj = new ObjectB
    {
        // use result here
    };

    return obj;
}

```

This works, however, I am maintaining some shared code in multiple places and if I were to add another object in the future I would potentially have a third method (`QueryObjectC`).

Can this, or should this, be improved through the use of generics and if so what might that look like? Perhaps something along the lines of the following?

```
public T QueryObject<T>(string propertyA) where T : new()
{
    // shared code

    // get type of T
    // switch statement that calls the relevant stored procedure and creates the object

    // return object
}

```