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 :-}
>>>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"); >>> } >>> } >>>}>>>
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); >> } >> }