Giter VIP home page Giter VIP logo

autodependencies's Introduction

AutoDependencies

BuildAndPublish NuGet

AutoDependencies is a library designed to get rid of boilerplate code in ASP.NET services by generating it with Incremental Generator.

Features

  • Generate a partial service
  • Generate constructor with injected services and save ones to the appropriate service members
  • Generate an interface with public methods of a service
  • Generate code with Incremental Generator
  • Create and publish nuget packages
  • Cover generator with tests
  • Generate method that registers all generated services to the IServiceCollection
  • Support for a custom constructor in addition to the generated one
  • Add code analyzer with code fix
  • Generate an interface for the service depending on a flag (i.e. supress interface generation)

Installation

Add the package to your application using

Install-Package AutoDependencies.Generator

This adds a <PackageReference> to your project. You can additionally mark the package as PrivateAssets="all".

Setting PrivateAssets="all" means any projects referencing this one won't get a reference to the AutoDependencies.Generator package.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <!-- Add the package -->
  <PackageReference Include="AutoDependencies.Generator" Version="..." PrivateAssets="all"/>

</Project>

Adding the package will automatically add a marker attributes, such as [Service] and [Inject], to your project.

Usage

First, create the service with some dependencies and public methods. Mark just created service with [Service] attribute, so source generator will process it.

using AutoDependencies.Attributes;

namespace Services;

[Service]
partial class SampleService
{
    private readonly IDependency _dependency;

    public void DoWork() 
    {
        _dependency.DoAnotherWork();
    }
}

Once you do this, the incremental generator will generate:

  • an interface based on public methods of the service;
  • a constructor that injects and initializes dependencies based on private readonly properties;
  • extension method for IServiceCollection that registers the generated services;

What's next

Check out more detailed guides on the various features:
๐Ÿ› ๏ธ Service generation ๐Ÿ› ๏ธ
๐Ÿ“œ Interface generation ๐Ÿ“œ
๐Ÿ”— Injecting dependencies ๐Ÿ”—
๐Ÿงฐ Custom constructor ๐Ÿงฐ
๐Ÿ’ฅ Service collection extension ๐Ÿ’ฅ

autodependencies's People

Contributors

chacaroon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jurilents

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.