public interface ITaskProvider
{
T GetTask<T>();
}
In the implementation of ITaskprovider below, as you see the IUserTask and IIdentityTask is being injected from property instead of constructor. The reason is that Windsor automatically instantiates the injected properties on runtime when accessed so that i dont have to put all the must injected dependencies in the constructor.
public class TaskProvider : ITaskProvider
{
public IUserTasks UserTasks { get; set; }
public IIdentityTasks IdentityTasks { get; set; }
public T GetTask<T>()
{
Type type = typeof(T);
if (type == typeof(IUserTasks)) return (T)this.UserTasks;
if (type == typeof(IIdentityTasks)) return (T)this.IdentityTasks;
return default(T);
}
}
In the controller I am injecting the ITaskProvider in the constructor.
public ITaskProvider TaskProvider { get; set; }
public AuctionsController(ITaskProvider taskProvider)
{
TaskProvider = taskProvider;
}
And here i call the taskprovider and its methods fine.
public ActionResult Index()
{
var userTasks = TaskProvider.GetTask<IUserTasks>();
var user = userTasks.FindbyId(guid);
}
Up to here, everything works fine.
I have been told that this is more like a service locator pattern and is violating dependency injection pattern and i want to know what is violating here.
For me, There is no violation against DI in your code, regarding wikipedia:
core principal to separate behavior from dependency resolution
But the bad side your controller has too much knowledge, in some cases (if you don't programs carefully) you may violate the Law Of Demeter
take a look at your code:
public ActionResult Index()
{
var userTasks = TaskProvider.GetTask<IUserTasks>();
var user = userTasks.FindbyId(guid);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With