I have a group of services that get injected into my controllers. Can my abstract class constructor instantiate new instances of commonly used objects?
public class ReportService : AReportService, IReportService
{
private readonly IMyService _myservice;
public ReportService(IMyService myService) : base ()
{
_myservice = myService;
}
public void MyMethodForThisService(string someProperty)
{
_parentService.DoSomething(someProperty);
}
}
public abstract class AReportService
{
protected readonly ParentService _parentService;
protected AReportService()
{
_parentService = new ParentService();
}
protected void MyFirstSharedMethodForAllServices(string someProperty)
{
_parentService.DoSomethingElse(someProperty);
}
}
Can my abstract class constructor instantiate new instances of commonly used objects?
Absolutely. The constructor for an abstract class can do basically anything a normal constructor can. The only restriction is that you can only call the constructor as part of chaining from another constructor.
See more on this question at Stackoverflow