是否有关于Ninject + Repositories和ASP.NET MVC的好教程

时间:2011-02-19 11:48:56

标签: asp.net-mvc-3

有人可以在ASP .NET MVC 3.0中使用存储库,测试e.t.c给我一个关于Ninject的好文章的链接。

THX。


在我以前的解决方案中,我使用了ControllerFactory:

/Core/MvcFairyTaileControllerFactory.cs

protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
    string connectionString = ConfigurationManager.ConnectionStrings["dbFairyTalesConnectionString"].ConnectionString;

    if (controllerType == null)
        return base.GetControllerInstance(requestContext, controllerType);
    else
        return (Activator.CreateInstance(controllerType, new DataManager(connectionString)) as IController);
}

我有DataManager类:

/Models/DataManager.cs

public class DataManager
{
    private MvcFairyTalesDataContext _MvcFairyTalesDataContext;
    public DataManager(string connectionString)
    {
        _MvcFairyTalesDataContext = new MvcFairyTalesDataContext(connectionString);
    }

    private FairyTalesRepository _fairyTalesRepository;
    public FairyTalesRepository Tales
    {
        get
        {
            if (_fairyTalesRepository == null)
                _fairyTalesRepository = new FairyTalesRepository(_MvcFairyTalesDataContext);

            return _fairyTalesRepository;
        }
    }

    private CategoriesRepository _categoriesRepository;
    public CategoriesRepository Categories
    {
        get
        {
            if (_categoriesRepository == null)
                _categoriesRepository = new CategoriesRepository(_MvcFairyTalesDataContext);

            return _categoriesRepository;
        }
    }
}

还有我的CategoriesRepository

/Models/CategoriesRepository.cs

public class CategoriesRepository
{
    private MvcFairyTalesDataContext _dataContext;

    public CategoriesRepository(MvcFairyTalesDataContext dataContext)
    {
        _dataContext = dataContext;
    }

    public IEnumerable<Category> GetAllCategories()
    {
        return _dataContext.Categories.OrderBy(c => c.NameAn);
    }
}

在我的控制器中,我可以使用这样的存储库:

public class CategoriesController : Controller
{
    //
    // GET: /Categories/
    private DataManager _dataManager;
    public CategoriesController(DataManager dataManager)
    {
        _dataManager = dataManager;
    }

    public ActionResult Index()
    {
        ViewData["Categories"] = _dataManager.Categories.GetAllCategories();
        return View();
    }
}

所以,我认为这不是一个很好的解决方案,我想在Ninject + MVC 3.0中重写它。


更新

现在我有这样的事情:

protected void Application_Start()
{
    RegisterDependencyResolver();
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

private void RegisterDependencyResolver()
{
    var kernel = new StandardKernel();
    kernel.Bind<IDataManager>().To<DataManager>().WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["dbFairyTalesConnectionString"].ConnectionString);
    DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}

我的DataManager类是:

public class DataManager : IDataManager
{
    FairyTalesMVC3DataContext _dataContext;
    public DataManager(string connectionString)
    {
        _dataContext = new FairyTalesMVC3DataContext(connectionString);
    }

    private TalesRepository _talesRepository;
    public TalesRepository Tales
    {
        get
        {
            if (_talesRepository == null)
                _talesRepository = new TalesRepository(_dataContext);

            return _talesRepository;
        }
    }
}

但是我应该在IDataManager中写什么来返回控制器“Tales”?

public class HomeController : Controller
{
    private readonly IDataManager _dataManager;

    public HomeController(IDataManager dataManager)
    {
        _dataManager = dataManager;
    }

    public ActionResult Index()
    {
        ViewBag.Message = _dataManager.Tales.GetAllCategories().First().NameAn; // Tales not defined.

        return View();
    }
}

1 个答案:

答案 0 :(得分:1)

您可以查看一个blog post。它说明了如何将新的IDependencyResolver与Ninject一起使用。这里another one使用更标准的NinjectHttpApplication

就单元测试而言,这与您正在使用的DI框架没有任何关系。至于您的存储库是通过接口正确抽象的,测试控制器就是选择一个模拟框架。这是一个example with Rhino Mocks

MvcContrib TestHelper也值得一看,因为它可以简化你的单元测试,模拟所有HTTP上下文,例如Request,Response,Session,......