ServiceContainer, IoC, and disposable objects

后端 未结 3 1159
囚心锁ツ
囚心锁ツ 2021-02-06 07:40

I have a question, and I\'m going to tag this subjective since that\'s what I think it evolves into, more of a discussion. I\'m hoping for some good ideas or some thoug

3条回答
  •  不要未来只要你来
    2021-02-06 08:16

    You actually came up with a very dirty solution: your IService contract violates the SRP, wich is a big no-no.

    What I recommend is to distinguish so-called "singleton" services from so-called "prototype" services. Lifetime of "singleton" ones is managed by the container, which may query at runtime whether a particular instance implements IDisposable and invoke Dispose() on shutdown if so.

    Managing prototypes, on the other hand, is totally the responsibility of the calling code.

提交回复
热议问题