Search code examples
c#asp.net-mvcdependency-injectioninversion-of-controlautofac

initializing custom classes and autofac injection


Let's assume I have a class like this

 public class Test {
     public IMyService MyService { get; set;}

     public string Name { get; set; }
}

How can I have the service injected with propertiesautowired in Autofac?

How can I run something like this:

Test t = new Test();
t.MyService.DoSomething();

Solution

  • you can autowire property for concrete classes in autofac as below

    builder.RegisterType<Test>().PropertiesAutowired();