Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Lazy Class Usage
Message
De
26/10/2010 21:24:29
 
 
À
26/10/2010 12:49:25
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01486990
Message ID:
01487049
Vues:
57
Just curious Viv ... if you *did* want to use the code as Kevin originally had it (*not* using the Lazy class) and thread safety *was* an issue, I'm assuming you could easily refactor that original code to make it thread safe, correct? I seem to remember reading something not too long ago about this, but I've forgotten where.

~~Bonnie





>>I have this property:
>>
>>
>>private List<MerchantLocationModel> _Locations = null;
>>public List<MerchantLocationModel> Locations
>>{
>>    get 
>>    {
>>        if (_Locations == null)
>>        {
>>            TDCDataProvider provider = new TDCDataProvider();
>>            _Locations = provider.GetMerchantLocations(RecordId);
>>        }
>>
>>        return _Locations; 
>>    }
>>    set
>>    {
>>        if (value != _Locations)
>>        {
>>            _Locations = value;
>>            RaisePropertyChanged("Locations");
>>        }
>>    }
>>}
>>
>>Can I use the new Lazy class in the getter? if so, I could use some help coding it. I don't quite understand it. Is there any benefit to sing Lazy over what I have here?
>
>Not much benefit unless thread safety is a concern. If you did want to use it then something like this:
   public class Something
>    {
>        private Lazy<List<MerchantLocationModel>> locations;
>
>        public Something()
>        {
>            locations = new Lazy<List<MerchantLocationModel>>(
>                () => { return GetList(); }
>                );
>        }
>
>        public List<MerchantLocationModel> Locations
>        {
>            get { return locations.Value; }
>        }
>
>        public List<MerchantLocationModel> GetList()
>        {
>         TDCDataProvider provider = new TDCDataProvider();
>            return provider.GetMerchantLocations(RecordId);
>        }
>    }
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform