0

我正在學習將DIC應用於MVC項目。所以,我已經勾畫出了這個DDD-ish DIC-ready-ish佈局,以便更好地理解。過去幾天我讀過許多博客文章。但是,我對於正確實施它沒有信心。您能否向我展示如何以正確的方式將它們放入DIC?在所有的閱讀結果後,我更喜歡Ninject或Windsor,但只要我能夠正確理解如何去做,任何DIC都會這樣做。學習在MVC中實現DIC

網絡控制器...

public class AccountBriefingController { 
    //create 
    private IAccountServices accountServices { get; set; } 
    public AccountBriefingController(IAccountServices accsrv) 
     accountServices = accsrv; 
    } 
    //do work 
    public ActionResult AccountBriefing(string userid, int days) { 
     //get days of transaction records for this user 
     BriefingViewModel model = AccountServices.GetBriefing(userid, days); 
     return View(model); 
    } 
} 

視圖模型...

public class BriefingViewModel { 
    //from user repository 
    public string UserId { get; set; } 
    public string AccountNumber {get; set;} 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    //from account repository 
    public string Credits { get; set; } 
    public List<string> Transactions { get; set; } 
} 

服務層...

public interface IAccountServices { 
    BriefingViewModel GetBriefing(); 
} 

public class AccountServices { 
    //create 
    private IUserRepository userRepo {get; set;} 
    private IAccountRepository accRepo {get; set;} 
    public AccountServices(UserRepository ur, AccountRepository ar) { 
     userRepo = ur; 
     accRepo = ar; 
    } 
    //do work 
    public BriefingViewModel GetBriefing(string userid, int days) { 
     var model = new BriefingViewModel(); //<---is that okay to new a model here?? 
     var user = userRepo.GetUser(userid); 
     if(user != null) { 
      model.UserId = userid; 
      model.AccountNumber = user.AccountNumber; 
      model.FirstName = user.FirstName; 
      model.LastName = user.LastName; 
      //account records 
      model.Credits = accRepo.GetUserCredits(userid); 
      model.Transactions = accRepo.GetUserTransactions(userid, days); 
     } 
     return model; 
    } 
} 

領域層和數據模型...

public interface IUserRepository { 
    UserDataModel GetUser(userid); 
} 
public interface IAccountRepository { 
    List<string> GetUserTransactions(userid, days); 
    int GetUserCredits(userid); 
} 
// Entity Framework DBContext goes under here 

請指出我的實現是否是錯誤的,例如我可以在AccountServices-> GetBriefing中感受到 - > BriefingViewModel()對我來說似乎是錯誤的,但是我不知道如何將該stud嵌入到DIC中?

非常感謝您的幫助!

回答

2

我想這裏有兩個問題。首先,如何與Ninject或Windsor建立客戶依賴關係解析器。其次,依賴關係是否建模正確。

我會在一對夫婦的第一個問題的鏈接指向你,因爲它已經回答了相當不錯這裏SO和博客文章:

Ninject

Windsor

在第二個問題,恕我直言,我不認爲IAccountServices應該有必要了解視圖模型。我喜歡保持我的依賴關係在一個方向上對齊。因此,AccountBriefingController的工作就是將User(或一個名爲Briefing的新模型對象)轉換爲BriefingViewModel。這照顧了是否要新建一個BriefingViewModel的問題。但是,您必須將User映射到控制器中的BriefingViewModel(這將需要您創建一個新實例)。否則,依賴關係對我來說確實很好。

所以我想改變GetBriefing這樣:

public User GetBriefing(string userid, int days) { 
     var user = userRepo.GetUser(userid); 
     if(user != null) { 
      return user; 
     } 
     throw new CustomException(); //if this makes sense 
    } 

然後,你會做在控制器中的映射。

+0

非常感謝。我現在想到了:D – Tom 2012-07-09 10:19:56

+1

@Tom很高興幫助。我在想這個更多。我想我會選擇返回一個新的「簡報」對象,以便您仍然可以查找積分和交易。然後將'Briefing'映射到'BriefingViewModel'。然後,你有一個乾淨的分離。 – 2012-07-09 11:16:22

+0

謝謝指出。我理解你的想法。其實,我原來的全尺寸計劃是app.website - >視圖模型 - > app.services - > domain.services - > domain.data模型和存儲庫...其中domain.services組織數據模型,app.services層圖他們查看模型...或更胖的視圖模型可以維護數據模型並管理它們...那麼我認爲整個事情並不是那麼幹,而我想出了第二個計劃。 – Tom 2012-07-10 08:46:13