Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Lazy Class Usage
Message
De
27/10/2010 04:24:51
 
 
À
26/10/2010 21:24:29
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01486990
Message ID:
01487086
Vues:
45
>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.

Hi,
I guess you could just put a lock around it:
get
            {
                lock (new object())
                {
                    if (_Locations == null)
                    {
                        TDCDataProvider provider = new TDCDataProvider();
                        _Locations = provider.GetMerchantLocations(RecordId);
                    }
                    return _Locations;
                }
            }
Downside being that the lock is created on every access but you could get round that by adding a check for _Locations==null before the lock as well:
get
            {
                if (_Locations == null)
                {
                    lock (new object())
                    {
                        if (_Locations == null)
                        {
                            TDCDataProvider provider = new TDCDataProvider();
                            _Locations = provider.GetMerchantLocations(RecordId);
                        }
                    }
                }
                return _Locations;
            }
by which point Lazy starts to look more attractive :-}

>
>>>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);
>>        }
>>    }
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform