0

I have created one Parent library where in, I have a abstract class,and I have another child library where I have inherited the parent library, now I'm consuming this child library in web API controller, so, I would be consuming this in service layer,however, I'm not sure how to use it correctly, secondly I have all, the methods inside this library as protected, please guide how to do dependency injection for abstract class or do I need to create another interface or something for DI?

Code:

// Parent library
public abstract class Page
{
       protected abstract void ImplementAbstractMethod();
}

// Child library
public class ImplementPage : Page
{
    protected override void ImplementAbstractMethod()
     {
        // Implementation... 
     }
}


// Web API 
public class ServiceController 
{
    public ServiceController( ????) // How to do dependency injection, should I use abstract class, if yes then how to consume protected method, or, do I need to make it public?
    {
     }
}

// Or In business layer

public class Service
{
    public Service( ????) // How to do dependency injection, should I use abstract class, if yes then how to consume protected method, or, should I inherit the child library??
    {
     }
}
11
  • You should use the base classes only in the same child and provide other methods publicly to other consumers and put the ImplementPage in the DI Container and inject it into your controller Commented Sep 11, 2022 at 4:00
  • @MohammadAghazadeh You mean something like this public ServiceController(Page pg) ?? Commented Sep 11, 2022 at 4:08
  • no see this maybe helpful : stackoverflow.com/questions/52982560/… Commented Sep 11, 2022 at 4:12
  • @MohammadAghazadeh That's confusing, can you show it by implementing it in my use case? That would be really helpful Commented Sep 11, 2022 at 4:16
  • But if you are only going to use the abstract class to create implementation rules, I think you should use the interface, otherwise, make public abstract methods in parent class public abstract void ImplementAbstractMethod(); Commented Sep 11, 2022 at 4:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.