WebApiContrib / WebAPIContrib.Core

Community Contributions for ASP.NET Core
MIT License
461 stars 116 forks source link

Fluent configuration support for CsvOutputFormatter #133

Open piotrkolodziej opened 6 years ago

piotrkolodziej commented 6 years ago

Hello community,

I would very much like to add 'fluent' configuration support for CsvOutputFormatter. It would work similarly to FluentValidation or EF, and would look like:

Startup.cs

services
    .AddMvc()
    .AddCsvSerializerFormatters(options =>
        options.RegisterConfigsFromAssemblyContaining<BusinessProfile>();
    );

InvoiceFormatterConfig.cs - a config

    public class InvoiceFormatterConfig : CsvFormattingConfiguration<InvoiceModel>
    {
        public void Configure(CsvFormattingBuilder<InvoiceModel> builder)
        {
            builder
                .UseHeaders()
                .ForHeader("Name")
                .UseProperty(i => i.Name)
                .ForHeader("GrandTotal")
                .UseProperty(i => i.InvoiceDetails.GrandTotal);
        }
    }

CsvOutputFormatter would use this configuration and would be able to generate CSV based on it. Please let mne know what do you think. I am doing it anyway for my project, I just think it's something that would be far better than creating separate model type with primitive properties and using automapper to map entity type to our new model––which would exist only for the purpose of formatters. What do you think? Are you interested in my contribution?

ps: this could be also implemented for other formatter types.

Best Regards Piotr Kolodziej

damienbod commented 6 years ago

@piotrkolodziej This would be cool. Would you add an extra extension method?

Greetings Damien

piotrkolodziej commented 6 years ago

@damienbod Let me work on a prototype for few days and reply to your question when I have more details.

piotrkolodziej commented 6 years ago

@damienbod what’s the procedure assuming I have a fork ready to share with you? This is my first contribution and also first time I am dealing with forks. Is it possible to create something similar to pull request so that you will be able to look over the changes?

filipw commented 6 years ago

Yes just push your changes to a new branch in your fork. You will then be able to open a PR from your fork into WebAPIContrib.Core master.

As soon as you push a branch with changes to your fork, and navigate to the root of WebApiContrib.Core repo, GH will automatically detect that you have some changes and offer a new button to open a PR.

Alternatively you can always trigger a PR from a fork branch using the following link: https://github.com/WebApiContrib/WebAPIContrib.Core/compare/master...piotrkolodziej:NAME_OF_YOUR_BRANCH

piotrkolodziej commented 6 years ago

Guys, just letting you know that prototype is ready but I will have a 1.5-2 weeks delay because I've got a sinus infection and will be changing location to India. Anyway don't close this topic, this is totally happening :-)

piotrkolodziej commented 6 years ago

@damienbod and @filipw , I've finished implementing (and testing) the fluent version of formatters. Let's keep in touch :-) Pull request has been created.