Factory with MEF

Apr 10, 2011 at 9:46 PM

Hi,

I'm using MEF in a project and I've run into a problem which MEF does not easily work with.

The thing is I want to create a new instance of a class which has dependencies that needs to be injected to it, and also pass in some parameters.

I prefer to pass the parameters in the ctor and have them store in readonly fields to make sure my instance is immutable.

 

I've search about it and I've found google guice page that show exactly this problem and how they solve it.

Off course this solution require changes to MEF.

 

What do you think?

Thank you,

Ido.

Apr 10, 2011 at 10:19 PM

It's a common problem known as parameterized construction, it is just not something that we were focused on for our initial extensibility scenarios. There is a way to address this through a pattern of using a state service. I've got a codepaste that illustrates how you can use this for passing specific instance state into view models here: http://codepaste.net/vvjj3z This approach will work with any IoC. Not sure it matches your scenario exactly, but it might.

Glenn

Apr 11, 2011 at 7:49 AM

The solution you suggest is not matches exactly my problem.

Google's factory pattern (even though I have to create manually) is better. That is export the artifact factory class which import any required parts and have it create a new instance of the product class pass in both the imported parts and the current state.

 

I find much resemblance between MEF and Guice - I actually use Guice documentation and suggested ways of use on my MEF project.

 

Thank you,

Ido.