Asp.net MVC + EF + Spring.Net 項目實踐(三)


這一篇要整合Model層和Repository層,提供一個統一的操作entity的接口層,代碼下載地址(博客園上傳不了10M以上的文件,所以用了百度):http://pan.baidu.com/s/1kTIIJqf,如果有編譯不通過的,請重新引用package中相關的dll

 

1. 首先把項目中所有沒有用的Class1.cs都刪除,然后把在StudentManageSystem.Repository層的Model都移動到StudentManageSystem.Model層里,效果如下

   --> 

  注意要修改Model的命名空間,還要添加引用

  

2. 在StudentManageSystem.Repository層和StudentManageSystem.Repository.Test層添加StudentManageSystem.Model的引用,運行單元測試,若能通過,則說明重構成功(若不成功,揮刀自宮^_^)

  

3. 添加IBaseRepository接口、BaseRepository類,再修改SMSContext.cs文件,添加Detach方法。
BaseRepository需要一個帶參數的構造器,這塊是為了后期使用時,可以注入dbcontex。

  

 

4. 在StudentManageSystem.Interface層添加IStudentRepository, IScoreRepository, ISubjectRepository, IGradeRepository四個文件,在IStudentRepository中添加接口GetById

  

5. 對應的在StudentManageSystem.Repository層添加四個Repository文件,在StudentRepository類里實現GetById接口,如下圖

  

6. 新建一個單元測試,StudentRepository的GetById方法能正確返回值。

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM