Example StructureMap factory instance

I am trying to register a factory method to create instances of a public generic type MongoCollection<>

. However, when I GetInstance

feel like it is using MongoCollection constructor instead of 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 line GetInstance

, it never hits a breakpoint inside the factory method, but it gives a message StructureMapException

that says "There is no default instance defined for PluginFamily MongoDb.Driver.MongoServerSettings." There is a constructor for MongoCollection

that accepts MongoServerSettings

, but I don't want the struct structure to use this constructor, I want it to use my factory method.

Any ideas why it doesn't use a factory method? This is mistake?

+3


source to share


1 answer


I searched the repository to view the code and figured out his specific error. I have fixed the bug and submitted a pull request , hopefully it will be complex and released soon.



+5


source







All Articles