2020
2121namespace SysML2 . NET . Viewer
2222{
23+ using System . Linq ;
2324 using System . Net . Http ;
25+ using System . Reflection ;
2426 using System . Threading . Tasks ;
25-
27+ using Blazored . SessionStorage ;
2628 using BlazorStrap ;
2729
30+ using Microsoft . AspNetCore . Components . Authorization ;
2831 using Microsoft . AspNetCore . Components . Web ;
2932 using Microsoft . AspNetCore . Components . WebAssembly . Hosting ;
3033
@@ -34,6 +37,10 @@ namespace SysML2.NET.Viewer
3437
3538 using Serilog ;
3639 using Serilog . Events ;
40+
41+ using SySML2 . NET . REST ;
42+ using SysML2 . NET . Serializer . Json ;
43+ using SysML2 . NET . Viewer . Services . Authentication ;
3744
3845 /// <summary>
3946 /// The purpose of the <see cref="Program"/> class is to provide the
@@ -63,17 +70,59 @@ public static async Task Main(string[] args)
6370
6471 builder . RootComponents . Add < App > ( "#app" ) ;
6572 builder . RootComponents . Add < HeadOutlet > ( "head::after" ) ;
73+
74+ AddServices ( builder ) ;
75+ AddViewModels ( builder ) ;
76+
77+ await builder . Build ( ) . RunAsync ( ) ;
78+ }
6679
67- builder . Services . AddScoped ( sp => new HttpClient ( ) ) ;
80+ /// <summary>
81+ /// Register all services into the <see cref="WebAssemblyHostBuilder.Services" />
82+ /// </summary>
83+ /// <param name="builder">The <see cref="WebAssemblyHostBuilder" /></param>
84+ private static void AddServices ( WebAssemblyHostBuilder builder )
85+ {
86+ builder . Services . AddBlazoredSessionStorage ( ) ;
87+ builder . Services . AddAuthorizationCore ( ) ;
6888
6989 builder . Services . AddScoped < DialogService > ( ) ;
7090 builder . Services . AddScoped < NotificationService > ( ) ;
7191 builder . Services . AddScoped < TooltipService > ( ) ;
7292 builder . Services . AddScoped < ContextMenuService > ( ) ;
73-
93+
7494 builder . Services . AddBlazorStrap ( ) ;
7595
76- await builder . Build ( ) . RunAsync ( ) ;
96+ builder . Services . AddScoped ( sp => new HttpClient ( ) ) ;
97+
98+ builder . Services . AddScoped < AuthenticationStateProvider , AnonymousAuthenticationStateProvider > ( ) ;
99+ builder . Services . AddScoped < IAuthenticationService , AuthenticationService > ( ) ;
100+ builder . Services . AddScoped < IRestClient , RestClient > ( ) ;
101+ builder . Services . AddScoped < IDeSerializer , DeSerializer > ( ) ;
102+ builder . Services . AddScoped < ISerializer , Serializer > ( ) ;
103+ }
104+
105+ /// <summary>
106+ /// Register all ViewModels into the <see cref="WebAssemblyHostBuilder" />
107+ /// </summary>
108+ /// <param name="builder">The <see cref="WebAssemblyHostBuilder" /></param>
109+ private static void AddViewModels ( WebAssemblyHostBuilder builder )
110+ {
111+ var viewModelInterfaces = Assembly . GetCallingAssembly ( ) . GetExportedTypes ( )
112+ . Where ( x => x . IsInterface && x . Name . EndsWith ( "ViewModel" ) ) . ToList ( ) ;
113+
114+ foreach ( var viewModelInterface in viewModelInterfaces )
115+ {
116+ var viewModel = Assembly . GetCallingAssembly ( ) . GetExportedTypes ( )
117+ . FirstOrDefault ( x => x . IsClass
118+ && x . Name == viewModelInterface . Name . Remove ( 0 , 1 )
119+ && x . GetInterface ( viewModelInterface . Name ) == viewModelInterface ) ;
120+
121+ if ( viewModel != null )
122+ {
123+ builder . Services . AddTransient ( viewModelInterface , viewModel ) ;
124+ }
125+ }
77126 }
78127 }
79128}
0 commit comments