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:
C#
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 SinghRank #66 · 26.2K reputationPosted Aug 31, 2021, 3:40 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted Aug 31, 2021, 4:24 AM EDT
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;
}
Sachin SinghRank #66 · 26.2K reputationPosted Aug 31, 2021, 4:31 AM EDT
Adalat KhanRank #1,143 · 1.6K reputationPosted Aug 31, 2021, 4:17 AM EDT
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