ASP.NET MVC 3 Application using Ninject, Entity Framework 4 Code-First CTP 5, Patterns

一笑奈何 提交于 2019-12-02 14:44:34
Rushino

Eww. This one was sneaky. Actually i don't know ninject much so i couldnt figure it out right away.

I found the solution for the SECOND question which was related to the error by finding that ninject actually shoot two instance of the DatabaseFactory, one for the repository and one for the unit of work. Actually, the error was not the problem. It was an internal error in the object database but its normal i think since im using Entity Framework.

The real problem was that Ninject was binding two different instance of IDatabaseFactory which lead to 2 connection open.

The review was added to the first set in _reviewRepostory which was using the first instance of the Database.

When calling commit on the unit of work.. it saved nothing due to the fact that the review wasnt on this database instance. In fact, the unit of work called the databasefactory which lead to creating a new instance since ninject sent a new instance of it.

To fix it simply use :

 Bind<IDatabaseFactory>().To<DatabaseFactory>().InSingletonScope();

instead of

Bind<IDatabaseFactory>().To<DatabaseFactory>();

And now all the system work correctly!

Now, would love some answers regarding the first question which was if there anything wrong with my current code ? Ive applied patterns correctly ? Any suggestions or recommendation that would lead me in the right direction ?

One small observation: by having your EFRepositoryBase and IReviewRepository have methods that return an IEnumerable<> instead of an IQueryable<>, you prevent subsequent methods from adding filter expressions/constraints or projections or so on to the query. Instead, by using IEnumerable<>, you will do any subsequent filtering (e.g. using LINQ extension methods) on the full result set, rather than allowing those operations to affect and simplify the SQL statement that gets run against the datastore.

In other words, you are doing further filtering at the webserver level, not at the database level where it really belongs if possible.

Then again, this may be intentional - sometimes using IEnumerable<> is valid if you do want to prevent callers of your function from modifying the SQL that is generated, etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!