Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1052579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:06:58+00:00 2026-05-16T17:06:58+00:00

public interface ITaskProvider { T GetTask<T>(); } In the implementation of ITaskprovider below, as

  • 0
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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:06:59+00:00Added an answer on May 16, 2026 at 5:06 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given: public interface IBatchProcess { void Run(); } and multiple implementation of: public class
From the javadocs : public interface Cache<K,V> extends Function<K,V> { //... void invalidate(Object key);
Example Interface: public Interface IEntity Property ID() as Integer end Interface I want all
Given the interface below: public interface ITest { string PropTest1 { get; set; }
I often see: public interface IFoo<TWidget> where TWidget : ISomethingElse { } In this
public Interface Foo<T extends Colors>{...} Is there a way to retrieve which T was
public interface ITest { void Somethink(); } public class Test1 : ITest { public
public interface IBasePresenter { } public interface IJobViewPresenter : IBasePresenter { } public interface
public interface Proposal { public static final enum STATUS { NEW , START ,
I have interface Foo public interface Foo { public void test(); } Class FooChild

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.