- Lazy Load Pattern use K‡i GKwU Object ‡K on Demand load/ initialize Kiv nq|
- Ab¨fv‡e ejv hvq hLb cª‡qvRb ZLb loading Gi gva¨‡g object creation defer Kiv nq|
UML Diagram:
Benefits
- hvnv cÖ‡qvRb ïaygvÎ Zvnv loading Gi gva¨‡g memory Save Kiv nq|
- Execution deferring Gi gva¨‡g initial load time reduce Kiv nq|
Drawbacks:
- lazy-loading Gi gva¨‡g load K…Z object wUi first time load delay n‡Z cv‡i|
Description:
·
Lazy-loading ORMs (Object-relational mappings) Gi key
feature.
·
KviY ORM
ZLbB database query ‡Z
perform K‡i
hLb data required nq|
d‡j data retrieval
time reduce nq|
Code Examples:
- g‡bKwi Avgv‡`i `ywU class Av‡Q Candidate class I EducationProfile class.
- GKRb candidate Gi GKvwaK EducationProfile(like: Bachelors (BBA) ,Master(MBA)) Av‡Q|
- GLb Avgiv candidate Gi mv‡_ Zvi Education Profile load Kiv‡Z PvB Z‡e Candidate Gi mv‡_ associated EducationProfiles load Ki‡Z n‡e| †m‡ÿ‡Î Candidate object initialize Ki‡Z n‡e| d‡j A‡bK fvix n‡q hv‡e|
- GB situation avoid Kivi Rb¨ Avgiv Lazy Loading Pattern use Ki‡ev|
- G‡ÿ‡Î ZLbB EdutioanProfile load n‡e hLb EducationProfile list use Kiv n‡e|
- d‡j data retrieval faster n‡e Ges performance increase n‡e|
- bx‡P Candidate EducationProfile load Gi Rb¨ Lazy loading pattern define Kiv n‡jv-
- cÖ_‡g Avgiv Candidate I EducationProfile class Øq create Ki‡ev-
public class EducationProfile
{
public int Id { get; set; }
public string Digree { get; set; }
public DateTime PassingYear { get; set; }
}
//Hope this will give you a little reference
of lazy loading
public class Candiate
{
public string Name { get; set; }
public int EducationProfileId { get; set; }
public List GetAllEducationProfile()
{
return educationProileList.value;
}
Lazy<list<educationproile>> educationProileList;
public Candiate(string name, int id)
{
//Initializing Candiate Object
Name = name;
EducationProfileId = id;
educationProileList = new Lazy<list<educationproile>>(()
=> { return GetEducationProfileList(id); });
//Initialization done
}
private List GetEducationProfileList(int id)
{
//Loading EducationProiles
List list = new List();
Parallel.For(100, 110, (int i) =>
{
EducationProile educationprofile = new EducationProile();
educationprofile.Id = i;
list.Add(educationprofile);
});
return list;
}
}
</list<educationproile></list<educationproile>
·
Candidate class Gi
constructor Gi g‡a¨ properties
mg~n initialize Kiv n‡q‡Q Ges educationProileList object
declare Kiv n‡q‡Q| (educationProileList
GKwU generic List Ges Bnv‡K GetEducationProfileList method Øviv fill Kiv
n‡q‡Q)
·
GB method
ZLbB call Kiv n‡e hLb educationProileList object use n‡e|
·
wb‡¤œ main
method define Kiv n‡q‡Q hvnv lazy
loading Gi behavior show K‡i-
public
static void Main(string[] args)
{
Candidate candidate = new
Candidate("AnyName", 1);
foreach(EducationProile eduProfile in
candiate.GetAllEducationProfile())
// it will actually load accounts, ie. lazy
loading
Console.WriteLine("Id:{0}",eduProfile.Id);
Console.Read();
}
When to
Use
wb‡¤œ KwZcq situation
‡`qv n‡jv †h‡ÿ‡Î Lazy-loading pattern
use Ki‡j effective result cvIqv
hvq-
- GKwU tab widget Gi content mg~n ZLbB load n‡e hLb D³ tab Dnv‡`i show Ki‡e
- GKwU image gallery ZLbB image mg~n load Ki‡e hLb D³ gallery Gi image mg~n viewed n‡e
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন