c# - Get Previously Instantiated Object from Heap for Variable Reference -




i have 2 classes contain class property other.

public class client {     public customer customer { get; set; }      public client()     {      } }  public class customer {     public client client { get; set; }      public customer(int id)     {         // retrieval logic using id ...     } } 

here factory class shows concept trying achieve if knew objects prior compile time. in reality, not , have instantiate them via reflection user provided data. in mind, looking assign object's property opposite class created object other class (i.e. below)

public class factory {     public factory()     {         // done via reflection @ run-time in real code         client client = new client();         client.customer = new customer(1); // id retrieve client property (not shown simplicity sake)         client.customer.client = client; // refer first object of client     } } 

however, utilizing provided factory class example, client object in code not in scope or rather higher on stack (the various properties instantiated recursively , if higher class has property same type in sub-subsequent class / object skipped (to avoid infinite recursion).

how can utilize either class name or object's properties in factory method point property instantiated object when may not in scope?

i thinking of using list / dictionary store higher level object(s) reference , pass them down through recursion , checking if object's type matches sub-subsequent property's type , using object stored in list / dictionary property. wanted see if best way or if there another.

this purely theoretical , may or may not used. goal time , object created these classes, of related classes have properties in first object contain instantiated instances of related classes never null / not infinitely recuse.

i suggest use factory both customer , client, 1 small enhancement: want use factory implements caching. example can found in answer.

when construct customer, populate client caching factory. , vice versa. caching mechanism take care of rest. naturally you'll want factory injected singleton instance cache common across code uses factory.

the customer factory might little this:

public class customerfactory : icustomerfactory {     private readonly iclientfactory clientfactory;  //to injected      private readonly concurrentdictionary<int, icustomer> customers =                   new concurrentdictionary<int, icustomer>();      public customerfactory(iclientfactory clientfactory)     {         this.clientfactory = clientfactory; //injected     }      public icustomer getcustomer(int id)     {         icustomer customer = this.customers.getoradd(id, () => new customer(id));         if (customer.client == null)         {             customer.client = this.clientfactory.getclient(customer.clientid);         }         return customer;     } } 

in above example, you'll notice newly instantiated customer added cache before attempting set client property. important avoid infinite loops. if tried retrieve client before customer in cache, clientfactory wouldn't able find it, , may end creating new instance.

on other hand, maybe don't need set client property right away. after all, have caching mechanism now, can afford set lazily. remove code getcustomer...

    public icustomer getcustomer(int id)     {         return this.customers.getoradd(id, () => new customer(id));     } 

...and retrieve client when needed.

class customer {    public client client    {               {            return this.clientfactory.getclient(this.clientid);        }    } } 

while seems expensive call clientfactory on , over, happens quick lookup in dictionary. , if caller doesn't need client saving round trip database.

this whole idea isn't different idea proposed toward end of question, except won't need pass references down. reference needed reference factories.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -