Initial commit

develop
Matija Koželj 2 years ago
parent 0d5097739c
commit 23220cc726

1
.gitignore vendored

@ -423,6 +423,7 @@ FodyWeavers.xsd
!/build
!/docs
!/examples
!/dependencies
!/src
!/tests
!/tools

3
.gitmodules vendored

@ -0,0 +1,3 @@
[submodule "dependencies/Connected"]
path = dependencies/Connected
url = https://git.tompit.com/Connected/Connected.git

@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.32916.344
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected", "src\Connected\Connected.csproj", "{26AD2745-4DB4-4603-A944-C54D1A374032}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected.Client", "src\Connected.Client\Connected.Client.csproj", "{F0E0F91D-DFCB-44B7-8BA1-5E74C5E008F5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected.UI", "src\Connected.UI\Connected.UI.csproj", "{52D89174-140B-4017-8B52-B83FA936D809}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Dependencies", "Dependencies", "{0A1EBC0F-0567-41FB-9187-7DE1722FAB4E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected.Components", "..\connected.components\src\Connected.Components\Connected.Components.csproj", "{7A6A8DFF-E5B4-482D-98F9-B2AE76235093}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{26AD2745-4DB4-4603-A944-C54D1A374032}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{26AD2745-4DB4-4603-A944-C54D1A374032}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26AD2745-4DB4-4603-A944-C54D1A374032}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26AD2745-4DB4-4603-A944-C54D1A374032}.Release|Any CPU.Build.0 = Release|Any CPU
{F0E0F91D-DFCB-44B7-8BA1-5E74C5E008F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F0E0F91D-DFCB-44B7-8BA1-5E74C5E008F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F0E0F91D-DFCB-44B7-8BA1-5E74C5E008F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F0E0F91D-DFCB-44B7-8BA1-5E74C5E008F5}.Release|Any CPU.Build.0 = Release|Any CPU
{52D89174-140B-4017-8B52-B83FA936D809}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{52D89174-140B-4017-8B52-B83FA936D809}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52D89174-140B-4017-8B52-B83FA936D809}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52D89174-140B-4017-8B52-B83FA936D809}.Release|Any CPU.Build.0 = Release|Any CPU
{7A6A8DFF-E5B4-482D-98F9-B2AE76235093}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7A6A8DFF-E5B4-482D-98F9-B2AE76235093}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7A6A8DFF-E5B4-482D-98F9-B2AE76235093}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7A6A8DFF-E5B4-482D-98F9-B2AE76235093}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{7A6A8DFF-E5B4-482D-98F9-B2AE76235093} = {0A1EBC0F-0567-41FB-9187-7DE1722FAB4E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4BE29712-DF74-4798-8F2E-92D7446D2465}
EndGlobalSection
EndGlobal

@ -0,0 +1 @@
Subproject commit 5721864f19815bd8f906ce52ae9f74301db69657

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\connected.components\src\Connected.Components\Connected.Components.csproj" />
<ProjectReference Include="..\Connected.Client\Connected.Client.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,63 @@
using Connected.Middleware;
using Connected.Startup;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
namespace Connected;
public static class Instance
{
public static WebAssemblyHost Host { get; private set; } = default!;
private static IServiceProvider ServiceProvider { get; set; } = default!;
internal static T? GetService<T>()
{
return ServiceProvider.GetService<T>();
}
public static async Task Start<TApp>(List<IStartup> startups, Dictionary<string, string> args) where TApp : IComponent
{
var builder = WebAssemblyHostBuilder.CreateDefault(UnpackArguments(args));
builder.RootComponents.Add<TApp>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddHttpClient();
builder.Services.AddSingleton(typeof(IComponentMiddlewareService), typeof(ComponentMiddlewareService));
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
foreach (var startup in startups)
await startup.ConfigureServices(builder.Services);
Host = builder.Build();
foreach (var startup in startups)
await startup.Configure(Host);
await Host.RunAsync();
}
private static string[] UnpackArguments(Dictionary<string, string> args)
{
var result = new string[args.Count];
for (var i = 0; i < args.Count; i++)
{
var arg = args.ElementAt(i);
result[i] = string.IsNullOrWhiteSpace(arg.Value) ? arg.Key : $"{arg.Key}={arg.Value}";
}
return result;
}
public static void UseMiddlewareComponent<TMiddleware, TComponent>(this WebAssemblyHost app)
{
if (app.Services.GetService<IComponentMiddlewareService>() is not IComponentMiddlewareService service)
throw new NullReferenceException(nameof(IComponentMiddlewareService));
service.Add<TMiddleware, TComponent>();
}
}

@ -0,0 +1,9 @@
@inherits LayoutComponentBase
<HeadContent>
<link rel="stylesheet" href="_content/Connected.UI/Components.min.css" />
<link rel="stylesheet" href="_content/Connected.UI/Styles.min.css" />
</HeadContent>
@Body

@ -0,0 +1,42 @@
using System.Collections.Concurrent;
namespace Connected.Middleware;
internal class ComponentMiddlewareService : IComponentMiddlewareService
{
public ComponentMiddlewareService()
{
Middleware = new();
}
private ConcurrentDictionary<string, List<Type>> Middleware { get; }
public void Add<TMiddleware, TComponent>()
{
var componentName = typeof(TComponent).FullName;
if (string.IsNullOrEmpty(componentName))
throw new ArgumentException(null, nameof(TMiddleware));
if (Middleware.TryGetValue(componentName, out List<Type>? items))
{
//TODO: sort by priority so multiple inheritance would work
items.Add(typeof(TMiddleware));
}
else
Middleware.TryAdd(componentName, new List<Type> { typeof(TMiddleware) });
}
public Type? Select<TComponent>()
{
var typeName = typeof(TComponent).FullName;
if (string.IsNullOrEmpty(typeName))
throw new ArgumentException(null, nameof(TComponent));
if (Middleware.TryGetValue(typeName, out List<Type>? items) && items is not null && items.Any())
return items[0];
return null;
}
}

@ -0,0 +1,8 @@
namespace Connected.Middleware;
public interface IComponentMiddlewareService
{
void Add<TMiddleware, TComponent>();
Type? Select<TComponent>();
}

@ -0,0 +1,29 @@
using Connected.Middleware;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
namespace Connected.Components;
public abstract class UIComposition : ComponentBase
{
private ILogger _logger;
[Inject]
private ILoggerFactory LoggerFactory { get; set; } = default!;
[Inject]
private IComponentMiddlewareService? MiddlewareService { get; set; }
protected ILogger Logger => _logger ??= LoggerFactory.CreateLogger(GetType());
protected Type ResolveComponent<TComponent>()
{
if (MiddlewareService is null)
return typeof(TComponent);
if (MiddlewareService.Select<TComponent>() is Type type)
return type;
return typeof(TComponent);
}
}
Loading…
Cancel
Save