c# class inherits from base class with constructor with dependency injection - c#

I have a project using Dependency Injection (Ninject) where I have the following class:
public class SecurityService : BaseService
{
ISecurityRepository _securityRepo = null;
public SecurityService(ISecurityRepository securityRepo)
{
_securityRepo = securityRepo;
}
}
Because BaseService is going to be referenced in many other service classes I wanted to add there a method that also go to Data Repository and get some information so I don't have to repeat the same code along the other service classes.
Here is what I have for BaseRepository:
public partial class BaseService
{
IEntityRepository _entityRepo = null;
public BaseService(IEntityRepository entityRepo)
{
_entityRepo = entityRepo;
}
public Settings AppSettings
{
get
{
return _entityRepo.GetEntitySettings();
}
}
}
But when I compile I get the following error:
There is no argument given that corresponds to the required formal parameter 'entityRepo' of 'BaseService.BaseService(IEntityRepository)'
And the error make sense because now I have a constructor that I guess is expecting something.
Any clue how to fix this but that I can still have my dependency injection in BaseRepository class?
UPDATE
I just tried to remove the constructor and use the attribute [Inject] but when debugging I see that _entityRepo is NULL.

Add the dependency to the constructor for the derived class, and pass it along.
public SecurityService(ISecurityRepository securityRepo, IEntityRepository entityRepo)
: base(entityRepo)
{
_securityRepo = securityRepo;
}

I could make it work:
I just convert the private property to be public and then [Inject] attribute started to work.
public partial class BaseService
{
[Inject]
public IEntityRepository EntityRepo { get; set; }
}

Pass the Repository object to the base class via the child class constructor:
public SecurityService(ISecurityRepository securityRepo) : base(IEntityRepository)
{
//Initialize stuff for the child class
}

Related

How to use dependency injection in a method or property?

