将两种方法合并为单一通用方法

时间:2014-09-17 15:42:42

标签: c# linq-to-sql

我的DAL中有两个相同的方法,它们根据作为参数提供的主键返回数据库行。 DB上下文是linqtoSQL。一个传递一个字符串参数,第二个传入一个int参数。我认为必须使用泛型来创建一个接受字符串或int但不确定如何的方法。

    /// <summary>
    /// Select table row by integer Primary Key Value
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="id">The PK value to search for</param>
    /// <returns>Single matching PK to id</returns>
    public T SelectRowByPk<T>(int id) where T : class
    {
        using (var dc = new DBDataContext())
        {
            // Get the table by the type passed in
            var table = dc.GetTable<T>();
            // Get the metamodel mappings (database to domain objects)
            MetaModel modelMap = table.Context.Mapping;
            // Get the data members for this type
            ReadOnlyCollection<MetaDataMember> dataMembers =  
            modelMap.GetMetaType(typeof (T)).DataMembers;
            // Find the primary key field name by checking for IsPrimaryKey
            string pk = (dataMembers.Single(m => m.IsPrimaryKey)).Name;

            // Return a single object where the id argument matches the primary key 
            field value
            return table.SingleOrDefault(delegate(T t)
                                            {
                                                int memberId =

  Convert.ToInt16(t.GetType().GetProperty(pk).GetValue(t, null));
                                                return memberId == id;
                                            });
        }
    }

    /// <summary>
    /// Select table row by Varchar Primary Key Value
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="id">The PK value to search for</param>
    /// <returns>Single matching PK to id</returns>
    public T SelectRowByVarcharPk<T>(string id) where T : class
    {
        using (var dc = new DBDataContext())
        {
            // Get the table by the type passed in
            var table = dc.GetTable<T>();
            // Get the metamodel mappings (database to domain objects)
            MetaModel modelMap = table.Context.Mapping;
            // Get the data members for this type
            ReadOnlyCollection<MetaDataMember> dataMembers = 
  modelMap.GetMetaType(typeof(T)).DataMembers;
            // Find the primary key field name by checking for IsPrimaryKey
            string pk = (dataMembers.Single(m => m.IsPrimaryKey)).Name;

            // Return a single object where the id argument matches the primary key 
 field value
            return table.SingleOrDefault(delegate(T t)
            {
                string memberId =
                   t.GetType().GetProperty(pk).GetValue(t, null).ToString();
                return memberId == id;
            });
        }
    }

2 个答案:

答案 0 :(得分:1)

您不能直接使用泛型将两种方法合并为一种,但是您可以将一些重构与泛型结合使用以获得干净的设计:

使用两个通用参数创建一个私有方法,一个用于实体类型,另一个用于主键类型:

 private T SelectRowById<T, TId>(TId id) where T : class
    {
        using (var dc = new DBDataContext())
        {
            // Get the table by the type passed in
            var table = dc.GetTable<T>();
            // Get the metamodel mappings (database to domain objects)
            MetaModel modelMap = table.Context.Mapping;
            // Get the data members for this type
            ReadOnlyCollection<MetaDataMember> dataMembers = 
  modelMap.GetMetaType(typeof(T)).DataMembers;
            // Find the primary key field name by checking for IsPrimaryKey
            string pk = (dataMembers.Single(m => m.IsPrimaryKey)).Name;

            // Return a single object where the id argument matches the primary key 
 field value
            return table.SingleOrDefault(delegate(T t)
            {
                var memberId =
                   (TId)t.GetType().GetProperty(pk).GetValue(t, null);
                return memberId == id;
            });
        }
    }

在此之后,您可以为使用此私有方法的特定id类型声明两个公共方法:

public T SelectRowById<T>(string id) where T : class
{
   return SelectRowById<T, string>(id);
}

public T SelectRowById<T>(int id) where T : class
{
   return SelectRowById<T, int>(id);
}

通过这种方式,您将拥有一个干净的公共界面,并且代码重复最少。

答案 1 :(得分:-1)

public T SelectRowByPk<T>(T id)
{
    //you'll need to do some run-time type checking of 'id' here since string and int
    //can't be constrained by a type parameter constraint in the same method
}

指定类型参数的一个要点是,可以使用动态参数类型。在此示例中,&#39; id&#39;成为你指定的任何类型。