Skip to content

Bionic Extensions - Development Extensions for a uniform Blazor development experience

Notifications You must be signed in to change notification settings

BionicFramework/BionicExtensions

Repository files navigation

Bionic Extensions

Simple extensions to aid with Bionic development model.

Install

dotnet -i BionicExtensions

Contents

Injectable Attribute

Intent:

Automatically register new services with Blazor's ServiceCollection.

Usage:

1 - Add InjectableAttribute.RegisterInjectables function to your Blazor Startup.cs file:

namespace RxBlazor {
  public class Startup {
    public void ConfigureServices(IServiceCollection services) {
      InjectableAttribute.RegisterInjectables(services); // Add this line
    }

    public void Configure(IBlazorApplicationBuilder app) {
      app.AddComponent<App>("app");
    }
  }
}

2 - Annotate new services with Injectable attribute:

The Injectable attribute accepts two arguments: [Injectable(InterfaceType, ServiceType)] where:

InterfaceType = the annotated service interface ServiceType = the type of service being registered: "singleton", "transient" or "scoped". default is "singleton"

The following example registers the CounterService as a Singleton for Dependency Injection with an interface of ICounterService.

[Injectable(typeof(ICounterService))]
public class CounterService : ICounterService {
  // Service impl here...
}

If we want to make Transient instead of a Singleton, we can do:

[Injectable(typeof(ICounterService), "transient")]

For more information please refer to Adding Service in DI documentation.

About

Bionic Extensions - Development Extensions for a uniform Blazor development experience

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published