简体   繁体   中英

asp.net MVC 4 with Data Access Layer using Entity Framework 5?

In my project, i have first created my Data Access Layer using Entity Framework with the following projects in a single solution, 1.Domain Model - Entity Model (.edmx) 2.Services - Business Services, Dtos, Infrastructure(Configurator), Interfaces and Models(Repository)

Now the problem is, i want to connect this data access layer to my MVC project, i do not know how to make the data access layer projects to behave as the models for my mvc project. So can anyone tell me how to connect my data access layer into my controllers and views.. any references is appreciated. Thanks in Advance !

I think what you're asking is what's the best way for controllers to interact with your services and data layer?

One option is to use the mediator pattern , and decouple the services from the controllers.

There's a great implementation for ASP.NET MVC apps: ShortBus , also available on nuget that I've used in a number of projects, and so far it's worked great.

One of the nice things about ShortBus is it's support for dependency injection. In the example below, all the services are created with Ninject, and require the appropriate registration.

The basic idea is you define queries and commands that the controllers will use, and then add handlers to perform the actual work.

public class AddUser : ICommand<User>
{
    public string Email { get; set; }
} 

and then a handler:

public class AddUserHandler : ICommandHandler<AddUser, User>
{
    private IDatabaseService _database;
    private IEmailService _email;

    public AddUserHandler(IDatabaseService database, IEmailService email)
    {
         _database = database;
         _email = email;
    }

    public User Handle(AddUser command) 
    {
        bool created = _database.CreateUser(command.Email);

        if (created)
        {
            _email.SendWelcome(command.Email);
        }
    }
}

Then inside your controller, all you'd do is issue the command:

public class UsersController : Controller
{
     private IMediator _mediator;

     public UsersController(IMediator mediator)
     {
         _mediator = mediator;
     }

     public ActionResult Create(string email)
     {
         User user = _mediator.Send(new AddUser("foo@bar.com"));
     }
}

The things I like about this pattern are:

  1. Controllers don't need to know how to create a user. It issues a command, and the appropriate business logic handles it.

  2. Each handler can require the services it needs. There's no need to pollute the controllers with services only used by a single action.

  3. It's really easy to unit test. I use a mock, and only need to verify that _mediator.Send() was called with the correct parameters. Then to test the handler, I mock IDatabaseService and IEmailService and verify they are called correctly in the 2 cases.

  4. Commands and queries can be reused, and again, the caller never needs to know what's required to handle the request.

As for the Views, I'd recommend ViewModels .

Each View gets it's own ViewModel, which holds whatever is required for showing that particular page. You'd then map your domain objects to their own individual ViewModels, possibly with AutoMapper .

What's nice about ViewModels is you can format the data appropriately (formatting a DateTime maybe), and then your Views don't need any special logic. If later you decide to update the DateTime format, you only need to change it in one place.

Create a (shared) interface to pass to the layer that's between the DAL and MVC, especially if you're unit testing. Use a repository pattern. Check it out here:

http://csharppulse.blogspot.com/2013/09/learning-mvc-part-5repository-pattern.html

This should get you going...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM