2016-11-06 79 views
0

我使用ServiceStack.ORMLite和SQLite作爲數據庫。我創建了一個通用的存儲庫:通過ServiceStack.ORMLite調用sqlite函數

public class Repository<T> : IRepository<T> where T : class, new() 
{ 
    private ReestrContext db; 

    public Repository(ReestrContext db) 
    { 
     this.db = db; 
    } 

    public long CountAll() 
    { 
     return db.Connection.Count<T>(); 
    } 

    public IQueryable<T> GetAll() 
    { 
     return db.Connection.SelectLazy(db.Connection.From<T>().Limit()).AsQueryable(); 
    } 

    public IQueryable<T> GetAll(int rows) 
    { 
     return db.Connection.SelectLazy(db.Connection.From<T>().Limit(rows)).AsQueryable(); 
    } 

    public IQueryable<T> GetAll(int? skip, int? rows) 
    { 
     return db.Connection.SelectLazy(db.Connection.From<T>().Limit(skip, rows)).AsQueryable(); 
    } 

    public T GetById(long id) 
    { 
     return db.Connection.LoadSingleById<T>(id); 
    } 

    public long CountByCondition(Expression<Func<T, bool>> predicate) 
    { 
     long res = db.Connection.Count<T>(predicate); 
     string qry = db.Connection.GetLastSql(); 
     return db.Connection.Count(predicate); 
    } 

    public IQueryable<T> GetByCondition(Expression<Func<T, bool>> predicate) 
    { 
     return db.Connection.SelectLazy(db.Connection.From<T>().Where(predicate).Limit()).AsQueryable(); 
    } 

    public IQueryable<T> GetByCondition(Expression<Func<T, bool>> predicate, int rows) 
    { 
     return db.Connection.SelectLazy(db.Connection.From<T>().Where(predicate).Limit(rows)).AsQueryable(); 
    } 

    public IQueryable<T> GetByCondition(Expression<Func<T, bool>> predicate, int? skip, int? rows) 
    { 
     return db.Connection.SelectLazy(db.Connection.From<T>().Where(predicate).Limit(skip, rows)).AsQueryable(); 
    } 

    public long Create(T item) 
    { 
     using (var trans = db.Transaction) 
     { 
      long res = db.Connection.Insert(item, selectIdentity: true); 
      try 
      { 
       trans.Commit(); 
      } 
      catch 
      { 
       trans.Rollback(); 
      } 
      return res; 
     } 
    } 

    public T Update(T item) 
    { 
     using (var trans = db.Transaction) 
     { 
      db.Connection.Update(item); 
      try 
      { 
       trans.Commit(); 
      } 
      catch 
      { 
       trans.Rollback(); 
      } 
      return item; 
     } 
    } 

    public long Delete(long id) 
    { 
     using (var trans = db.Transaction) 
     { 
      long res = db.Connection.Delete(id); 
      try 
      { 
       trans.Commit(); 
      } 
      catch 
      { 
       trans.Rollback(); 
      } 
      return res; 
     } 
    } 
} 

在一個客戶端我創建了一個返回表達式樹過濾功能。但我的POCO類有

[Alias("Bd")] 
[DataType(DataType.Date)] 
public Nullable<DateTime> BirthdaySingle { get; set; } 

也在過濾條件中使用的字段。 因此,我無法找到解決方案來正確地創建此字段上的過濾器(因爲表達式樹不處理它),我想知道什麼可能是另一種解決方案來實現這種過濾。 ORMLite是否支持調用SQLite函數?在我的情況下,它需要是「日期」功能。或者可能它使用System.ComponentModel.DataAnnotations命名空間在字符串字段上設置[DataType(DataType.Date)]屬性。我不知道。請幫幫我。

回答

0

它看起來像你的代碼引用了LINQ的Expression<T>樹而不是OrmLite的SqlExpression<T>樹。它們看起來很相似,但OrmLite只支持將SqlExpression<T> lambda轉換爲查詢。

我會推薦在新發布的OrmLite Gistlyn sandbox中玩遊戲以快速測試你的ORM代碼。

+0

感謝您的鏈接,我會嘗試。但還有一個問題。 LINQ表達式工作正常,但如果使用DateTime字段則不適用。那麼分層體系結構如何,我需要在表示層中引用OrmLite庫嗎?或者我需要在另一層中構建過濾器? – Dmitry