开发者

StructureMap constructor injection with named maps

开发者 https://www.devze.com 2023-03-11 18:42 出处:网络
In my object factory initialization, I have something like this: ObjectFactory.Initialize(factory=>

In my object factory initialization, I have something like this:

ObjectFactory.Initialize(factory=>
{
    factory.For<User>().Add((IContext context) => 
        (User)Session["Current"]).Named("CurrentUser");
    factory.For<User>().Add((IContext context) => 
        new User()).Named("NewUser");
});

Then in my controllers, I want to be able to do something like this:

public MyController(User CurrentUser) { ... }

public MyOthercontroller(User NewUser) { ... }

Where some aspect of definition of the constructor tells StructureMap which named instance to use. I've been looking around to see if I can somehow get the name of the parameter out of the IContext (doesn't seem to be possible), or use the nam开发者_如何学编程e of the parameter as the name of the instance (doesn't seem to be possible).

this question seems to describe a way of doing this by setting up the mapping for each controller as part of initializing the object factory. I'd prefer something a bit more convention-oriented, rather than having to explicitly configure it.

Is such a thing possible?


There is no good way to do it with StructureMap, but I would argue that you probably shouldn't do it with StructureMap. MyOtherController should just create the User instance itself (or delegate to an IUserSource if there is more involved).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号