Can I pass constructor parameters to Unity's Resolve() method?
As of today they have added this functionality:
It’s in the latest drop here:
http://unity.codeplex.com/SourceControl/changeset/view/33899
Discussion on it here:
http://unity.codeplex.com/Thread/View.aspx?ThreadId=66434
Example:
container.Resolve<IFoo>(new ParameterOverrides<Foo> { { "name", "bar" }, { "address", 42 } });"
configure Unity to resolve a constructor parameter and interface
var container = new UnityContainer();
var phone = "214-123-4567";
container.RegisterType<IFaxProvider, EFaxProvider>();
container.RegisterType<IFaxService, FaxService>(new InjectionConstructor(phone, typeof(IFaxProvider)));
var fax = container.Resolve<IFaxService>();
How resolve a dependency with unity passing arguments in the constructor?
Create an InjectionConstructor
and pass that to RegisterType()
:
var unityContainer = new UnityContainer();
ISnuh snuh = new Snuh();
InjectionConstructor injectionConstructor = new InjectionConstructor(snuh);
unityContainer.RegisterType<ICalculator, SimpleCalculator>(injectionConstructor);
If you have to do it only when you resolve the type, that's when ResolverOverride[]
would be used:
public static T Resolve<T>(this IUnityContainer container, params ResolverOverride[] overrides);
Per MSDN:
Use ParameterOverride to override the specified constructor parameter
or parameters.
Unity – Resolving objects by passing constructor parameter
You should register Parent1
with dependency to Document
in constructor:
Container.RegisterType<IParent, Parent1>("Parent1", new InjectionConstructor(doc));
then you can resolve an instance of that class:
string parentClass = "Parent1";
IParent parent = Container.Resolve<IParent>(parentClass);
How to send a parameter to an object's constructor with Unity's Resolve() method?
Here's a related question that answers this pretty well:
Can I pass constructor parameters to Unity's Resolve() method?
The only option you have if you want to do this is a scoped container.
IUnityContainer subContainer = this.container.CreateScopedContainer();
subContainer.RegisterInstance<Customer>(customer);
smartFormPresenter1 = subContainer.Resolve<SmartFormPresenter>();
How to pass a type as parameter to a constructor when using Unity dependency injection
The answer was contained in the code from the link posted by Haukinger:
object actual =
container.Resolve<IDatarecordSerializer>(
new ParameterOverride(
"type",
new InjectionParameter(typeof(string))
)
);
How to pass runtime value to constructor parameter when register a type?
Within the InjectionFactory
method, use a Resolve
method overload that allows you to specify a ResolverOverride
, specifically a ParameterOverride
, and pass the runtime value in that way. To avoid a StackOverflowException
, you can use an additional named registration with compile-time values provided for the relevant parameters, like this:
class A
{
public A(int id, B b, C c)
{
Trace.WriteLine("Got " + id);
}
}
class B { }
class C { }
static void Main(string[] args)
{
using (var unity = new UnityContainer())
{
unity.RegisterType<A>(
"compile-time",
new InjectionConstructor(-1,
new ResolvedParameter<B>(),
new ResolvedParameter<C>()
)
);
unity.RegisterType<A>(
new InjectionFactory(
u => u.Resolve<A>("compile-time",
new ParameterOverride("id", new Random().Next()))
)
);
unity.Resolve<A>();
unity.Resolve<A>();
unity.Resolve<A>();
}
}
Resolve with parameter from other constructor with unity
I register all my types at service startup, but at startup i do not
know the "customerName"-parameter
In other words, your customerName
parameter is runtime data. Injecting runtime data into components during the components' initialization is an anti-pattern.
There are two possible solutions to your problem, but in your your case, the most likely solution is to pass through the parameter through the public API as follows:
public class Service
{
private readonly IManager _manager;
public Service(IManager manager) {
_manager = manager;
}
public void ServiceCall(string customerName) {
_manager.DoSomething(customerName);
}
}
Here the IManager
interface is changed so that the customerName
is passed through the DoSomething
method. Because the runtime value isn't needed anymore during construction, there is no need to inject the Unity container into the Service
(which is a form of the Service Locator anti-pattern).
For the second option, please read this article.
Related Topics
Getting All File Names from a Folder Using C#
C# Sha-1 VS. PHP Sha-1...Different Results
ASP.NET Page Is Not Loading CSS Styles
Simple iOS Bluetooth Data Transmission Using Unity
Is There a Standard Way to Encode a .Net String into JavaScript String for Use in Ms Ajax
Using Image Control in Wpf to Display System.Drawing.Bitmap
Compare Two Datatables to Determine Rows in One But Not the Other
Enforce an Async Method to Be Called Once
Query an Xdocument for Elements by Name at Any Depth
How to Return Text from Native (C++) Code
Load CSS Dynamically in ASP.NET
Executing R Script Programmatically
Calling a C# Library from Python
How to Use the Cancellationtoken Property