Learning to implement DIC in MVC

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2012-07-08T08:45:29Z Indexed on 2012/07/08 21:15 UTC
Read the original article Hit count: 211

I am learning to apply DIC to MVC project. So, I have sketched this DDD-ish DIC-ready-ish layout to my best understanding. I have read many blogs articles wikis for the last few days. However, I am not confident about implementing it correctly. Could you please demonstrate to me how to put them into DIC the proper way? I prefer Ninject or Windsor after all the readings, but anyDIC will do as long as I can get the correct idea how to do it.

Web controller...

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);
    }
}

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; }
}

Service layer ...

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;
    }
}

Domain layer and data models...

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

Please point out if my implementation is wrong, e.g.I can feel in AccountServices->GetBriefing -> new BriefingViewModel() seems wrong to me, but I don't know how to fit the stud into DIC?

Thank you very much for your help!

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about mvc