MVC3 & EF. Interface for TDD

2019-09-04 15:06发布

问题:

Can somebody please explain:

  1. I am using MVC3/C#/Razor to build a project to get used to using MVC.
  2. I am using the inbuilt account controller.
  3. I am storing the account data in my local SQL database using Entity Framework to connect.
    How can I easily generate interfaces for EF?

  4. SO FAR I am using the plugin from: http://blog.johanneshoppe.de/2010/10/walkthrough-ado-net-unit-testable-repository-generator/#step1

This allows me to have an interface for my entities already created.

However, I know that I have to change my HomeController arguments to accept either the real repository or a fake one for testing.

I am completely lost!

回答1:

Have a look at these. They will help and get you started :

http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

http://msdn.microsoft.com/en-us/library/gg416511(VS.98).aspx

For dependency injection, you can follow these steps :

Install-Package Ninject.MVC3 with nuget to your ASP.NET MVC 3 project (if your app is on version 3). This will basically do everything.

Then have the following on your controller :

    private IMyModelRepository _myrepo;

    public HomeController(IMyModelRepository myrepo)
    {
        _myrepo = myrepo;
    }

Go to NinjectMVC3.cs file inside App_Start folder and add the following code to inside RegisterServices method :

    private static void RegisterServices(IKernel kernel) {

        kernel.Bind<IMyModelRepository>().To<MyModelRepository >();

    }   

Fire up your app and you should be up and running.