Understanding Repository Objects, do all start a new connection to the DB?
I have my Database, that have my ADO.NET Entity Data Model that have my Repository in one Application Library project
then, in the website for each Controller I start with
MyRepository db = new MyRepository(开发者_JAVA百科);
I am expecting that this website will be accessed 50.000 a day and I was wondering... does the new MyRepository()
part start a new connection to the Database?
is this safer?
public class MyController : Controller
{
public MyRepository db { get; set; }
protected override void Initialize(RequestContext requestContext)
{
if (db == null) { db = new MyRepository(); }
base.Initialize(requestContext);
}
...
How can I learn a little bit more about how does the Entity Model hooks up into the database to prevent multiple "bad" things to happen?
I have several objects that I would like to Cache it, as they are pretty complex and never change (unless someone on the ADMIN area changes it), what are my best options?
Thank you.
ADO.NET uses a connection pool which avoids creating connections to the database everytime. A pool of connections is created per application domain and per connection string and connections from this pool are reused. So when you instantiate your repository no connection is created to the database. For caching objects you might take a look at the standard techniques.
As far as your example is concerned if MyRepository
is disposable it is recommended to call the Dispose
method on it which could be done in the Dispose
method of the controller.
精彩评论