using Autofac; using System; namespace PatternDemoCore { public class Entity { public delegate Entity Factory(); private static Random random = new Random(); private int number; public Entity() { number = random.Next(); } public override string ToString() { return $"test {number}"; } } public class ViewModel { private readonly Entity.Factory entityFactory; public ViewModel(Entity.Factory entityFactory) { this.entityFactory = entityFactory; } public void Method() { var entity = entityFactory(); Console.WriteLine(entity); } } public class Demo { static void Main(string[] args) { var cb = new ContainerBuilder(); cb.RegisterType().InstancePerDependency(); cb.RegisterType(); var container = cb.Build(); var vm = container.Resolve(); vm.Method(); vm.Method(); } } }