1

I'm trying to work with NInject in my MVC 3 application, and i have one question.

Interface

public interface ITalesRepository
{
    IEnumerable<Tale> GetAllTales();
}

Repository

public class TalesRepository : ITalesRepository
{
    private FairyTalesMVC3DataContext _dataContext;

    public TalesRepository(FairyTalesMVC3DataContext dataContext)
    {
        _dataContext = dataContext;
    }

    public IEnumerable<Tale> GetAllTales()
    {
        return _dataContext.Tales.OrderBy(c => c.NameAn);
    }
}

Home controller

public class HomeController : Controller
{
    private readonly ITalesRepository _talesRepository;

    public HomeController(ITalesRepository talesRepository)
    {
        _talesRepository = talesRepository;
    }

    public ActionResult Index()
    {
        ViewBag.Tales = _talesRepository.GetAllTales();

        return View();
    }
}

So, i need to initialize my TalesRepository with DataContext, and now it is so:

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

So, my question, is it ok or something wrong?

FSou1
  • 1,861
  • 4
  • 18
  • 25
  • Yes it is. I would just suggest renaming `Tales` to `Tale` – Aliostad Feb 22 '11 at 12:53
  • I wouldn't pass in the LINQToSql data context into the repository constructor - just the connection string. Use the TalesRepository class to encapsulate the data context. Or are you using the context in other repository classes? – Steven Striga Feb 22 '11 at 13:06
  • Yeah, i have lots of repositories where i use dataContext. – FSou1 Feb 22 '11 at 13:08

2 Answers2

1

First of all:

public IEnumerable<Tale> GetAllTales()
{
    return _dataContext.Tales.OrderBy(c => c.NameAn);
}

I would add .ToList() to the end. Else you'll get data layer exceptions in your presentation layer which is not fine.

Next, I would recommend that you switch to ViewModels instead of using ViewBag. It's a lot easier to prevent that logic leaks into the views if you are using ViewModels. Since you can add the logic to the ViewModel and thus get the same behaviour in all views using the model.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
0

Your application should inherit from NinjectHttpApplication. It registers dependency resolver, so you don't have to do it.

You should also override CreateKernel in application class and register your own module with bindings:

public class MvcApplication : NinjectHttpApplication
{
    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new INinjectModule[] {new MvcModule()});
    }
}

public class MvcModule : NinjectModule
{
    public override void Load()
    {
        Bind<ITalesRepository>().To<TalesRepository>();
        Bind<FairyTalesMVC3DataContext>().To<FairyTalesMVC3DataContext>().InRequestScope();
    }
}
LukLed
  • 31,452
  • 17
  • 82
  • 107