How To: Pass argument to factory method

Jun 16, 2010 at 9:11 PM

I have a factory method that creates a new instance of my class.  In order for this to work, I need to pass in the unique identifier of the record to retrieve from the database that contains the property values for the object.  As a result, the factory method looks like:

public static MyObject GetMyObject(Guid id) { ... }

If I eliminate the "id" parameter, I can easily create the object from the Unity container using InjectionFactory when I register the type.  But how do I pass an argument into the factory method when I want a new instance?

 

Along those same lines, what is the last argument passed into the factory method?  The InjectionFactory constructor expects a method or delegate of type: System.Func<IUnityContainer, object>, or System.Func<IUnityContainer, Type, string, object>  I haven't been able to find any documentation on these parameters.  (Can you say XML comments!!!)  Perhaps the last argument is what I'm looking for?  If so, how do I specify what value to pass in?

 

 

 

Jun 16, 2010 at 10:17 PM

Func<IUnityContainer, object> - go read the docs for Func<T1, T2>. The object is the return type, not a parameter.

The second overload, Func<IUnityContainer, Type, string, object> gets passed the actual type and name that were being passed to Resolve.

You're better off creating an actual factory class that exposes the method you need, and resolving that factory object explicitly.

 

Jun 17, 2010 at 2:53 PM

Yea, I realized my brain-fart on the Func statement last night but wasn't able to get back on and update my post.

Any chance you can post an example of your solution?