开发者

StructureMap grouping of named instances

Long post - sorry....

I'm doing input validation for a WCF service and using StructureMap IoC to instantiate the appropriate validation objects. I have 2 different validation groups:

  1. Per object validation: means that one input parameter, will be resolve by the Ioc (e.g. Ioc.ResolveAll<IValidatorObject<InputParameter1>, .... <Inpu开发者_如何学CtParameter2>... etc). If any rules are found, the validate method is invoked.
  2. Per context validation: mean that validation rules are invoked, based on the current context (explicit roles). A context could be 'deposit money' or 'open bank account'. Context validation are usually dependent on 2 or more of the input parameters and is the key difference between object and context validation.

The input validation is performed in the BeforeCall event call in the IParameterInspector (provider/server side!). With this event I get a string containing the operation name (aka. the context) and an object[] with the input parameters.

The problem is that there's multiple validation rules for a single context and the only way I have figured out to register the context in the Ioc, is by using named intances. However I can only register 1 named instance pr. interface. And the interface is not uniquely identifiable by its signature. E.g.

  • Context rule for 'create account': IValidatorContext<User, Account>
  • Context rule for 'deposit money': IValidatorContext<User, Account>

So my question is, whether it is possible to register the context in StructureMap in any other way than named instances - or maybe a way to group named instances.

An alternative route, is to implement explicit interfaces for each context, so that the DepositMoney service method might look like this:

public Response Deposit(IDepositMoney inputArguements)

where IDepositMoney contains the input parameters.

So am I way off here, or can I somehow register a context in StructureMap? Or should I just go ahead and use explicit interface instead (3rd option?)

Thanks in advance


Ended up wrapping each set of input parameters in a context and used the context to register in StructureMap. Works like a charm!


The whole idea of named instances is that the name points to a single instance, so you won't be able to use that feature to do what you are trying to achieve. I would use explicit interfaces, since this will allow you to auto wire more things and have less calls to your container.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