StructureMap resolve dependency through injection instead of service location

后端 未结 4 766
孤城傲影
孤城傲影 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:38

    Since your code assumes it is getting a JsonSerializer, create a new IJsonSerializer interface that only the JsonSerializer implements. Any class that needs the JsonSerializer should accept an IJsonSerializer. If you still need the ISerializer interface to be common across all serializers, the IJsonSerializer can be used just as a marker interface.

    Alternatively, you can tie the specific ISerializer implementation to your class when you register your class in StructureMap.

    x.For().Use(c => new MySomeClass(c.GetInstance()));
    

提交回复
热议问题