Skip to content

Commit

Permalink
Implement AbstractFactory.
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Feb 3, 2024
1 parent 89433ce commit dbb9a53
Show file tree
Hide file tree
Showing 21 changed files with 206 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DesignPatterns.AbstractFactory.DataLake;

public class Directory : IPathInfo
{
public string Name { get; init; }

Check warning on line 5 in Creational/DesignPatterns.AbstractFactory/DataLake/Directory.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Path { get; init; }

Check warning on line 6 in Creational/DesignPatterns.AbstractFactory/DataLake/Directory.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Path' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Uri { get; init; }

Check warning on line 7 in Creational/DesignPatterns.AbstractFactory/DataLake/Directory.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Uri' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
8 changes: 8 additions & 0 deletions Creational/DesignPatterns.AbstractFactory/DataLake/File.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DesignPatterns.AbstractFactory.DataLake;

public class File : IPathInfo
{
public string Name { get; init; }
public string Path { get; init; }
public string Uri { get; init; }
}
51 changes: 51 additions & 0 deletions Creational/DesignPatterns.AbstractFactory/DataLake/Storage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Azure.Storage.Files.DataLake;

namespace DesignPatterns.AbstractFactory.DataLake;

public class Storage : IStorage
{
private readonly StorageSettings settings;
private readonly DataLakeServiceClient dataLakeServiceClient;

public Storage(StorageSettings settings, DataLakeServiceClient dataLakeServiceClient)
{
this.settings = settings;
this.dataLakeServiceClient = dataLakeServiceClient;
}

public async Task<IPathInfo> CreateDirectory(string name)
{
var fileSystemClient = this.dataLakeServiceClient.GetFileSystemClient(this.settings.FileSystemName);
await fileSystemClient.CreateIfNotExistsAsync();
var response = await fileSystemClient.CreateDirectoryAsync(name);
if (!response.HasValue)
{
throw new DirectoryNotFoundException($"Could not create the '{name}' directory.");
}

return new Directory
{
Name = response.Value.Name,
Path = response.Value.Path,
Uri = response.Value.Path
};
}

public async Task<IPathInfo> CreateFile(string name)
{
var fileSystemClient = this.dataLakeServiceClient.GetFileSystemClient(this.settings.FileSystemName);
await fileSystemClient.CreateIfNotExistsAsync();
var response = await fileSystemClient.CreateFileAsync(name);
if (!response.HasValue)
{
throw new FileNotFoundException($"Could not create the '{name}' file.");
}

return new File
{
Name = response.Value.Name,
Path = response.Value.Path,
Uri = response.Value.Path
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Azure.Storage.Files.DataLake;

namespace DesignPatterns.AbstractFactory.DataLake;

public class StorageFactory : IStorageFactory
{
private readonly StorageSettings settings;
private readonly DataLakeServiceClient serviceClient;

public StorageFactory(StorageSettings settings, DataLakeServiceClient serviceClient)
{
this.settings = settings;
this.serviceClient = serviceClient;
}

public IStorage Create()
{
return new Storage(settings, serviceClient);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DesignPatterns.AbstractFactory.DataLake;

public class StorageSettings
{
public string FileSystemName { get; init; }

Check warning on line 5 in Creational/DesignPatterns.AbstractFactory/DataLake/StorageSettings.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'FileSystemName' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
14 changes: 0 additions & 14 deletions Creational/DesignPatterns.AbstractFactory/DataLakeStorage.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@
<RootNamespace>DesignPatterns.AbstractFactory</RootNamespace>
</PropertyGroup>

</Project>
<ItemGroup>
<PackageReference Include="Azure.Storage.Files.DataLake" Version="12.17.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
5 changes: 0 additions & 5 deletions Creational/DesignPatterns.AbstractFactory/Directory.cs

This file was deleted.

5 changes: 0 additions & 5 deletions Creational/DesignPatterns.AbstractFactory/File.cs

This file was deleted.

5 changes: 0 additions & 5 deletions Creational/DesignPatterns.AbstractFactory/IPath.cs

This file was deleted.

7 changes: 7 additions & 0 deletions Creational/DesignPatterns.AbstractFactory/IPathInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DesignPatterns.AbstractFactory;

public interface IPathInfo
{
string Name { get; }
string Path { get; }
}
4 changes: 2 additions & 2 deletions Creational/DesignPatterns.AbstractFactory/IStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace DesignPatterns.AbstractFactory;

public interface IStorage
{
Task<IPath> CreateDirectory(string name);
Task<IPath> CreateFile(string name);
Task<IPathInfo> CreateDirectory(string name);
Task<IPathInfo> CreateFile(string name);
}
9 changes: 1 addition & 8 deletions Creational/DesignPatterns.AbstractFactory/IStorageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,5 @@ namespace DesignPatterns.AbstractFactory;

public interface IStorageFactory
{
}

public class LocalStorageFactory : IStorageFactory
{
}

public class AzureStorageFactory : IStorageFactory
{
IStorage Create();
}
7 changes: 7 additions & 0 deletions Creational/DesignPatterns.AbstractFactory/Local/Directory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DesignPatterns.AbstractFactory.Local;

public class Directory : IPathInfo
{
public string Name { get; init; }

Check warning on line 5 in Creational/DesignPatterns.AbstractFactory/Local/Directory.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Path { get; init; }

Check warning on line 6 in Creational/DesignPatterns.AbstractFactory/Local/Directory.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Path' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
7 changes: 7 additions & 0 deletions Creational/DesignPatterns.AbstractFactory/Local/File.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DesignPatterns.AbstractFactory.Local;

public class File : IPathInfo
{
public string Name { get; init; }

Check warning on line 5 in Creational/DesignPatterns.AbstractFactory/Local/File.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Path { get; init; }

Check warning on line 6 in Creational/DesignPatterns.AbstractFactory/Local/File.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Path' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
39 changes: 39 additions & 0 deletions Creational/DesignPatterns.AbstractFactory/Local/Storage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace DesignPatterns.AbstractFactory.Local;

public class Storage : IStorage
{
private readonly StorageSettings settings;

public Storage(StorageSettings settings)
{
this.settings = settings;
}

public Task<IPathInfo> CreateDirectory(string name)
{
var directoryPath = Path.Combine(this.settings.Path, name);
var directoryInfo = new DirectoryInfo(directoryPath);
directoryInfo.Create();
var result = new Directory
{
Name = directoryInfo.Name,
Path = directoryInfo.FullName
};

return Task.FromResult<IPathInfo>(result);
}

public Task<IPathInfo> CreateFile(string name)
{
var filePath = Path.Combine(this.settings.Path, name);
var fileInfo = new FileInfo(filePath);
fileInfo.Create();
var result = new File
{
Name = fileInfo.Name,
Path = fileInfo.FullName
};

return Task.FromResult<IPathInfo>(result);
}
}
16 changes: 16 additions & 0 deletions Creational/DesignPatterns.AbstractFactory/Local/StorageFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace DesignPatterns.AbstractFactory.Local;

public class StorageFactory : IStorageFactory
{
private readonly StorageSettings settings;

public StorageFactory(StorageSettings settings)
{
this.settings = settings;
}

public IStorage Create()
{
return new Storage(this.settings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace DesignPatterns.AbstractFactory.Local;

public class StorageSettings
{
public string Path { get; init; }

Check warning on line 5 in Creational/DesignPatterns.AbstractFactory/Local/StorageSettings.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Path' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
14 changes: 0 additions & 14 deletions Creational/DesignPatterns.AbstractFactory/LocalStorage.cs

This file was deleted.

10 changes: 8 additions & 2 deletions Creational/DesignPatterns.AbstractFactory/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// See https://aka.ms/new-console-template for more information
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

Console.WriteLine("Hello, World!");
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(ctx => ctx.AddJsonFile("appsettings.json"))
.ConfigureServices(services => { })
.Build();

Console.WriteLine();
8 changes: 8 additions & 0 deletions Creational/DesignPatterns.AbstractFactory/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"DataLake": {
"FileSystemName": "abstract-factory-example"
},
"Local": {
"Path": "/home"
}
}

0 comments on commit dbb9a53

Please sign in to comment.