I’m working in asp.net mvc 4 with Entity Framework 5 (with edmx). I’m trying to get Ninject to work correctly, but using the bindings has got me quite confused. I’ve seen Ninject being used before, but that was in a WCF project where the DI was being set in the WCF-layer.
Right now I have 4 layers:
- DataAcces (contains edmx and repositories, which I will show later)
- BusinessLogic (standard BL)
- Common (models)
- Gui (a mvc4 project)
Now here’s the tricky part: I want to use DI here. The way I saw it being used in my previous WCF-project was that my WCF-layer went to my DataAccess so I can use the kernel.bind.
Now I don’t want that here. I’m not using WCF. I also don’t want to call my DataAccess in my Gui.
Since I said I’d show some code for insight:
Repository in DataAccess
public class Repo: IRepo
{
Entities context = new Entities();
public IQueryable<PictureSource> PictureSource
{
get { return context.PictureSource; }
}
}
my IRepository is just this:
public interface IRepository
{
IQueryable<PictureSource> PictureSource { get; }
}
What I want to be able to do is in my BusinessLogic. I want to be able to do the following:
public List<Picture> GetStuff(IRepository Repo)
{
//code
}
Now I’ve looked a lot on the internet. About 80% of the examples use Web Apim which is useless to me. The other 20% just seem to do whatever they want “because it’s just a demo” and violate Gui-BL-DA principle. I’ve seen examples consisting of a single layer to examples doing Business Logic in Data Access.
The ninject wiki also didn’t help me as I’m new to DI and I’ve only seen it being used in an existing application.
Try to code using Poor-Man’s-DI. Inject your dependencies in your constructor!
Once you have your codebase right, register your dependencies in Ninject. Ninject then takes care to inject your Repository to you BusinessLogic class.
Please note that you have an IQueryable in your IRepo, which you should avoid (http://www.infoq.com/news/2012/03/IQueryable-api)
Edit:
This would be your solution structure and the references: