working on layered arch for wcf service -is it better to initialize the Database object in constructor or in method

Aug 31, 2011 at 3:32 PM

Hi am working on layerd architecture..

have a look at the below code..

    public class MovieDataService : IMovieDataProvider 

  {   
     public MovieDataService()   

   {

           Database db = DatabaseFactory.CreateDatabase();   

          }   
           public List<string> FindAllGenre()  

          {     
      Database db = DatabaseFactory.CreateDatabase();   
          } 

 }

 

from the above code which one is the right pattern ..ceating the database factor object in the constructor or in the method call..

I personally fell its better to have that in the method ...

 

any suggestions

Sep 1, 2011 at 7:09 AM

Hi,

According to Chris Tavares on this thread, it is best to create the database object whenever you needed it, since it is cheap and easier to create and dispose when done.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

 

Sep 5, 2011 at 6:39 AM

Hey am sorry i dont understand your point...

but i think what you are saying is to create the database object in side the methods rather than the constructor....

Sep 5, 2011 at 6:44 AM

Yes, it would be on the method, since database operations are called inside the method :)

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us