Is there a way to configure Autofac to create inheritors of some base class to be single instances?

StackOverflow https://stackoverflow.com/questions/23562511

  •  18-07-2023
  •  | 
  •  

Вопрос

I am using Prism with Autofac, and I found myself couple of times having a bugs due to multiple instances of the ViewModel class which I expected to be single instance.

Thus my question is: how can I configure Autofac so that all inheritors from some base class (e.g. ViewModelBase) will always be single instances?

Это было полезно?

Решение

That's very easy to do with container builder extension methods. This is one way

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
    .AssignableTo<ViewModelBase>()
    .SingleInstance();
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top