0

I am using Repository design pattern "Martin's Fowler" in my application (MVC3, WebForms) also i use "ONLY ONE" DbContext ("Singletone") through out all of my application.

Details: Repository is looks like that,

class Repository<T> : IRepository<T>, IUnitOfWork

only one repository wich i create many more repositories, like so:

class UserRepository {
   private IRepository<User> _repository;
   //dependency injection via constructor using Ninject 
   public UserRepository(IRepository<User> repository) {
       _repository = repository;
   }
}

and so on...

What is best practice in these cases, what you are suggestion to make better???

I have to increase my performance and of course to learn the correct way to do it.

Thank you all for the answers.

gnat
  • 6,199
  • 101
  • 49
  • 71
IamStalker
  • 3,813
  • 8
  • 50
  • 88

1 Answers1

2

You are using one context for the whole application = you are done. Your application will not work. You must change it to use a new context instance for each request.

Also I wonder how is your question related to performance and what should your code snippet represent?

Community
  • 1
  • 1
Ladislav Mrnka
  • 349,807
  • 56
  • 643
  • 654
  • more than one dbcontext throws me an error, something like "you are using more than one dbcontext...." after i changed to use singletone it's worked nicely and didn't failed me once. For the question about performance : It's almost correct if create more than one context this will make performance better? and i've said i need to know how to do it the right way. – IamStalker Aug 28 '11 at 05:04
  • I have already described what you need to do to do it right way. Simply your current solution is terribly wrong. – Ladislav Mrnka Aug 28 '11 at 09:14
  • Ok, thanks that really didn't helped me much. Any way thank you for the help. – IamStalker Aug 28 '11 at 11:49
  • Here is what i found: http://stackoverflow.com/questions/6334592/one-dbcontext-per-request-in-asp-net-mvc-without-ioc-container I think it will tell that your idea is not correct. What is your proff point? – IamStalker Aug 29 '11 at 05:29
  • It tells exactly what I did. It asks how to use a new context per request whereas you are using one context for the whole application (singleton). – Ladislav Mrnka Aug 29 '11 at 06:20
  • ok can you explane in a simpler language or maybe you can show the code snippet for this please? – IamStalker Aug 29 '11 at 08:11
  • But the question you linked contains both explanations and exampley. – Ladislav Mrnka Aug 29 '11 at 08:27