我的數據庫中有二進制數據,我必須在某個時間點將其轉換爲位圖。我在考慮是否適合使用存儲庫並在那裏執行。我的消費者,這是一個表示層,將使用此存儲庫。例如:EF模型和代碼使用者之間的存儲庫使用情況
// This is a class I created for modeling the item as is.
public class RealItem
{
public string Name { get; set; }
public Bitmap Image { get; set; }
}
public abstract class BaseRepository
{
//using Unity (http://unity.codeplex.com) to inject the dependancy of entity context.
[Dependency]
public Context { get; set; }
}
public calss ItemRepository : BaseRepository
{
public List<Items> Select()
{
IEnumerable<Items> items = from item in Context.Items select item;
List<RealItem> lst = new List<RealItem>();
foreach(itm in items)
{
MemoryStream stream = new MemoryStream(itm.Image);
Bitmap image = (Bitmap)Image.FromStream(stream);
RealItem ritem = new RealItem{ Name=item.Name, Image=image };
lst.Add(ritem);
}
return lst;
}
}
這是使用存儲庫模式的正確方法嗎?我正在學習這個模式,我已經在網上看到了很多的例子正在使用的存儲庫,但是當我看了看他們的源代碼,例如:
public IQueryable<object> Select
{
return from q in base.Context.MyItems select q;
}
,你可以看到幾乎沒有行爲除了隱藏數據訪問查詢外,通過他們的方法添加到系統中,所以我很困惑,也許存儲庫是別的東西,我把它弄錯了。最後應該有額外的使用它們的好處嗎?
更新:因爲事實證明,如果在發送數據前沒有更多數據需要完成,您就不需要存儲庫了,但請等待!沒有抽象的LINQ查詢?這樣客戶端必須爲我們提供查詢語句,這些查詢語句可能有點不安全並且難以驗證,所以也許庫也提供了對數據查詢的抽象?如果這是真的,那麼擁有一個存儲庫總是項目架構中的一個基本需求! 然而這個抽象可以通過使用SQL存儲過程來提供。如果兩種選擇都可用,有什麼選擇?
請檢查我的更新。謝謝。 – jim 2012-04-01 15:27:28
看到我更新的答案 – MikeSW 2012-04-01 16:25:30