Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Lazy Class Usage
Message
De
27/10/2010 10:24:04
 
 
À
27/10/2010 04:24:51
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01486990
Message ID:
01487150
Vues:
32
OK, I've used that double-check locking pattern in some of my classes. I thought that was all that was necessary, but wanted to make sure I wasn't missing anything.

>by which point Lazy starts to look more attractive :-}

There was something I didn't like about the Lazy class when I first read about it not too long ago ... but right now, off the top of my head, I can't remember what it was. It's a b*tch getting old!

~~Bonnie



>>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);
>>>        }
>>>    }
Bonnie Berent DeWitt
NET/C# MVP since 2003

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

Click here to load this message in the networking platform