StructureMap instance factory method
-
29-05-2021 - |
Frage
I'm trying to register a factory method for creating instances of an open generic type MongoCollection<>
. However, when I GetInstance
it appears that it is using a constructor of MongoCollection instead of the factory method.
var mongo = new MongoConfiguration("mongodb://localhost", "test");
For(typeof (MongoCollection<>)).Use(c =>
{
var requestedType = c.BuildStack.Current.RequestedType; // set breakpoint here
var type = requestedType.GetGenericArguments()[0];
return mongo.GetCollection(type);
});
Then I do
ObjectFactory.GetInstance<MongoCollection<User>>();
When I run the GetInstance
line it never hits the breakpoint inside the factory method, but it throws a StructureMapException
saying "No default instance defined for PluginFamily MongoDb.Driver.MongoServerSettings". There is a constructor for MongoCollection
that takes a MongoServerSettings
, but I don't want structure map to use that constructor, I want it to use my factory method.
Any ideas why it isn't using the factory method? Is this a bug?
Lösung
I forked the repository to browse the code and realized its definitely a bug. I fixed the bug and sent a pull request, hopefully it will be merged and released soon.