Is there an abbreviation for updating common methods?

We have a data access library with such universal methods:

public List<T> Find<T>(
    List<DataObject> conditions,
    Boolean fuzzy = false,
    String order = null,
    Int32 limit = Int32.MaxValue,
    T start = null) where T : DataObject, new()
{
    ... 
}

// special case of Find()
public List<T> Find<T>(
    DataObject condition,
    Boolean fuzzy = false,
    String order = null,
    Int32 limit = Int32.MaxValue,
    T start = null) where T : DataObject, new()
{
    ...
}

Now our business objects should be endowed with several methods Get(), such as:

public static MyClass Get(Guid id)
{
    List<MyClass> possibles = Get(new MyClass() { Id = id });
    if (possibles.Count == 1)
    {
        return possibles[0];
    }
    else
    {
        return null;
    }
}

public static List<MyClass> Get(
    MyClass condition,
    Boolean fuzzy = false,
    String order = null,
    Int32 limit = Int32.MaxValue,
    MyClass start = null)
{
    using (QueryBuilder qb = ResourceFinder.GetQueryBuilder()) {
        return qb.Find<MyClass>(condition, fuzzy, order, limit, start);
    }
}

public static List<MyClass> Get(
    List<MyClass> conditions,
    Boolean fuzzy = false,
    String order = null,
    Int32 limit = Int32.MaxValue,
    MyClass start = null)
{
    using (QueryBuilder qb = ResourceFinder.GetQueryBuilder()) {
        return qb.Find<MyClass>(conditions, fuzzy, order, limit, start);
    }
}

Is there a shorter way to determine this higher level Get()s?

This will be a great place to use some pre-processing; but C # is missing. With a few exceptions, they are all the same except for the Type parameter. And the ultimate goal is to make these almost identical definitions less copy / paste / replace efforts, which admittedly is not much; it's just stupid.

: DataObject. , DataObject, , , - SQL-. , .

. " , ", " " " " .

+3
1

:

class SomeBase<T> where T : DataObject, new()
{
    public T Get(Guid id) where T : DataObject, new()
    {
        return Get(new T() { Id = id }).FirstOrDefault();
    }

    public List<T> Get(
        T condition,
        Boolean fuzzy = false,
        String order = null,
        Int32 limit = Int32.MaxValue,
        MyClass start = null) where T : DataObject
    {
        using (QueryBuilder qb = ResourceFinder.GetQueryBuilder()) {
            return qb.Find<T>(condition, fuzzy, order, limit, start);
        }
    }

    public List<T> Get(
        List<T> conditions,
        Boolean fuzzy = false,
        String order = null,
        Int32 limit = Int32.MaxValue,
        MyClass start = null) where T : DataObject
    {
        using (QueryBuilder qb = ResourceFinder.GetQueryBuilder()) {
            return qb.Find<T>(conditions, fuzzy, order, limit, start);
        }
    }

factory, :

// This class will have "typed" versions for these factory methods
public class MyClassFactory : SomeBase<MyClass>
{
}
+4
source

All Articles