Code Monkey home page Code Monkey logo

flaeng.productivity's Introduction

Flaeng.Productivity

CodeFactor Maintainability

Nuget Nuget

Mission / What does it do

Flaeng.Productivity aims to improve productivity and developer experience by generating code, that would often be boilerplate, using C# source generators

How does it that?

GenerateInterfaceAttribute generates a interface with all the public properties, fields and methods in your class so you don't have to keep it up-to-date but only need to change your test code.

InjectAttribute generates a constructor for the declaring class with parameters for every property or field with the InjectAttribute in the order they are defined in the class.

MakeFluentAttribute - Makes methods for each property or field marked with the attribute (or all properties and fields if marked on the class) so that you can chain the method-calls like a fluent API

RegisterServiceAttribute - Makes an RegisterServices-extensions-method on the IServiceCollection-interface so that you don't have to remmeber to add your services to you dependency injection container

Examples

More examples in the docs

GenerateInterface Example 1: Input

using System;

namespace Flaeng.Productivity.Sample.Providers;

[Flaeng.GenerateInterface]
public partial class SummaryProvider
{
    public string[] GetSummaries()
    {
        return new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };
    }
}

GenerateInterface Example 1: Output

// <auto-generated/>

using System;

#nullable enable

namespace Flaeng.Productivity.Sample.Providers
{
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Flaeng.Productivity", "0.2.3.0")]
    public interface ISummaryProvider
    {
        string[] GetSummaries();
    }
    public partial class SummaryProvider : ISummaryProvider
    {
    }
}

GenerateInterface and Inject Example 2: Input

using Flaeng.Productivity.Sample.Providers;

namespace Flaeng.Productivity.Sample.Services;

[Flaeng.GenerateInterface]
public partial class WeatherForecastService
{
    [Flaeng.Inject] protected readonly ISummaryProvider _summaryProvider;

    public IEnumerable<WeatherForecast> GetWeatherForecast()
    {
        var Summaries = _summaryProvider.GetSummaries();
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }
}

GenerateInterface and Inject Example 2: Output

// <auto-generated/>

using Flaeng.Productivity.DependencyInjection;
using Flaeng.Productivity.Sample.Providers;

#nullable enable

namespace Flaeng.Productivity.Sample.Services
{
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Flaeng.Productivity", "0.2.3.0")]
    public interface IWeatherForecastService
    {
        IEnumerable<WeatherForecast> GetWeatherForecast();
    }
    public partial class WeatherForecastService : IWeatherForecastService
    {
    }
}
// <auto-generated/>

using Flaeng.Productivity.DependencyInjection;
using Flaeng.Productivity.Sample.Providers;

#nullable enable

namespace Flaeng.Productivity.Sample.Services
{
    public partial class WeatherForecastService
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Flaeng.Productivity", "0.2.3.0")]
        public WeatherForecastService(
            ISummaryProvider _summaryProvider
            )
        {
            this._summaryProvider = _summaryProvider;
        }
    }
}

MakeFluent Example 1: Input

[Flaeng.MakeFluent]
public class Blah 
{ 
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age;
}

MakeFluent Example 1: Output

public static partial class BlahExtensions
{
    public static Blah FirstName(
        this Blah _this,
        global::System.String FirstName
    )
    {
        _this.FirstName = FirstName;
        return _this;
    }
    public static Blah LastName(
        this Blah _this,
        global::System.String LastName
    )
    {
        _this.LastName = LastName;
        return _this;
    }
    public static Blah Age(
        this Blah _this,
        global::System.Int32 Age
    )
    {
        _this.Age = Age;
        return _this;
    }
}

RegisterService Example 1: Input

namespace TestNamespace
{
    public interface IMyServiceInterface { }
    [Flaeng.RegisterService]
    public class MyService : IMyServiceInterface { }

    public interface IMyTransientServiceInterface { }
    [Flaeng.RegisterService(ServiceType = Flaeng.ServiceType.Transient)]
    public class MyTransientService : IMyTransientServiceInterface { }

    public interface IMyScopedServiceInterface { }
    [Flaeng.RegisterService(ServiceType = Flaeng.ServiceType.Scoped)]
    public class MyScopedService : IMyScopedServiceInterface { }

    public interface IMySingletonServiceInterface { }
    [Flaeng.RegisterService(ServiceType = Flaeng.ServiceType.Singleton)]
    public class MySingletonService : IMySingletonServiceInterface { }
}

RegisterService Example 1: Output

using Microsoft.Extensions.DependencyInjection;

public static partial class StartupExtensions
{
    public static global::Microsoft.Extensions.DependencyInjection.IServiceCollection RegisterServices(
        this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services
    )
    {
        services.AddScoped<global::TestNamespace.IMyServiceInterface, global::TestNamespace.MyService>();
        services.AddTransient<global::TestNamespace.IMyTransientServiceInterface, global::TestNamespace.MyTransientService>();
        services.AddScoped<global::TestNamespace.IMyScopedServiceInterface, global::TestNamespace.MyScopedService>();
        services.AddSingleton<global::TestNamespace.IMySingletonServiceInterface, global::TestNamespace.MySingletonService>();
        return services;
    }
}

More examples in the docs

flaeng.productivity's People

Contributors

flaeng avatar

Stargazers

 avatar  avatar

Watchers

 avatar

flaeng.productivity's Issues

GenerateEqualityComparer

See example in Flaeng.Productivity/Generators/EqualityComparerGenerator.cs

Possible challenges

  • Datatypes that are not primitive datatypes

Improved documentation

Automatic generate documentation (using Github wiki) from tests with examples

Ideas:

  • Show source input and output from each test case in c sharp formatting
  • Have a way to make comments in the test-file show up as markup in the md-file for the documentation

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.