I am new to the Autofac. I know that with Autofac, I need to register these properties like this:Property Injection. And I am looking for something in Autofac just like [Inject] attribute in Ninject. Like this:
[Inject]
public Interface Someproperty { get; set; }
Can something like this be done with Autofac? Any help will be appreciated!
Autofac doesn't use attributes. Just make the properties you want injected public and ensure they have a set
and Autofac does the rest.
var builder = new ContainerBuilder();
// Register the type you want the properties wired up
builder.RegisterType<YourType>().PropertiesAutowired();
// You have to register the thing you want injected to - the value of the property
// and it has to be registered to expose the type of the property. So if the property
// being injected is type `Interface` then make sure you say that here.
builder.RegisterType<AClass>().As<Interface>();
If you really, really want to control which properties get injected via attribute - which I don't recommend, because it's not great practice to have DI-related stuff intertwined with your business logic - there is a working example in the Autofac repo in the form of a unit test. That example shows you how to create your own custom attribute and use a custom Autofac property selector to only wire up properties that have the custom attribute.