Skip to content

Creating modules

A module is a class that implements IModule. Veva finds it at startup and lets it register services, components, middleware and database migrations. This article covers the interface as it is in Veva 7.26, how modules are found and ordered, and a minimal module.

A module is a public class that implements IModule from Veva.Core.Domain.Modules. One module can hold components, a theme, services, middleware, API endpoints and its own database context. Veva itself is built from modules, and your website's code joins in the same way.

How Veva finds a module

You do not register a module anywhere. At startup Veva loads every DLL in the application folder whose file name contains veva, theme, module or component. It then looks through all loaded assemblies for public, non-abstract classes that implement IModule and creates one instance of each.

Three things follow from that:

  • The module class needs a public constructor without parameters. Services are not injected into it; they reach the methods as arguments.
  • The module's project must be referenced by the website project, so that its DLL is in the application folder. An assembly name with one of the four words in it is always picked up.
  • Module names must be unique. A second module with the same Name is skipped.

A minimal module

Only Name, Priority and ConfigureServices must be implemented. Every other member has a default body in the interface.

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Veva.Core.Domain.Modules;

namespace MyWebsite.Modules.MyFeature;

public class Module : IModule
{
	public string Name => "My feature";

	public int Priority => (int)ModulePriorityEnum.Default;

	public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
	{
		services.AddScoped<IMyService, MyService>();
	}
}

The interface

This is IModule in Veva 7.26. Configure takes a WebApplication and ConfigureBuilder a WebApplicationBuilder. Older versions passed IApplicationBuilder and IWebHostBuilder, so a module written for them needs those two signatures changed.

public interface IModule
{
	string Name { get; }

	int Priority { get; }

	void ConfigureServices(IServiceCollection services, IConfiguration configuration);

	void Configure(WebApplication app) { }

	void ConfigureMvc(IMvcBuilder mvc) { }

	void ConfigureComponents(IComponentService componentService, IComponentViewService componentViewService) { }

	void InitializeDatabase(IServiceProvider serviceProvider) { }

	bool DatabaseMigrationsNeeded(IServiceProvider serviceProvider)
	{
		return false;
	}

	IEnumerable<string> GetAppliedMigrations(IServiceProvider serviceProvider)
	{
		return [];
	}

	void ConfigureBuilder(WebApplicationBuilder builder) { }
}
  • ConfigureServices: add the module's services to dependency injection. Extensions such as hooks, tasks and text processors are added here with AddNamed.
  • ConfigureBuilder: reach the WebApplicationBuilder before the application is built, for example to add a configuration source.
  • Configure: add middleware and map endpoints on the built application.
  • ConfigureMvc: change the MVC setup through IMvcBuilder.
  • ConfigureComponents: register the module's components and component views, so editors can add them to pages.
  • InitializeDatabase: apply pending migrations when the module has its own database context.
  • DatabaseMigrationsNeeded: return true when that context has migrations that are not applied yet.
  • GetAppliedMigrations: list the migrations already applied, for the system information in the admin.

Priority

Priority is a number, and ModulePriorityEnum names the usual levels. Configure, ConfigureBuilder, ConfigureMvc, ConfigureComponents and the database methods are called module by module, highest priority first. Use Default unless your module must run before or after another one.

public enum ModulePriorityEnum
{
	Init = 3000,
	Setup = 2000,
	High = 1000,
	BeforeCore = 600,
	Core = 500,
	AfterCore = 400,
	Default = 0,
	Low = -1000,
}

Registering components

A component is a Razor component with a [VevaComponent] id. Register it in ConfigureComponents, and it shows under Add content in the page designer. Creating custom components builds one from start to finish.

public void ConfigureComponents(IComponentService componentService, IComponentViewService componentViewService)
{
	componentService.RegisterComponent<MyComponent>();
}

Registering extensions

Veva's extension points are interfaces that a module implements and registers by name. AddNamed is in the namespace Veva.Core.Application.DependencyInjection. The pages under Writing extensions describe each interface.

using Veva.Core.Application.DependencyInjection;

public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
	services.AddNamed<ITask, MyBackgroundTask>();
}

A module with its own database

A module that stores data adds its own Entity Framework Core context. AddVevaDbContextFactory registers it on the database provider the site is configured for, SQL Server or PostgreSQL. The three database methods then tell Veva whether migrations are waiting, apply them, and list them.

public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
	services.AddVevaDbContextFactory<MyDatabaseContext>(configuration, connectionStringName: "MyConnection");
}

public bool DatabaseMigrationsNeeded(IServiceProvider serviceProvider)
{
	var contextFactory = serviceProvider.GetService<IDbContextFactory<MyDatabaseContext>>();
	using var context = contextFactory.CreateDbContext();
	return context.Database.GetPendingMigrations().Any();
}

public void InitializeDatabase(IServiceProvider serviceProvider)
{
	var contextFactory = serviceProvider.GetService<IDbContextFactory<MyDatabaseContext>>();
	using var context = contextFactory.CreateDbContext();
	context.Database.Migrate();
}

public IEnumerable<string> GetAppliedMigrations(IServiceProvider serviceProvider)
{
	var contextFactory = serviceProvider.GetService<IDbContextFactory<MyDatabaseContext>>();
	using var context = contextFactory.CreateDbContext();
	return context.Database.GetAppliedMigrations();
}

Back to top

Type to search the docs, components and API.

↑↓ to move ↵ to open Esc to close Search by Veva