I'm very new to autofac so it's possible that I'm completely misusing it.
Let's say I have a class that has this structure:
public class HelperClass : IHelperClass
{
public HelperClass(string a, string b)
{
this.A = a;
this.B = b;
}
}
and I have two classes that use that class, but require different defaults for the constructor. The second constructor is JUST for testing purposes -- we will always want a HelperClass in the "real" app.:
public class DoesSomething: IDoesSomething
{
public DoesSomething()
: this(new HelperClass("do", "something"));
{
}
internal DoesSomething(IHelperClass helper)
{
this.Helper = helper;
}
}
public class DoesSomethingElse : IDoesSomethingElse
{
public DoesSomethingElse()
: this(new HelperClass("does", "somethingelse"));
{
}
internal DoesSomethingElse(IHelperClass helper)
{
this.Helper = helper;
}
}
Here's my AutoFac module:
public class SomethingModule: Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<DoesSomething>().As<IDoesSomething>();
builder.RegisterType<DoesSomethingElse>().As<IDoesSomethingElse();
}
}
My question(s):
- When I call resolve on DoesSomething or DoesSomethignElse -- will it resolve the internal constructor instead of the public one? Do I need to leave IHelperClass unregistered?
- If yes, how do I make it pass different parameters to each instance of IHelperClass depending on whether it's used in DoesSomething or DoesSomethingElse?
IHelper
. If so, you'd better make the dependencies visible. Why don't you just remove your parameterless constructor, make the other one public and have your module configuration provide required parameters. You can useWithParameter
as Daniel Hilgarth has suggested.DoesSomething
andDoesSomethingElse
classes' constructorsinternal
and usingFindConstructorsWith
on registration to allow Autofac to resolve them. Then you'll be sure that in partial trust noone outside your assembly will create your classes, but everyone will be able to resolve the classes and use them. Yet, in full trust this won't work. Nothing will. Hope this helps.