创建指定Type T的通用存储库

时间:2014-11-19 08:02:43

标签: c# linq generics ormlite-servicestack

我正在尝试编写一个使用基本存储库的通用存储库,但是包含包含PersonId的特定poco对象子集的方法。有没有办法使用lamda表达式,我在声明中将类型T指定为BasePersonItem,但在linq表达式中使用类型T?目前我刚收到编译错误:

BasePersonRepository.cs(45,45): Error CS1061: Type `ServiceStack.OrmLite.SqlExpression<T>' does not contain a definition for `PersonId' and no extension method `PersonId' of type `ServiceStack.OrmLite.SqlExpression<T>' could be found. Are you missing an assembly reference? (CS1061) (TribeGuru.Database.Repository)

我无法看到一种方法告诉函数T是基本人物类型而不是它的实现方式?

[DataContract]
public class BasePersonItem : BaseItem
{
    public BasePersonItem ()
    {
    }

    [Alias("person_id")]
    [DataMember]
    public int PersonId { get; set; }
}

public class BaseRepository<T> : IBaseRepository<T> where T : class
{
    private IDbConnectionFactory _connectionFactory;

    public BaseRepository ()
    {
        var connection = ConfigurationManager.ConnectionStrings ["bob"].ConnectionString;
        _connectionFactory = new OrmLiteConnectionFactory (connection, MySqlDialect.Provider);
    }

    protected IDbConnection GetConnection()
    {
        return _connectionFactory.OpenDbConnection ();
    }
}

public class BasePersonRepository<T> : BaseRepository<T> where T : BasePersonItem 
{
    public BasePersonRepository ()
    {
    }

    public List<T> GetByPersonId (int personId)
    {
        using (var cn = GetConnection ())
        {
            return cn.Select<T> (c => c.PersonId = personId).ToList ();
        }
    }
}

0 个答案:

没有答案