-
Notifications
You must be signed in to change notification settings - Fork 36
Func
remogloor edited this page Feb 25, 2012
·
7 revisions
The second way to add factories is to inject a Func to the class that needs to create a new object. Like the factory interface it supports arguments too. But since a Func gives you absolutely no information about the parameters you have to pass, I recommend not using Func unless it takes no arguments. Even if it takes no arguments I personally think factory interfaces is the cleaner way to add factories. You have to write more code but the readability is better than with Func.
public class Foo
{
private Func<Bar> barFactroy;
public Foo(Func<Bar> barFactory)
{
this.barFactory = barFactory;
}
public void Do()
{
var bar = this.barFactroy();
...
}
}
That’s everything you have to do in this case. There is no need to create a special binding for the Func.