Deferred load class
1 Public classLazyloader<tmodel>whereTModel:class2 {3 TModel _data;4Func<tmodel> _loaddatafunc =NULL;5 BOOL_loaded =false;6 PublicLazyloader (): This(NULL){ }7 PublicLazyloader (func<tmodel>Loaddatafunc)8 {9_loaddatafunc =Loaddatafunc;Ten_data =NULL; One_loaded =false; A } - PublicTModel querydata () - { the if(!_loaded && _loaddatafunc! =NULL) - { -_data =_loaddatafunc (); - } + return_data; - } + Public voidSetData (TModel value) A { at_data =value; -_loaded =true; - } -}Lazyloader
How to use (simply to name a column)
For example, the employee menu might not need to load his menu when you re-use the employee's object. When you want to use it, you need to delay loading it.
1 Public classEmployee2 {3 PublicLazyloader<ienumerable<menu>>_menus;4 PublicEmployee (Guid employeeId,stringEmployeeno,stringNamestringPasswordstringEmailstringPhoneBOOLEnabledBOOLIsadministrtor, DateTime Registerdate,stringRemarkstringiplimitation)5 {6EmployeeId =employeeId;7Employeeno =Employeeno;8Name =name;9Password =password;TenEmail =email; OnePhone =phone; AEnabled =enabled; -Isadministrator =Isadministrtor; -Registerdate =registerdate; theRemark =remark; -Iplimitation =iplimitation; -_menus =NewLazyloader<ienumerable<menu>> (() =Systemresourceservice.querymenusbyemployeeid (employeeId)); - } + - PublicGuid EmployeeId {Get;Private Set; } + Public stringEmployeeno {Get;Private Set; } A Public stringName {Get;Private Set; } at Public stringPassword {Get;Private Set; } - Public stringEmail {Get;Private Set; } - Public stringPhone {Get;Private Set; } - Public BOOLEnabled {Get;Set; } - Public BOOLIsadministrator {Get;Private Set; } - PublicDateTime Registerdate {Get;Private Set; } in Public stringRemark {Get;Private Set; } - Public stringiplimitation {Get;Set; } to Public ObjectId + { - Get{returnEmployeeId;} the } *}Employee
Custom Lazy Load Classes