Is Func appropriate to use as a ctor arg when applying Dependency Injection?

前端 未结 7 550
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 12:09

Example:

public class BusinessTransactionFactory  where T : IBusinessTransaction
{
    readonly Func _createTransactio         


        
7条回答
  •  故里飘歌
    2021-01-31 12:22

    I think the real question here is whether the dependency type should be considered a contract.

    In the case of a Func delegate, you are declaring a dependency on a certain method structure, but nothing more. You cannot deduce the range of accepted input values (pre-conditions), constraints on the expected output (post-conditions), or what they mean exactly by looking at this structure. For example, is null an acceptable return value? And if it is, then how should it be interpreted?

    In the case of an interface, you have a contract agreed upon by both parties: the consumer explicitly declares that it needs a particular interface, and the implementer explicitly declares to provide it. This goes beyond structure: the documentation of the interface will talk about the pre-conditions, post-conditions and semantics.

提交回复
热议问题