Normally I use Dependency injection in the constructor of a class.
The dependencies are registered in the unity container, f.e.:
container.RegisterType<IMyInterface, MyClass>();
But in one class it is not possible to inject with the constructor (it's a third party class which I inherit from and the default constructor (without injection) MUST be used.
How can I use my MyClass in this class too?
Thanks!
If the main constraint is calling the default constructor of the 3rd party class, can't you do something like this?
public class InheritedClass : ThirdPartyClass
{
private IMyInterface _myInterface;
// the default ctr of the 3rd party class is still being called
public InheritedClass(IMyInterface myInterface, string arg) : base(arg)
{
_myInterface = myInterface;
}
}
In case you actually mean your own inherited class has a certain constructor that you have to use, then why not do something like this ?
public class InheritedClass : ThirdPartyClass
{
// Constructor that has to be used
public InheritedClass(string arg) : base(arg)
{
}
// Ctr with DI that also calls the required default ctr - could be used instead..
public InheritedClass(IMyInterface myInterface, string arg) : this(arg)
{
_myInterface = myInterface;
}
}
In case none of these apply to your case, what you're looking for is the concept of Method Injection, an example could be this one
I guess I don't understand how this is hard.
(it's a third party class which I inherit from and the default constructor (without injection)
Assume this is a 3rd Party Class that you inherit from:
public abstract MyClassBase
{
public MyClassBase()
{
}
public abstract void DoSomething();
}
What prevents you from deriving and injecting?
public MyClassDerived
{
private IInjectioned _injection;
public MyClassDerived(IInjectioned injection)
// Call default constructor without params
: base()
{
_injection = injection;
}
public override void DoSomething()
{
_injection.DoSomething();
}
}

Making Microsoft Unity dependency properties available in constructors

I've got the following bit of (simplified) code:
public abstract class BaseClass
{
[Dependency]
public IRequiredService RequiredService { get; set; }
protected string RequiredParameter { get; private set; }
public BaseClass(string requiredParameter)
{
this.RequiredParameter = requiredParameter;
}
}
public class DerivedClass : BaseClass
{
public DerivedClass(string requiredParameter) : base(requiredParameter)
{
RequiredService.DoSomething(); //this will fail!
}
}
In other words, I'd like to have access to the Unity-filled RequiredService in the constructor... but that's impossible, since that property hasn't been filled by Unity yet. I COULD add the IRequiredService as a required constructor parameter, but then I'd need to refactor every constructor of every derived class to also include that parameter.
I was wondering if there's a better way.
In short, I'd like to run a bit of code after a class has been constructed and after unity has filled all the class' properties marked with the [Dependency] attribute.
Is there a simple way to do this?
Instead of putting RequiredService.DoSomething(); in the constructor you can put it in a inside a [InjectionMethod] call, this will allow you to reliably know that RequiredService has been populated.

How to retrieve/import object using MEF in Prism

I have a requirement to have one object (RuleFile), representing file to be serialized, across the whole application like word (*.docs) file that will be associated with my application.
I am using Prism 5 along with MEF as a dependency injection container.
[Export]
[Serializable()]
public class RuleFile : NotificationBase, IRuleFile { }
Now i have decorated the object with [Export] and trying to import it in one of the MyViewModel but it is giving null.
public class MyViewModel : ViewModelBase
{
[Import]
private RuleFile RuleFile; // 'null' coming here
}
Kindly direct me what am i missing? Or tell me any other way to best handle this scenario.
Are you checking the value in the constructor? Imports that are decorated directly on the property are resolved after the constructor. If you want to access the RuleFile in the constructor you'd need to set it up like this
public class MyViewModel : ViewModelBase
{
public RuleFile RuleFile { get; set; }
[ImportingConstructor]
public MyViewModel(RuleFile ruleFile)
{
RuleFile = ruleFile;
}
}
Alternatively, you can implement the IPartImportsSatisfiedNotification which will give you a notification method to signify that the imports have been resolved. Like this
public class MyViewModel : ViewModelBase, IPartImportsSatisfiedNotification
{
[Import]
public RuleFile RuleFile { get; set; }
public void OnImportsSatisfied()
{
// Signifies that Imports have been resolved
}
}

Why set up a constructor in a class to set a base parameter when I could do the same in a class it inherits from?

I have the following code:
public abstract class ApiBaseController : ApiController
{
protected IUow Uow { get; set; }
}
and:
public class ContentStatusController : ApiBaseController
{
public ContentStatusController(IUow uow)
{
Uow = uow;
}
}
Is there any reason why I could not code the assignment of Uow (using IOC) in the ApiBaseController?
The reason I ask is because I am trying to do something similar to the Code Camper application sample and I notice that in that sample the Unit of work is assignment is always performed in the constuctors of the controllers themselves rather than in the ApiBaseConstructor. In the examples I see this is the only thing that's done in the constructors.
If I did the assignement in the base controller then how could I code that and would Uow still need to have "protected" for it to be available in controllers that inherit from the ApiBaseController?
Your IOC container is injecting dependencies via constructors. If you want to continue to use that mechanism (some containers allow e.g. property injection, but not everyone likes to do that) then you'll still need to have the same constructor in your derived class to pass the injected component down to the base class1.
Something like:
public abstract class ApiBaseController : ApiController
{
public ApiBaseController(IUow uow)
{
Uow = uow;
}
protected IUow Uow { get; private set; }
}
public class ContentStatusController : ApiBaseController
{
public ContentStatusController(IUow uow) : base(uow) //<-- This is needed
{
}
}
1Because classes don't inherit constructors.

Autofac. Base Class properties resolved without specific injection

I'm looking for a possible solution to the following.
I have a base class that has a dependency which I currently use property injection to satisfy.
public class BaseClass {
IService SomeService { get; set; }
}
I have multiple classes which inherit from this base class.
public class DerivedClass : BaseClass, IDerivedClass {
}
And I inject using the following.
builder.RegisterType<DerivedClass>().As<IDerivedClass>().OnActivated(e => e.Instance.SomeService = e.Context.Resolve<IService>());
I do this for about 12 other classes which extend the base class. Is there a way so that any class extending BaseClass will get my IService registration without setting up an Activation event for each registration? It's working fine like this, but would just like to clean up the registrations.
Just use constructor injection. Create constructor with parameters for your class like
public DerivedClass(IServiceOne service1, IService2 service2)
{
this._service1 = service1;
this._service2 = service2;
}
and make autofac doing his job automatically, like
builder.RegisterType<T>();

Categories

Resources