Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StructureMap instance factory method

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?

like image 641
kelloti Avatar asked Aug 03 '26 06:08

kelloti


1 Answers

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.

like image 121
kelloti Avatar answered Aug 07 '26 01:08

kelloti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!