Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code to a plugin design. Structural changes, functional modi… #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
<PackageReference Include="SkiaSharp.QrCode" Version="0.6.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="StorageServices\" />
</ItemGroup>

</Project>
8 changes: 4 additions & 4 deletions Egineering.UrlShortener.Services/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
global using System.Diagnostics.CodeAnalysis;
global using Azure;
global using Azure.Data.Tables;
global using Egineering.UrlShortener.Services.DTOs;
global using Egineering.UrlShortener.Services.Helpers;
//global using Azure;
//global using Azure.Data.Tables;
//global using Egineering.UrlShortener.Services.DTOs;
//global using Egineering.UrlShortener.Services.Helpers;
global using Egineering.UrlShortener.Services.Interfaces;
global using Microsoft.Extensions.Configuration;
23 changes: 7 additions & 16 deletions Egineering.UrlShortener.Services/Helpers/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
namespace Egineering.UrlShortener.Services.Helpers;
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;

namespace Egineering.UrlShortener.Services.Helpers;

[ExcludeFromCodeCoverage]
public static class Constants
{
// API
public const string Authorization = "Authorization";
public const string SecurityToken = "SecurityToken";
public const string StorageType = "StorageType";



// Azure Storage
public const string AzureStorageConnectionString = "AzureStorage";
public const string UrlTableName = "urls";

// URL properties
public const string Name = "name";
public const string Url = "url";
public const string UrlPartitionKey = Url;
public const string Visits = "visits";
public const string IsPublic = "isPublic";

// Azure Request Error Codes
public static class AzureRequestErrorCodes
{
public const string ResourceNotFound = "ResourceNotFound";
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using Egineering.UrlShortener.Services.Exceptions;

namespace Egineering.UrlShortener.Services;
global using Azure;
global using Azure.Data.Tables;
using Egineering.UrlShortener.Storage.AzureTableStorage;
using Egineering.UrlShortener.Storage.AzureTableStorage.Interfaces;
using Egineering.UrlShortener.Storage.DTOs;
using Egineering.UrlShortener.Storage.Exceptions;
using Microsoft.Extensions.Configuration;
namespace Egineering.UrlShortener.Services.StorageServices;

public class AzureTableStorageService : IAzureTableStorageService
{
Expand Down Expand Up @@ -49,7 +54,7 @@ public IEnumerable<ShortenedUrl> GetAllPublicUrls()
var results = tableEntities.Select(entity => new ShortenedUrl
{
Name = entity.GetString(Constants.Name),
PartitionKey = entity.PartitionKey,
//PartitionKey = entity.PartitionKey, //TODO: Remove?
Timestamp = entity.Timestamp.Value,
Url = entity.GetString(Constants.Url),
Vanity = entity.RowKey,
Expand Down Expand Up @@ -84,13 +89,13 @@ public async Task AddUrl(UrlRequest urlRequest)
public async Task ReplaceUrl(UrlRequest urlRequest)
{
var urlEntity = await GetUrlEntityByVanity(urlRequest.Vanity);

if (urlEntity == null)
{
throw new UrlEntityNotFoundException(urlRequest.Vanity);
}

var entity = new TableEntity(Constants.UrlPartitionKey, urlRequest.Vanity)
var entity = new TableEntity(Constants.UrlPartitionKey, urlRequest.Vanity)
{
{ Constants.Name, urlRequest.Name },
{ Constants.Url, urlRequest.Url },
Expand Down Expand Up @@ -122,4 +127,7 @@ private TableClient GetUrlTableClient()
}
return tableEntity;
}
public static string TypeName => "AzureTableStorage";
public bool IsStorageType(string name) => name == TypeName;
public string StorageType() => TypeName;
}
22 changes: 22 additions & 0 deletions Egineering.UrlShortener.Storage.AzureTableStorage/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Egineering.UrlShortener.Storage.AzureTableStorage
{
internal static class Constants
{
// Azure Storage
public const string AzureStorageConnectionString = "AzureStorage";
public const string UrlTableName = "urls";

// URL properties
public const string Name = "name";
public const string Url = "url";
public const string UrlPartitionKey = Url;
public const string Visits = "visits";
public const string IsPublic = "isPublic";

// Azure Request Error Codes
public static class AzureRequestErrorCodes
{
public const string ResourceNotFound = "ResourceNotFound";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.Data.Tables" Version="12.6.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Egineering.UrlShortener.Storage\Egineering.UrlShortener.Storage.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Egineering.UrlShortener.Storage.DTOs;
using Egineering.UrlShortener.Storage.Interfaces;

namespace Egineering.UrlShortener.Storage.AzureTableStorage.Interfaces;

public interface IAzureTableStorageService : IStorageService
{
Task<string> GetUrlFromVanityAsync(string vanity);
//public string PartitionKey { get; set; }
IEnumerable<ShortenedUrl> GetAllPublicUrls();
Task AddUrl(UrlRequest urlRequest);
Task ReplaceUrl(UrlRequest urlRequest);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Egineering.UrlShortener.Services.StorageServices;
using Egineering.UrlShortener.Storage.Interfaces;
using Microsoft.Extensions.DependencyInjection;

namespace Egineering.UrlShortener.Storage.AzureTableStorage
{
public class PluginConfiguration : IPluginFactory
{
public void Configure(IServiceCollection services)
{
services.AddSingleton<IStorageService, AzureTableStorageService>();
}

public string StorageType()
{
return AzureTableStorageService.TypeName;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
namespace Egineering.UrlShortener.Services.DTOs;
using System.Diagnostics.CodeAnalysis;

namespace Egineering.UrlShortener.Storage.DTOs;

[ExcludeFromCodeCoverage]
public class ShortenedUrl
{
public string Name { get; set; }
public string PartitionKey { get; set; }
public DateTimeOffset Timestamp { get; set; }
public string Url { get; set; }
public string Vanity { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Egineering.UrlShortener.Services.DTOs;
using System.Diagnostics.CodeAnalysis;

namespace Egineering.UrlShortener.Storage.DTOs;

[ExcludeFromCodeCoverage]
public class UrlRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.4.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Egineering.UrlShortener.Services.Exceptions;
using System.Diagnostics.CodeAnalysis;

namespace Egineering.UrlShortener.Storage.Exceptions;

[ExcludeFromCodeCoverage]
public class ConflictException : Exception
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Egineering.UrlShortener.Services.Exceptions;
using System.Diagnostics.CodeAnalysis;

namespace Egineering.UrlShortener.Storage.Exceptions;

[ExcludeFromCodeCoverage]
public class UnauthorizedException : Exception
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Egineering.UrlShortener.Services.Exceptions;
using System.Diagnostics.CodeAnalysis;
namespace Egineering.UrlShortener.Storage.Exceptions;

[ExcludeFromCodeCoverage]
public class UrlEntityNotFoundException : Exception
Expand Down
9 changes: 9 additions & 0 deletions Egineering.UrlShortener.Storage/Interfaces/IStorageBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

namespace Egineering.UrlShortener.Storage.Interfaces
{
public interface IStorageBuilder
{
Dictionary<string, Type> StorageOptions { get; }
Type? FindStorageOption(string name);
}
}
16 changes: 16 additions & 0 deletions Egineering.UrlShortener.Storage/Interfaces/IStorageFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace Egineering.UrlShortener.Storage.Interfaces
{

public interface IPluginFactory
{
void Configure(IServiceCollection services);
string StorageType();
}
}
16 changes: 16 additions & 0 deletions Egineering.UrlShortener.Storage/Interfaces/IStorageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

using Egineering.UrlShortener.Storage.DTOs;

namespace Egineering.UrlShortener.Storage.Interfaces
{
public interface IStorageService
{
bool IsStorageType(string name);
string StorageType();

Task<string> GetUrlFromVanityAsync(string vanity);
IEnumerable<ShortenedUrl> GetAllPublicUrls();
Task AddUrl(UrlRequest urlRequest);
Task ReplaceUrl(UrlRequest urlRequest);
}
}
49 changes: 49 additions & 0 deletions Egineering.UrlShortener.Storage/StorageBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Egineering.UrlShortener.Storage.Interfaces;
using McMaster.NETCore.Plugins;
using Microsoft.Extensions.DependencyInjection;

namespace Egineering.UrlShortener.Services
{
public static class StorageBuilder
{
static private List<PluginLoader> LoadtorageOptions()
{
var loaders = new List<PluginLoader>();

// create plugin loaders
var pluginsDir = Path.Combine(AppContext.BaseDirectory, "plugins");
foreach (var dir in Directory.GetDirectories(pluginsDir))
{
var dirName = Path.GetFileName(dir);
var pluginDll = Path.Combine(dir, dirName + ".dll");
if (File.Exists(pluginDll))
{
var loader = PluginLoader.CreateFromAssemblyFile(
pluginDll,
sharedTypes: new[] { typeof(IPluginFactory), typeof(IServiceCollection) });
loaders.Add(loader);
}
}
return loaders;
}

public static void ConfigureStorageProvider(IServiceCollection services, string storagePluginName)
{
var loaders = LoadtorageOptions();
// Create an instance of plugin types
foreach (var loader in loaders)
{
foreach (var pluginType in loader
.LoadDefaultAssembly()
.GetTypes()
.Where(t => typeof(IPluginFactory).IsAssignableFrom(t) && !t.IsAbstract))
{
// This assumes the implementation of IPluginFactory has a parameterless constructor
var plugin = Activator.CreateInstance(pluginType) as IPluginFactory;
if(plugin?.StorageType() == storagePluginName)
plugin?.Configure(services);
}
}
}
}
}
16 changes: 14 additions & 2 deletions Egineering.UrlShortener.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32804.467
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Egineering.UrlShortener", "Egineering.UrlShortener\Egineering.UrlShortener.csproj", "{AD04C2A5-7441-4BBC-93C4-309EF3E61E48}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Egineering.UrlShortener", "Egineering.UrlShortener\Egineering.UrlShortener.csproj", "{AD04C2A5-7441-4BBC-93C4-309EF3E61E48}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Egineering.UrlShortener.Services", "Egineering.UrlShortener.Services\Egineering.UrlShortener.Services.csproj", "{A7220A97-E567-4421-AC91-7BD60ABB5B5A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Egineering.UrlShortener.Services", "Egineering.UrlShortener.Services\Egineering.UrlShortener.Services.csproj", "{A7220A97-E567-4421-AC91-7BD60ABB5B5A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Egineering.UrlShortener.Storage", "Egineering.UrlShortener.Storage\Egineering.UrlShortener.Storage.csproj", "{FDCB7A2D-933E-41D1-B1AA-D6D65197275C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Egineering.UrlShortener.Storage.AzureTableStorage", "Egineering.UrlShortener.Storage.AzureTableStorage\Egineering.UrlShortener.Storage.AzureTableStorage.csproj", "{4400303B-BA66-4D86-9781-E28B5CB7E5DE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,6 +25,14 @@ Global
{A7220A97-E567-4421-AC91-7BD60ABB5B5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7220A97-E567-4421-AC91-7BD60ABB5B5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7220A97-E567-4421-AC91-7BD60ABB5B5A}.Release|Any CPU.Build.0 = Release|Any CPU
{FDCB7A2D-933E-41D1-B1AA-D6D65197275C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FDCB7A2D-933E-41D1-B1AA-D6D65197275C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FDCB7A2D-933E-41D1-B1AA-D6D65197275C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FDCB7A2D-933E-41D1-B1AA-D6D65197275C}.Release|Any CPU.Build.0 = Release|Any CPU
{4400303B-BA66-4D86-9781-E28B5CB7E5DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4400303B-BA66-4D86-9781-E28B5CB7E5DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4400303B-BA66-4D86-9781-E28B5CB7E5DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4400303B-BA66-4D86-9781-E28B5CB7E5DE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
8 changes: 8 additions & 0 deletions Egineering.UrlShortener/Egineering.UrlShortener.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@

<ItemGroup>
<ProjectReference Include="..\Egineering.UrlShortener.Services\Egineering.UrlShortener.Services.csproj" />
<ProjectReference Include="..\Egineering.UrlShortener.Storage\Egineering.UrlShortener.Storage.csproj" />
<ProjectReference Include="..\Egineering.UrlShortener.Storage.AzureTableStorage\Egineering.UrlShortener.Storage.AzureTableStorage.csproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot\images\" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="echo OutDir: $(OutDir)&#xD;&#xA;&#xD;&#xA;xcopy ..\Egineering.UrlShortener.Storage.AzureTableStorage\$(OutDir) $(OutDir)plugins\Egineering.UrlShortener.Storage.AzureTableStorage /i /s /e /y" />
</Target>

</Project>
Loading