Friday, 28 May 2010

DeferredLoadingEnabled options in Linq

Linq to SQL has a drawback it load data from all the available relationship of database table. So it crush or become slow that’s why if we make DeferredLoadingEnabled property false it will not load its child or parent data when we request any single row from any table. 

base.DataContext.DeferredLoadingEnabled = false;

We can specify the parent or child table name when we try to fetch any particular table data. Then it will retrieve all the related data automatically from the define table. We can do it when we make the instance of any class. 

DataLoadOptions options = new DataLoadOptions();

options.LoadWith<Builder>(m => m.SalesOffices);

options.LoadWith<SalesOffice>(m => m.ModelHomes);

options.LoadWith<ModelHome>(m => m.ModelHomeDetailImages);

options.LoadWith<SalesOffice>(m => m.SalesOfficeAds);

base.DataContext.LoadOptions = options;

In the above example we create DataLoadOptions object then assign its LoadWidth property then again assigne DataContext LoadOptions. So current DataContext instance only have above define tables relational data only.

In this way we can make faster use of Linq to SQL.

Singletone Pattern

Making one instance of a class through out the project is called singletone. To do a object singletone you could make a class instance as static. Here is the technique to declare a object as singletone.

static BuilderInformation _Instance = null;

public static BuilderInformation GetInstance()


if (_Instance == null)


            _Instance = new BuilderInformation();


return _Instance;


When you need the object of this class just declare like:

 BuilderInformation builderInformation = BuilderInformation.GetInstance();

We can make collection of object as singletone. Here is an example of that. If SalesOfficeOID is not in the _LiveAgentConversationList list make an object of _ InstanceLA and append it to the collection of list.

static LiveAgentConversation _InstanceLA = null;

static IList<LiveAgentConversation> _LiveAgentConversationList = new List<LiveAgentConversation>();

public static LiveAgentConversation GetInstance(string saleOfficeOID)


LiveAgentConversation liveAgentConversation = null;

      if (_LiveAgentConversationList.Count > 0)


            for (int i = 0; i <>


                  if (_LiveAgentConversationList[i]._SalesOfficeOID == saleOfficeOID)


                        return _LiveAgentConversationList[i];



            if (liveAgentConversation == null)


                liveAgentConversation = new LiveAgentConversation();

                liveAgentConversation._SalesOfficeOID = saleOfficeOID;               



            return liveAgentConversation;



During object invokation write statement like:

LiveAgentConversation liveAgentConversation = LiveAgentConversation. GetInstance(string saleOfficeOID)