Question

I am building an applicaiton using asp.net mvc3. I added two layers to this project, repository and service layer. some of my code, I am not sure which layer I should put them in.

on my register page, to create an account, I only ask user to enter their email and password, its using UserRegisterViewModel. before I add it to the database, my user table need more info.

I need to create an User entity from the UserRegisterViewModel:

user.ip= "1.1.1.1";
user.createddate="11/11/1911";
....

so above code, where should I put them? webui, repoistory or service layer.

Was it helpful?

Solution

I would add the 'CreatedDate' in the constructor of your 'User' entity, and add the IP address in the controller that receives the ViewModel object.

I case you didn't know: you can map ViewModels to Entities using AutoMapper.

OTHER TIPS

You can only get the IP address from the request so you have 'get' it there in the Action

Something like this

[HttpPost]
public ActionResult Login(UserRegisterViewModel model) {
    if(ModelState.IsValid) {
        SaveLogonToAudit(model.Username);
    }
    return View(model);
}

private void SaveLogonToAudit(string username) {
    var user = new UserAccount(username, Request.Browser.Browser, Request.Browser.Type, Request.UserHostAddress);
    user.Save();
}

The User entity could live in another layer, your UserRegisterViewModel will live in the MVC UI layer. It's perfectly normal to have a ViewModel that represents the data in your view and a completely separate class in another layer that represents your User entity. That's good design. Your User entity can be in the ServiceLayer and have business rules associated to it. That class will then call into your repository layer to save its data.

I agree with Leon Cullens, the CreateDate should live in the User entity, that's why you don't see me setting it. The User entity should handle it's own CRUD actions that call into your RepositoryLayer. Set the CreateDate in the ctor or better yet, have a base class that has CreateDate, CreatedBy, LastUpdateDate, LastUpdatedBy that the User will use internally.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top