I have a DAL base class ( data access ) which has 2 members:
/*1*/ class BaseDal
/*2*/ {
/*3*/ static DatabaseProviderFactory factory = new DatabaseProviderFactory();
/*4*/ static SqlDatabase sqlServerDB = factory.Create("ExampleDatabase") as SqlDatabase;
/*5*/ }
/*6*/
/*7*/
/*8*/ subclasses :
/*9*/
/*10*/ class MyCustomerDal:BaseDal
/*11*/ {
/*12*/ ...
/*13*/ ...
/*14*/ public static DataTable GetData()
/*15*/ {
/*16*/ // do something....
/*17*/ }
/*18*/
/*19*/ }
/*20*/
My question is about lines 3,4.
please notice that I don't create new MyCustomerDal
cuz I dont need an instance but only to use the method GetData()
(static
).Also , those 2 lines can serve all derived classes.
And here is my question :
I want those 2 initializers (line 3,4) to be laze initialize.
well I have 2 options :
option 1
I can set a static ctor which basically means that those members will be running only when the class is accessed ( beforefieldinit
issue).
option 2
I could use Lazy
:(+property)
/*1*/ Lazy<SqlDatabase> myDb = new Lazy<SqlDatabase>(() => factory.Create("ExampleDatabase") as SqlDatabase);
/*2*/
/*3*/ protected SqlDatabase Mydb
/*4*/ {
/*5*/ get { return myDb.Value; }
/*6*/ }
But to tell you the truth I don't know which approach is better....