Serve Laurijssen
Serve Laurijssen

Reputation: 9763

getting rid of a generic type argument: is it possible?

Consider this DAL like code:

mDealerTypes = Read<Dictionary<string, DealerType>, DealerType>(
() => { return DALFactory.Instance.ReadDealerTypes(cn); },
(c, o) => c.Add(o.DealerTypeKey, o));

mCountries = Read<Dictionary<string, Country>, Country>(
() => { return DALFactory.Instance.ReadDealerTypes(cn); },
(c, o) => c.Add(o.Code, o));

        private C Read<C, T>(Func<SqlConnection, IDataReader> func, Action<C, T> a)
            where C : ICollection, new()
            where T : EntityBase, new()
        {
            C objects = new C();

            using (IDataReader reader = func(connection))
            {
                while (reader.Read())
                {
                    T obj = new T();
                    obj.Init(reader);
                    a(objects, obj);
                }
            }

            return objects;
        }

For readability, I would like to somehow change this so the object which is stored in the collection is not repeated twice in the type argument list of Read<>.

mCountries = Read<Dictionary<string, Country>>(
() => { return DALFactory.Instance.ReadDealerTypes(cn); },
(c, o) => c.Add(o.Code, o));

but how to rewrite Read<> then? Is this even possible?

private ?? Read(Func func, Action a)

Upvotes: 0

Views: 52

Answers (1)

J&#252;rgen Steinblock
J&#252;rgen Steinblock

Reputation: 31743

Create a simple wrapper method (or extension method)

public Dictionary<T1, T2> ReadEx<T1, T2>(...) where T2 : EntityBase
{
    return Read<Dictionary<T1, T2>, T2>(...)
}

Upvotes: 1

Related Questions