StructureMap resolve dependency through injection instead of service location

后端 未结 4 762
孤城傲影
孤城傲影 2021-02-09 19:50

In my project I register many ISerializers implementations with the assembly scanner. FWIW this is the code that registers my ISerializers



        
4条回答
  •  一生所求
    2021-02-09 20:49

    I am curious. What value a ISerializer on it's own is adding? Let's go from specific implementation to one or many picked at runtime.

    If your type is dependent on a specific type of serializer take a dependency on it (IJsonSerializer). This requires that a default instance of that type be registered with the container.

    However, if you are thinking more of having ISerializers as Strategies you would register all your ISerializers and then take a dependency on an array of them and StructureMap will push in an array of all registered ISerializers. The class consuming these serializers then is responsible for selecting which one to use.

    In the strategy scenario you'll likely need some metadata on the serializer for use by your coordinating class to discriminate between them. IMHO, this should really not be container configuration, like names on registered types, but metadata on the implementation itself.

提交回复
热议问题