Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Lazy Class Usage
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01486990
Message ID:
01487001
Vues:
77
>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