Skip to content

Commit

Permalink
Add support for EasyMicroservices.Cores.Infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali-YousefiTelori committed Feb 13, 2024
1 parent 0ef301e commit 29853e5
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net45;net6.0;net8.0</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<Authors>EasyMicroservices</Authors>
<Version>0.0.1.50</Version>
<Description>core of infrastructure.</Description>
<Copyright>[email protected]</Copyright>
<PackageTags>core,cores,base,infrastructure</PackageTags>
<PackageProjectUrl>https://github.com/EasyMicroservices/Cores</PackageProjectUrl>
<LangVersion>latest</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RootNamespace>EasyMicroservices.Cores</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EasyMicroservices.Utilities" Version="0.0.0.13" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Threading.Tasks;

namespace EasyMicroservices.Cores.Interfaces;
public interface IWidget
{
void Build();
Type GetObjectType();
}

public interface IWidget<T>: IWidget
{
Task Initialize(params T[] parameters);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;

namespace EasyMicroservices.Cores.Interfaces;

public interface IWidgetManager
{
void Register(IWidget widget);
void UnRegister(IWidget widget);
IEnumerable<IWidget> GetWidgetsByType(Type type);
IEnumerable<T> GetWidgets<T>()
where T : IWidget;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using EasyMicroservices.Cores.Interfaces;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace EasyMicroservices.Cores.Widgets;
public class WidgetManager : IWidgetManager
{
readonly ConcurrentDictionary<Type, List<IWidget>> Widgets = new ConcurrentDictionary<Type, List<IWidget>>();

public void Register(IWidget widget)
{
var type = widget.GetObjectType();
if (Widgets.TryGetValue(type, out List<IWidget> widgets))
widgets.Add(widget);
else
{
Widgets.TryAdd(widget.GetObjectType(), new List<IWidget>()
{
widget
});
}
}

public void UnRegister(IWidget widget)
{
Widgets.TryRemove(widget.GetObjectType(), out _);
}

public IEnumerable<IWidget> GetWidgetsByType(Type type)
{
if (Widgets.TryGetValue(type, out List<IWidget> widgets))
return widgets.ToList();
return Enumerable.Empty<IWidget>();
}

public IEnumerable<T> GetWidgets<T>() where T : IWidget
{
if (Widgets.TryGetValue(typeof(T), out List<IWidget> widgets))
return widgets.Cast<T>().ToList();
return Enumerable.Empty<T>();
}
}

0 comments on commit 29853e5

Please sign in to comment.