我正在学习将 DIC 应用于 MVC 项目。所以,我已经根据我的最佳理解勾勒了这个 DDD-ish DIC-ready-ish 布局。在过去的几天里,我阅读了许多博客文章 wiki。但是,我对正确实施它没有信心。您能否向我演示如何以正确的方式将它们放入 DIC 中?在阅读完所有资料后,我更喜欢 Ninject 或 Windsor,但只要我能正确理解如何操作,anyDIC 就可以。
网页控制器...
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 -> new BriefingViewModel() 中感觉到对我来说似乎是错误的,但我不知道如何将螺柱放入 DIC?
非常感谢您的帮助!