我已經用C#,MVC,實體框架,LINQ等編寫了一個Web應用程序......現在我想追溯地爲整個項目創建單元測試。爲單元測試創建存儲庫
據我所知,爲了編寫有效的單元測試,我需要爲模型創建存儲庫,以便它們可以被模擬,因此實體框架將對數據庫執行任何命中。
我有3個主要模型;類別,密碼,用戶密碼。
類別包含類別和密碼。密碼包含UserPasswords(鏈接到用戶帳戶)。所以我創建了3個存儲庫,其基本方法如:創建,刪除,更新等..
然而,看着這個LINQ查詢:
var selectedCategoryItem = DatabaseContext.Categories
.Where(c => c.CategoryId == ParentCategoryId)
.Include(c => c.SubCategories)
.Include(c => c.Passwords)
.Include(c => c.Passwords.Select(p => p.Creator))
.ToList()
.Select(c => new Category()
{
SubCategories = c.SubCategories
.Where(sub => !sub.Deleted)
.OrderBy(sub => sub.CategoryOrder)
.ToList(), //make sure only undeleted subcategories are returned
Passwords = c.Passwords
.Where(pass => !pass.Deleted
&& (
(UserPasswordList.Any(up => up.PasswordId == pass.PasswordId && up.Id == UserId))
|| (userIsAdmin && ApplicationSettings.Default.AdminsHaveAccessToAllPasswords)
|| pass.Creator_Id == UserId
)
) //make sure only undeleted passwords - that the current user has acccess to - are returned
.OrderBy(pass => pass.PasswordOrder)
.ToList(),
CategoryId = c.CategoryId,
CategoryName = c.CategoryName,
Category_ParentID = c.Category_ParentID,
CategoryOrder = c.CategoryOrder,
Parent_Category = c.Parent_Category,
Deleted = c.Deleted
})
.SingleOrDefault();
如果倉庫只是像保存,更新,刪除,插入,FindById等基本方法....我應該如何將查詢分解成存儲庫?這裏還有很多商業邏輯,例如過濾用戶可以訪問的密碼,應該在哪裏?
我讀了一些關於返回IQueryable對象的信息,以便可以在服務層中更改LINQ查詢......看起來如何?
你看着努力呢? HTTPS://effort.codeplex。com/ – Bringer128 2014-12-02 01:19:16