make sure object only created by factory (C#)

前端 未结 8 1574
梦谈多话
梦谈多话 2021-01-18 11:03

How do I make sure that a certain class is only instantiated by a factory and not by calling new directly?

EDIT: I need the factory

8条回答
  •  执念已碎
    2021-01-18 11:36

    I do not like to have the factory on the type itself especially if it is a domain object. Have it internal if you are having a separate class as factory (which I think you should). Use InternalVisible attribute if the factory is on the different assembly.

提交回复
热议问题