How can I use Dependency Injection to either Override a method or to set a default method when no dependency is explicitly injected?
NickName:UberAffe Ask DateTime:2015-06-11T05:36:35

How can I use Dependency Injection to either Override a method or to set a default method when no dependency is explicitly injected?

I'll explain the reason I am trying to do this first. My Initial Hierarchy:

BaseClass
   |
ControlClass
   |     \
CalltoBase  DerivedClass1
              |
            CalltoBase

Both call paths hit a particular method of the base class Depending on where the CalltoBase originates I need to use a different implementation of that one method. This is what I normally would have done:

BaseClass____DerivedClass2
   |              |      
ControlClass   ControlClass2
   |    \             |    \
 call DerivedClass1  call  DerivedClass3
          call                 call

The problem is that this project is growing and there will be necessity for even more implementations of that one method in the base class. Hence my reason for wanting to do something like this, I would like to only have to create 1 extra derived class instead of three every time I run into this.

BaseClass
  |      \______
ControlClass<t> \_____           
 |    \               \
call DerivedClass1<t>  tClass
       call

Note: every line except the call lines are inheritance I do not have the authority or the time to change the system so that they would not be inheritance.

my goal is to do something like this:

public class BaseClass{code}
public class tClass : BaseClass {code}
public class ControlClass<t> : t where t : BaseClass {code}
public class DerivedClass1<t> : ControlClass<t> where t : BaseClass {code}
//defaults
public class ControlClass : ControlClass<BaseClass> {empty}
public class DerivedClass1 : ControlClass<BaseClass> {empty}

Is something like this possible or should I just accept that I'm screwed and make all of the derivative classes every time?

Edit1***************************************** After doing some more research I think my real question is: How can I use Dependency Injection to either Override a method or to set a default method when no dependency is explicitly injected?

Edit2******************************************

This code is all inside of a library that the company created and is used by many projects. It is for UAT's using selenium/specflow.

public class BaseClass
{
   public T Control<T>(string controlName, string pageName = null) 
                      where T : class
   {
      By sel = GetSelector(controlName, pageName)
      ...
      return control as T;
   }
   private By GetSelector(string controlName, string pageName = null)
   {
      pageName = string.IsNullOrEmpty(pageName) ?
                 pageMapping[Browser.GetPage()] : pageName;
      ...
      return selector;
   }
}
public class ControlClass : BaseClass
{
   public NgElement NgControl(string controlName, string pageName)
   {
      By sel = GetSelector(controlName, pageName);
      ...
      return element;
   }
   public By GetSelector(string controlName, string pageName)
   {
      pageName = string.IsNullOrEmpty(pageName) ? PageName : pageName;
      pageName = string.IsNullOrEmpty(pageName) ?
                            pageMapping[Browser.GetPage()] : pageName;
      ...
      return selector;
   }
   public string PageName
   {
      get
      {
         ...
         return returnPage.Value;
      }
   }
}
public class DerivedClass1 : ControlClass
{
   //SpecFlow GWT's
   GivenAngularClicks(string controlName)
   {
      WaitForAngular();
      NgControl(crontrolName).Click();
   }
}

The parts that change pageName are the parts that need to be modified based on which project is the caller. It doesn't matter which derived class makes the call, what matters is the class that instantiates base class.

Copyright Notice:Content Author:「UberAffe」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30767737/how-can-i-use-dependency-injection-to-either-override-a-method-or-to-set-a-defau

Answers
comdiv 2015-06-10T21:48:40

I think it's better in your case make more complicated base class - your hierarchy looks like inhertiance used to provide some helper and secondary functionality. May be better to split it horizontally to services and make aggregates instead of making hard-to-understand mixed generic hierarchy?\n\nMay be u use only .ctors in classes to initialize and it can be changed to Factory?\n\nWhy you so sure that u must to have split-and-then-merge hierarchy?\n\nBad design cause bad questions. You can do anything with inheritance and generics, but think better if you have to do this...",


Bart van der Drift 2015-06-11T07:38:27

Shouldn't something like this be possible by using overrides, in which you call different methods on the base class? Something like:\n\npublic class BaseClass\n{\n public virtual void FunctionA() { ... }\n public void FunctionAVariant1() { ... }\n public void FunctionAVariant2() { ... }\n}\n\npublic class DerivedClass1 : BaseClass\n{\n public override void FunctionA()\n {\n base.FunctionAVariant1();\n }\n}\n\npublic class DerivedClass2 : BaseClass\n{\n public override void FunctionA()\n {\n base.FunctionAVariant2();\n }\n}\n",


More about “How can I use Dependency Injection to either Override a method or to set a default method when no dependency is explicitly injected?” related questions

How can I use Dependency Injection to either Override a method or to set a default method when no dependency is explicitly injected?

I'll explain the reason I am trying to do this first. My Initial Hierarchy: BaseClass | ControlClass | \ CalltoBase DerivedClass1 | CalltoBase Both call paths...

Show Detail

How to use dependency injection correctly? Because using methods of injected class creates new dependency

I'm a self-learner, trying to become a good PHP developer and want to fully understand Dependency Injection. I understand the broad principle, in theory dependency injection makes my classes not h...

Show Detail

Dependency injection in Constructor

Suppose that I have Spring service classes or JSF beans. I wire these classes in another class. There are no problems till now. I can use these injected fields in any method. But, using them in the

Show Detail

When is a dependency injected object initialized in WPF?

I'm trying to wrap my head around the dependency injection concept in WPF. If I have a Dialog that has a ViewModel defined as being dependency injected, at what point during runtime is the dependency

Show Detail

Dependency Injection using Template 10

I am trying to migrate some code from an old Windows 8.1 app that I had developed using Prism/Unity to a new UWP app using Template 10 and Unity. I have seen in the documentation for Template 10 here

Show Detail

Use dependency injection in static class

I need to use Dependency Injection in a static class. the method in the static class needs the value of an injected dependency. The following code sample demonstrates my problem: public static c...

Show Detail

Use dependency injection in static class

I need to use Dependency Injection in a static class. the method in the static class needs the value of an injected dependency. The following code sample demonstrates my problem: public static c...

Show Detail

How to use Dependency Injection (unity) when dynamically invoke method in a dll?

I have a project which dynamically invokes methods from dlls. The dll names are defined in a configuration file. Below, it is the code to invoke the method 'Run'. Assembly a = Assembly.LoadFile(fu...

Show Detail

proper use of dependency injection

I've been using dependency injection, and I noticed that in the constructors (that you are injecting to), the parameters are usually an interface or a base class. I think I understand the reason w...

Show Detail

NestJS Dependency Injection with Constructor parameters in injected dependency

I am struggling in NestJS with the Dependency Injection, for a service, that is injected into another service, that is then used by a controller. Controller - ReturnResults &lt;- Injected Servi...

Show Detail