GithubHelp home page GithubHelp logo

codefork / startupmodules Goto Github PK

View Code? Open in Web Editor NEW

This project forked from henkmollema/startupmodules

0.0 2.0 0.0 27 KB

Startup modules for ASP.NET Core.

License: MIT License

Batchfile 0.76% PowerShell 5.23% Shell 0.16% C# 93.85%

startupmodules's Introduction

StartupModules

Startup modules for ASP.NET Core.

Create modular and focused Startup-like classes for each of your application's features/components/modules and keep your startup file sane.

Windows Linux NuGet
Windows Linux NuGet

Installation

StartupModules is available on NuGet for ASP.NET Core 2.1.

Getting started

Create a startup module

Creating a startup module is easy. Create a new class and implement the IStartupModule interface:

public class MyStartupModule : IStartupModule
{
    public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
    {
    }

    public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
    {
    }
}

You'll notice the familiair ConfigureServices and Configure methods. A convient context-object is passed to both of them providing useful services while configuring your application, such as IHostingEnvironment and IConfiguration. A scoped IServiceProvider is present in the ConfigureMiddlewareContext as well.

Configure startup modules

You can configure startup modules using the UseStartupModules when building the web host in Program.cs:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartupModules()
        .UseStartup<Startup>();

This will automatically discover startup modules (and application initializers) in the entry assembly of your application. You can also specify an array of assemblies to discover startup modules from:

.UseStartupModules(typeof(Startup).Assembly, typeof(SomeTypeInAnotherAssembly).Assembly)

You can have more control of the configuration using the overload with the options:

.UseStartupModules(options =>
{
    // Discover from entry assembly
    o.DiscoverStartupModules();

    // Discover from specific assemblies
    o.DiscoverStartupModules(typeof(Startup).Assembly, typeof(SomeTypeInAnotherAssembly).Assembly);

    // Add individual startup modules
    o.AddStartupModule<MyStartupModule>();
})

Application Initializers

Application initializers allow you to write asynchronous startup logic for your application, such as configuring your Entity Framework database context and executing migrations. Applications initializers are, just like startup modules, discovered automatically as well.

public class DatabaseInitializer : IApplicationInitializer
{
    private readonly AppDbContext _dbContext;

    public DatabaseInitializer(AppDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public async Task Task Invoke()
    {
        await _dbContext.MigrateAsync();
    }
}

You can specify any number of dependencies available in your application via the constructor of an application initializer. The dependencies will be resolved from a scoped service provider instance and will be disposed after application startup. This prevents common pitfalls such as a resolving a singleton database context and leaking its connection for the lifetime off the application and avoids the hassle of creating a service provider scope yourself.

startupmodules's People

Contributors

henkmollema avatar

Watchers

 avatar  avatar

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.