Skip to content

Common Library

samatrhea edited this page Mar 22, 2024 · 9 revisions

How to use it

index.html/_Host.cshtml

index.html

Include a reference to the css, with the following lines

<link href="_content/CDP4.WEB.Common/css/styles.css" rel="stylesheet" />
<link href="_content/DevExpress.Blazor.Themes/bootstrap-external.bs5.min.css" rel="stylesheet" />

_Imports.razor

To use common pages/components, add those using directive

@using DevExpress.Blazor
@using COMET.Web.Common.Components
@using COMET.Web.Common.Components.Applications
@using COMET.Web.Common.Components.ParameterTypeEditors
@using COMET.Web.Common.Components.Selectors
@using COMET.Web.Common.Components.ValueSetRenderers
@using COMET.Web.Common.Pages

Program.cs

To register all services and ViewModels requires to use the Common library, use the IServiceCollection extension method RegisterCommonLibrary. You can register your assembly, additional top menu entries and applications by filling data inside this method. Some services have to be initialized after building the application, by using the IServerProvider extension method InitializeServices.

WASM

            var builder = WebAssemblyHostBuilder.CreateDefault(args);

            builder.RootComponents.Add<App>("#app");
            builder.RootComponents.Add<HeadOutlet>("head::after");

            builder.Services.RegisterCommonLibrary(false, options =>
            {
                options.Applications = Applications.ExistingApplications;
                options.AdditionalAssemblies.Add(Assembly.GetAssembly(typeof(Program)));
                options.AdditionalMenuEntries.AddRange(new List<Type>{typeof(ShowHideDeprecatedThings), typeof(AboutMenu)});
            });
            
            builder.Services.AddScoped(_ => new HttpClient()
            {
                BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
            });

            var host = builder.Build();
            await host.Services.InitializeServices();
            await host.RunAsync();

Blazor Server

            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddRazorPages();
            builder.Services.AddServerSideBlazor();

            builder.Services.RegisterCommonLibrary(globalOptions: options =>
            {
                options.Applications = Applications.ExistingApplications;
                options.AdditionalAssemblies.Add(Assembly.GetAssembly(typeof(Program)));
                options.CustomHeaderTitle = typeof(TopMenuTitle);
            });

            builder.Logging.SetMinimumLevel(Debugger.IsAttached ? LogLevel.Debug : LogLevel.Warning);

            var app = builder.Build();

            app.UseStaticFiles();
            app.UseRouting();
            app.MapBlazorHub();
            app.MapFallbackToPage("/_Host");

            await app.Services.InitializeServices();
            await app.RunAsync();

Existing pages

The Logout page already exists and should not be implemented again. For the Index page, you can use the IndexComponent if this component suits your needs.

Development

If any changes have to be made during the development of the CometWebApp, reference the COMET.WEB.Common project instead of using the Nuget Package, by including following lines in the COMETwebapp.csproj file

    <ItemGroup>
      <ProjectReference Include="..\COMET.Web.Common\COMET.Web.Common.csproj" />
    </ItemGroup>