Property injection for members of an interface
I can do property injection by using the following code:开发者_如何学JAVA
builder.RegisterType<ListViewModel>()
.PropertiesAutowired();
This injects dependencies for all properties. But sometimes I only want to do property injection for members of an interface:
public interface IHasCommonServices
{
ISession Session { get; set; }
IEventPublisher EventPublisher { get; set; }
}
public class ListViewModel : IHasCommonServices
{
// Inject dependencies for these properties
public ISession Session { get; set; }
public IEventPublisher EventPublisher { get; set; }
// Don't inject for these properties
public ListItemViewModel SelectedItem { get; set; }
}
Is something like the following possible?
builder.RegisterType<ListViewModel>()
.InjectForInterface<IHasCommonServices>();
Thanks.
Not sure if this is what you're after, but I think in most cases you can get away with:
.PropertiesAutowired(PropertyWiringFlags.PreserveSetValues);
If you need to have a bit more fine-grained control you can use OnActivated:
builder.Register<ListViewModel>()
.As<IHasCommonServices>()
.OnActivated(c =>
{
c.Instance.Session = c.Context.Resolve<ISession>();
c.Instance.EventPublisher = c.Context.Resolve<IEventPublisher>();
});
It's a little-bit tedious, but I don't think you would often need to do much property injection.
Hope that helps.
精彩评论