c# - lazyloading without using lazy -
c# - lazyloading without using lazy<t> -
i have:
category class
public partial class category : baseentity { ... public string name { get; set; } private icollection<discount> _applieddiscounts; public virtual icollection<discount> applieddiscounts { { homecoming _applieddiscounts ?? (_applieddiscounts = new list<discount>()); } protected set { _applieddiscounts = value; } } }
service:
public ilist<category> getcategories() { // ado.net homecoming category entities. } public icollection<discount> getdiscount(int categoryid) { // ado.net . }
i don't want utilize orm ef.. plain ado.net , don't want set in ugly lazy<t>
in domain definition, e.g public lazy....
so how in case applieddiscounts automatically binded lazy getdiscount without using explicitly declaration of lazy<t>
on category class ?
i don't know why don't lazy<t>
type - simple, useful , don't have worry anything.
and no 1 forces utilize public lazy<ienumerable<discount>> getdiscounts();
you utilize internally:
lazy<ienumerable<discount>> discounts = new lazy<ienumerable<discount>>(() => new discountcollection()); public ienumerable<discount> getdiscounts() { homecoming discounts.value; }
it operates intended - until no 1 asks discounts won't created.
if want - create own implementation. singleton class in richter's "clr via c#" book (because lazy has 'properties' of proper singleton container - thread safety, 1 instance of inner 'singleton' value evaluated...).
but want create , test? replace well-designed standard component fishy custom one.
after reading question attention
1) if lazy loading not need thread safety accomplish similar behaviour without lazy or complex constructs - utilize func delegate:
public partial class category : baseentity { ... private func<icollection<discount>> getdiscounts; public category(func<icollection<discount>> getdiscounts) { ... } public string name { get; set; } private icollection<discount> _applieddiscounts; public virtual icollection<discount> applieddiscounts { { homecoming _applieddiscounts ?? (_applieddiscounts = new list<discount>(this.getdiscounts())); } protected set { _applieddiscounts = value; } } } public ilist<category> getcategories() { // ado.net homecoming category entities. ... = new category(() => this.getdiscount((int32)curdataobject["categoryid"])) } public icollection<discount> getdiscount(int categoryid) { // ado.net . }
if inject service more simple:
public virtual icollection<discount> applieddiscounts { { homecoming _applieddiscounts ?? (_applieddiscounts = new list<discount>(this.service.getdiscounts(this.categoryid))); } protected set { _applieddiscounts = value; } }
2) if need utilize these objects in multiple threads have redesign classes - don't threadsafe.
after comment
what want guy stackoverflow.com/questions/8188546/… . want know concept how orm ef domain, maintain clean , separated injecting service class still able handle lazy loading. know can utilize reflection object properties , object variables(like applieddiscounts), dont' know how transform these dynamically lazy type loaded later when needed.
it universal principle can't nothing. can't create entities both clean , separated services(even through proxy), , allow them load lazily - if don't know services , services don't know them how lazy loading work? there no way accomplish such absolute decoupling(for 2 components interact have either know each other, know 3rd module-communicator, or module should know them. such coupling partially or hidden.
technologies provide entity object models utilize of next techniques:
code generation create wrappers(or proxies) above simple info objects, or solid instances of interfaces. c# code or il weaving, well, in-memory assembly created dynamically in runtime using reflection.emit. not easiest or direct approach, give enormous code-tuning capabilities. lot of modern frameworks utilize it. implementation of capabilities in context classes - won't have lazy loading in end objects, have utilize explicitly context classes:context.orders.with("orderdetails")
. positive side entities clean. injection of service(or of needed subset of operations) - that's you'd prefer avoid. use of events or other observer-like pattern - entities clean service logic , dependencies(at to the lowest degree in sense), contain hookup infrastructure won't straightforward or easy manage. for custom object model 2 or 3 best bets. seek 1 roslyn
c#
Comments
Post a Comment