Hi,
I have a question.
Why we declare an object from a class or interface as property object? whats the logic behind this. Why we not declare an object instead of property?. Following is the example:
public interface ICacheManager
{
T GetData(CacheDataType type, string location = null, string client = null, string customKey = null);
Task GetDataAsync(CacheDataType type, string location = null, string client = null, string customKey = null);
List GetList(CacheDataType type, string location = null, string client = null, string customKey = null);
Task GetListAsync(CacheDataType type, string location = null, string client = null, string customKey = null);
void ReloadData(string location = null, string client = null);
void ReloadData(CacheDataType type, string location = null, string client = null);
void StoreData(CacheDataType type, T data, TimeSpan? slidingExpiry = null, string location = null, string client = null, string customKey = null);
}
//Implementation class
public class LocationService : ILocationService
{
ICacheManager _cacheManager { get; set; }
public LocationService(IConfigManager configManager)
{
_cacheManager = cacheManager;
}
}
In the above class LocationService we declared a property object from the interface ICacheManager. Why we not declare an object instead of property? Whats the purpose behind this?

Sachin SinghPosted Aug 31, 2021, 8:31 AM
Sachin SinghPosted Aug 31, 2021, 8:24 AM
Actually, is bad practice to create a property in your case, property means both getter and setter. you don't need a getter/setter while implementing DI. it is the UI that will pass the concrete object, so in your Location Service, a property doesn't make any sense to me.
so instead of
ICacheManager _cacheManager { get; set; }
you should have used this
private ICacheManager _cacheManager2;
private ICacheManager _cacheManager2;
public LocationService(IConfigManager configManager)
{
_cacheManager = cacheManager;
}
Adalat KhanPosted Aug 31, 2021, 8:17 AM
Hi Sachin,
Thanks for your great reply but my question was why we declared an object property from ICachManager Interface?. Please explain the bellow point:
ICacheManager _cacheManager { get; set; }
public LocationService(IConfigManager configManager)
{
_cacheManager = cacheManager;
}
what is the difference between the following in my case:
ICacheManager _cacheManager { get; set; }
ICacheManager _cacheManager2;
Property object and object only
Sachin SinghPosted Aug 31, 2021, 7:40 AM