Code Monkey home page Code Monkey logo

web-app-service-library's Introduction

WebAppServiceLibrary

Publish Packages package

This class library is meant to ease configuration and registration of services, database contexts and option files in .NET 5.0 web app projects.

Installation and configuration

Install package from nuget gallery using either dotnet CLI

dotnet add package ServiceLibrary.WebApp

or the package manager

Install-Package ServiceLibrary.WebApp

Register all the configured services adding this line in the startup:

services.ConfigureService(Configuration);

Configuring services

Services registered with DI are usually interface's implementations. Starting froma a generic interface IService, the implementation would be

public interface Service : IService

To have this automatic registered by this class library, we just add one attribute

[Service(typeof(IService), ServiceLifetime.Scoped)]
public interface Service : IService

This is the equivalent of adding

services.AddScoped<IService, Service>();

in the startup.cs. By changing the values of the second parameter we can select the lifetime of the service (scoped, singleton, ...). If the Service does not implement any interface, we can simply leave first attribute null.

Configuring databases

Databases can be easily configured adding the attribute

[Database("ConnectionString")]
public class MyDbContext : DbContext

This is the equivalent of adding

services.AddDbContext<MyDbContext>(options => options.UseSqlite("Data Source=<connection_string>"));

in the startup.cs.

The string parameter is the name of the json attribute in the appsettings.json file which holds the value for the connection string.

Configuring options

Options can be easily configured adding the attribute

[Option("JsonSections")]
public class MySettings

This is the equivalent of adding

// Add functionality to inject IOptions<T>
services.AddOptions();

// Add our Config object so it can be injected
services.Configure<MySettings>(Configuration.GetSection("JsonSections"));

in the startup.cs.

The string parameter is the name of the json section in the appsettings.json file which we want to map to our model. Every attribute in the json object needs to have a correspondent property in the model.

web-app-service-library's People

Contributors

dependabot[bot] avatar

Stargazers

 avatar

Watchers

 avatar

web-app-service-library's Issues

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.