Giter VIP home page Giter VIP logo

netjection's Introduction

Netjection

.Net package to automatically inject attribute decorated services into IOC Container

.NET Nuget NuGet stable version GitHub license

Give a Star! โญ

If you like or are using this project please give it a star. Thanks!

Installing

Using dotnet cli

dotnet add package Netjection --version 1.0.7

or package reference

<PackageReference Include="Netjection" Version="1.0.7" />

Usage

// Attribute without any parameter inject service as scoped
// and implementation type will be interface name without "I" prefix.
[Injectable]
public interface IUserService { }

public class UserService : IUserService { }
// You can specify service lifetime.
[Injectable(Lifetime.Singleton)]
public interface IProductService { }

public class ProductService : IProductService { }
// You can specify implementation type.
[Injectable(typeof(ConsoleLogger),Lifetime.Transient)]
public interface ICustomLogger { }

public class ConsoleLogger : ICustomLogger { }

Or scope specific attributes

// Attribute without any parameter inject service as scoped
// and implementation type will be interface name without "I" prefix.
[InjectAsSingleton]
public interface ISingletonService { }

public class SingletonService : ISingletonService { }

// You can specify implementation type.
[InjectAsScoped(typeof(ScopedServiceV2))]
public interface IScopedService { }

public class ScopedServiceV2 : IScopedService { }

[InjectAsTransient]
public interface ITransientService { }

public class TransientService : ITransientService { }

can be used with classes

[InjectAsSingleton]
public class DummyStorage
{
    public string GetDummyText() => "Some Dummy Text";
}

Netjection can map configuration with classes and inject as singleton in DI container. it will use class name to search configuration section, but you can specify custom section name in attribute constructor.

"Options": {
    "Url": "localhost",
    "Port": 8080,
    "UserName": "user",
    "Password": "password"
  }
[Configure] // or [Configure("CustomSectionName")]
public class Options
{
    public string Url { get; set; }
    public int Port { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
}

then you can inject in constructor in three ways shown below.

public class WhereOptionsAreInjected
{
    private readonly Test _test;
    private readonly Test _testAsOptionsValue;
    private readonly Test _testAsOptionsSnapshotValue;
    public WhereOptionsAreInjected(Test test,IOptions<Test> testOptions,
            IOptionsSnapshot<Test> testOptionsSnapshot)
    {
        _test = test;
        _testAsOptionsValue = testOptions.Value;
        _testAsOptionsSnapshotValue = testOptionsSnapshot.Value;
    }
}

and then use extension method on IServiceCollection to inject all service

using Netjection

public void ConfigureServices(IServiceCollection services)
{
    services.InjectServices(Assembly.GetExecutingAssembly(),Assembly.GetAssembly(typeof(SingletonService)));
}

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Icon

injection by VectorsLab from Noun Project

netjection's People

Contributors

revazashvili avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

shsahin

netjection's Issues

Scope specific attributes

will be good to add attributes for specific scopes (InjectAsSingleton,InjectAsScoped,InjectAsTransient).

what should be priority for injecting services(example; 1 - singleton,2 - scoped, 3 - transient)

Support for only classes

attributes support only interfaces, it should support classes to inject only class in IOC container

Documentation

need to write docs in Readme.md file, describe how to use package

IOptions<T>

Configure attribute should also inject classes with Configure method on IServiceCollection to resolve as IOptions

sample

add sample project, to show how to use package

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.