Guys, I am going to use Enterprise Library (4.1) and especially DAAB. Here is I have questions:
What is the best approach and why:
Every time when I need to run a DbCommand I create Database instance using DatabaseFactory.CreateDatabase();
I have a base class with instanced Database (using the same CreateDatabase() static method) and something like public property which returns the instanced database.
How it is “heavy” or fast/slow to create an instance of Database class? What if I do it every time when a DbCommand is needed?
Thank you.
It's not a problem. Creating the database class has low overhead.
However, actually creating the database connection has high overhead, which is why Windows does Connection Pooling. Briefly, the first time a process creates a DB Connection, it looks in the connection pool for an existing connection with exactly the same connection string. If it doesn't find one, it creates a new one (an expensive operation). When th process closes it and lets it go out of scope, it doesn't actually close the connection to the DB, it puts it in the Connection Pool. There is stays until the same process creates another connection with the same connection string. Then it gives you the already existing one from the Connection Pool.
You can turn off connection pooling (via a setting in the connection string) but that's usually a very bad idea.