Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Make removing a formatter a 1-liner #2945

Closed
rynowak opened this issue Aug 10, 2015 · 7 comments
Closed

Make removing a formatter a 1-liner #2945

rynowak opened this issue Aug 10, 2015 · 7 comments
Labels
3 - Done enhancement up-for-grabs Members of our awesome commnity can handle this issue
Milestone

Comments

@rynowak
Copy link
Member

rynowak commented Aug 10, 2015

See code sample here: #2944 (comment)

When we were using descriptors for formatters we had a 1-liner extension method for removing a formatter by-type from the collection. We should add this back by defining custom collection types for input formatters and output formatters.

@danroth27 danroth27 modified the milestones: 6.0.0-beta8, Backlog Aug 14, 2015
@danroth27 danroth27 added the up-for-grabs Members of our awesome commnity can handle this issue label Aug 18, 2015
@Krusen
Copy link
Contributor

Krusen commented Sep 24, 2015

Hi, I'm new to contributing to other projects but would like to give this a shot.

Were you thinking something like this?

namespace Microsoft.AspNet.Mvc.Core.Formatters
{
    public class FormatterCollection<TFormatter> : Collection<TFormatter>
    {
        /// <summary>
        /// Removes all formatters of the specified type.
        /// </summary>
        /// <typeparam name="T">The type to remove.</typeparam>
        public void RemoveType<T>() where T : TFormatter
        {
            var formatters = this.OfType<T>();
            foreach (var formatter in formatters)
            {
                Remove(formatter);
            }
        }
    }
}
public MvcOptions()
{
    // ...
    InputFormatters = new FormatterCollection<IInputFormatter>();
    OutputFormatters = new FormatterCollection<IOutputFormatter>();
    // ...
}

public FormatterCollection<IInputFormatter> InputFormatters { get; }
public FormatterCollection<IOutputFormatter> OutputFormatters { get; }

// ...

Should the IList<IInputFormatter>/IList<IOutputFormatter> properties in Microsoft.AspNet.Mvc.Filters.ResourceExecutingContext and Microsoft.AspNet.Mvc.Filters.ActionBindingContext be changed as well?

@rynowak
Copy link
Member Author

rynowak commented Sep 24, 2015

@Krusen - yes, that's pretty much exactly what I had in mind.

/cc @Eilon in case he has feedback on the approach. This also follows the design of some other places where we were getting rid of extension methods that you need to use from options/startup code.

@Eilon
Copy link
Member

Eilon commented Sep 24, 2015

I'm cool with this. BTW I'm not sure the proposed code will necessarily work: you might have to force .ToList() after calling .OfType() so that all the LINQ evaluation will be complete before modifying the collection.

@Eilon Eilon modified the milestones: 6.0.0-rc1, Backlog Sep 24, 2015
@Krusen
Copy link
Contributor

Krusen commented Sep 25, 2015

Great. I'll go ahead and make a pull request later today.

@Eilon You are right, it needs to be changed.

@davidfowl
Copy link
Member

Avoid using LINQ if you can. If you use a List<T> you can use RemoveAll

@rynowak
Copy link
Member Author

rynowak commented Sep 25, 2015

Avoid using LINQ if you can. If you use a List you can use RemoveAll

Why does this matter? This is about making startup code more friendly.

@rynowak
Copy link
Member Author

rynowak commented Sep 28, 2015

ec18b35

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3 - Done enhancement up-for-grabs Members of our awesome commnity can handle this issue
Projects
None yet
Development

No branches or pull requests

5 participants