DanielCunha.Toolkit.MappedServices 1.0.1

dotnet add package DanielCunha.Toolkit.MappedServices --version 1.0.1
NuGet\Install-Package DanielCunha.Toolkit.MappedServices -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DanielCunha.Toolkit.MappedServices" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DanielCunha.Toolkit.MappedServices --version 1.0.1
#r "nuget: DanielCunha.Toolkit.MappedServices, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DanielCunha.Toolkit.MappedServices as a Cake Addin
#addin nuget:?package=DanielCunha.Toolkit.MappedServices&version=1.0.1

// Install DanielCunha.Toolkit.MappedServices as a Cake Tool
#tool nuget:?package=DanielCunha.Toolkit.MappedServices&version=1.0.1

Mapped Services

The idea of this package is to make it easier to work with dependency injection in ASP.NET Core.

Basically, it contains an attribute which can be used to automatically add your services to the service collection.

This spares you from adding all your services manually to the service collection. All you have to do is to call services.AddMappedServices() in your Startup.cs class and the work will be done.

Motivation

Create an ease to use feature which automatically map contracts (Interfaces) and implementations (Classes) into services and add them to the service collection.

Installation

Install NuGet package DanielCunha.Toolkit.MappedServices or download repo and compile it manually so you can reference it from your target project.

Usage

All you have to have is to add the MappedServiceAttribute to your classes, call services.AddMappedServices() in the ConfigureServices method of your Startup class and all the work will be done by the package. Here's an example:

public interface IUserService
{
    Task<User> AuthenticateAsync(string email, string password);
}

[MappedService]
public class UserService : IUserService
{
    public Task<User> AuthenticateAsync(string email, string password)
    {
        // Your implementation...
    }
}

public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMappedServices();

        // Other services...
    }
}

And that's all! Using this feature you can inject as many services you want to just by adding a attribute to your class, without touching in the Startup class every time.

MappedServiceAttribute

The attribute have some properties to make it easier to use it: Scope, InheritedType and InheritedTypeName.

The first one is basically the scope of the service which can be Scoped, Transient and Singleton. The default value is Scoped. Read more about scopes in this article.

The other two are basically an explicitly way of mapping a service to its contract. Usually, if you don't use any of them, the package will look to an inherited type with the name format of "I" + the name of the service (UserService and IUserService for example).

Otherwise, if you use one of them, it will use the informed type or look for an interface with the informed name. It will always prefer to use the type instead of the name in case you pass both.

Contribute

Fell free to clone this repository and modify the project as you wish. I'll glad to add your changes to the next version of the package!

License

MIT License.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 557 11/9/2019
1.0.0 386 11/9/2019