Is the DI pattern limiting wrt expensive object creation coupled with infrequent dependency usage?

前端 未结 4 1966
甜味超标
甜味超标 2020-12-16 04:19

I\'m having a hard time getting my head around what seems like an obvious pattern problem/limitation when it comes to typical constructor dependency injection. For example

4条回答
  •  北荒
    北荒 (楼主)
    2020-12-16 04:45

    No you are not tied to the limitations you have listed. As of .net 4 you do have Lazy(Of T) at your disposal, which will allow you to defer instantiation of your dependencies until required.

    It is not assumed that object construction is dirt-cheap and consequently some DI containers support Lazy(Of T) out of the box. Whilst Unity 2.0 supports lazy initialization out of the box through automatic factories, there is a good article here on an extension supporting Lazy(Of T) the author has on MSDN.

提交回复
热议问题