autofac / Autofac.Web

ASP.NET WebForms integration for Autofac
MIT License
10 stars 9 forks source link

Collections being injected incorrectly #3

Closed tillig closed 8 years ago

tillig commented 9 years ago

From @JonathanBooker on September 17, 2015 15:17

Web Forms .NET 4.5.1 AutoFac 3.5.2

In our UserControls we have IList. This property is being injected before Page_Load and then it is populated by our code so the DropDownList is bound to twice.

public IList<User> Users
{
    set
    {
         DropDownList.DataSource = value;
         DropDownList.DataBind();
     }
}

Cannot seem to override action of IList being populated by AutoFac which is not the behaviour we want. It is causing issues with the databinding of the list.

Copied from original issue: autofac/Autofac#681

tillig commented 9 years ago

Properties on things don't get injected unless you tell it to. Is that the behavior you want everywhere else? If not, I'd say stop injecting properties.

If you need all settable properties injected except that one consider making the property protected - Autofac will only work on public properties, and generally only ones that appear unset.

I don't see a getter on that property, but that's another option - set a default non-null value on the property and Autofac will see that it's set and won't try to set it again.

There's really not a great way to say "I want IEnumerable<T> support to work but not IList<T>" - there's not a built-in cherry-picking functionality like that.

Let me know how that goes.

JonnyBooker commented 9 years ago

Apologies for the lack of clarification before, I was rather rushed to get a question asked and back to work. I'll elaborate more this time.

I haven't set up anything to inject any IList properties in my configuration, all my registrations are singular class to interface.

In this instance making the properties protected is not an option, the IList properties are on a UserControl and therefore these properties need to be public so they can set via the page they reside on.

I will try your advice and get back to you. Thank you for the advice.

tillig commented 8 years ago

We have an issue on core Autofac to potentially add some sort of attribute-based control over which properties get injected. You may want to subscribe to follow the progress on that.

Given I don't think there's anything else we can help you with here, I'm going to close the issue. If you have more questions about usage, I encourage you to ask on StackOverflow and tag it autofac - we monitor things over there, but you also have the rest of the community that may also be able to provide ideas.