Commit c1eaf869 authored by HankG's avatar HankG

Add Service Factory for ProfileImage and LinkService, and underlying storage factory methods too

parent b71b8f2d
......@@ -12,10 +12,13 @@ namespace MySocialPortalDesktop.Services
public static readonly ServiceFactory Instance = Singleton.Value;
public LinkPreviewService LinkPreviewService { get; }
public ImageService ImageService { get; }
private ServiceFactory()
{
var imagePreviewCacheFolder = DirectoryServices.Instance.OpenGraphCacheDirectory();
var linkPreviewImageService = new ImageService(imagePreviewCacheFolder);
LinkPreviewService = new LinkPreviewService(RepositoryFactory.Instance.LinkPreviewRepository, imagePreviewCacheFolder);
}
......
......@@ -10,32 +10,44 @@ namespace MySocialPortalDesktop.Factory
private const string AllPostsRepositoryDbName = "all_people.db";
private const string LinkPreviewRepositoryDbName = "link_preview.db";
private const string MainPeopleRepositoryDbName = "all_posts.db";
private const string ProfileImageRepositoryDbName = "profile_images.db";
private static readonly Lazy<RepositoryFactory> Singleton =
new Lazy<RepositoryFactory>(() => new RepositoryFactory());
public static readonly RepositoryFactory Instance = Singleton.Value;
private bool _disposed = false;
public IPostsRepository AllPostsRepository => AllPostsRepositoryLazy.Value;
public ILinkPreviewRepository LinkPreviewRepository => LinkPreviewRepositoryLazy.Value;
public IFileCacheRepository LinkPreviewImageCacheRepository => LinkPreviewImageCacheRepositoryLazy.Value;
public IFileCacheRepository ProfileImageCacheRepository => ProfileImageCacheRepositoryLazy.Value;
public IPersonsRepository MainPeopleRepository => MainPeopleRepositoryLazy.Value;
public IPostsRepository AllPostsRepository => AllPostsRepositoryLazy.Value;
public RepositoryFactory()
{
AllPostsRepositoryLazy = new Lazy<IPostsRepository>(() => BuildAllPostsRepository());
LinkPreviewRepositoryLazy = new Lazy<ILinkPreviewRepository>(() => BuildLinkPreviewRepository());
MainPeopleRepositoryLazy = new Lazy<IPersonsRepository>(() => BuildPeopleRepository());
AllPostsRepositoryLazy = new Lazy<IPostsRepository>(BuildAllPostsRepository);
LinkPreviewRepositoryLazy = new Lazy<ILinkPreviewRepository>(BuildLinkPreviewRepository);
LinkPreviewImageCacheRepositoryLazy = new Lazy<IFileCacheRepository>(BuildLinkPreviewImageCacheRepository);
MainPeopleRepositoryLazy = new Lazy<IPersonsRepository>(BuildPeopleRepository);
ProfileImageCacheRepositoryLazy = new Lazy<IFileCacheRepository>(BuildProfileImageCacheRepository);
}
private Lazy<IPostsRepository> AllPostsRepositoryLazy { get; }
private Lazy<ILinkPreviewRepository> LinkPreviewRepositoryLazy { get; }
private Lazy<IFileCacheRepository> LinkPreviewImageCacheRepositoryLazy { get; }
private Lazy<IPersonsRepository> MainPeopleRepositoryLazy { get; }
private Lazy<IFileCacheRepository> ProfileImageCacheRepositoryLazy { get; }
private IPostsRepository BuildAllPostsRepository()
{
return new PostsLiteDbRepository(BuildDbPath(AllPostsRepositoryDbName));
......@@ -46,6 +58,16 @@ namespace MySocialPortalDesktop.Factory
return new LinkPreviewLiteDbRepository(BuildDbPath(LinkPreviewRepositoryDbName));
}
private IFileCacheRepository BuildLinkPreviewImageCacheRepository()
{
return new FileCacheLiteDbRepository(BuildDbPath(LinkPreviewRepositoryDbName));
}
private IFileCacheRepository BuildProfileImageCacheRepository()
{
return new FileCacheLiteDbRepository(BuildDbPath(ProfileImageRepositoryDbName));
}
private IPersonsRepository BuildPeopleRepository()
{
return new PersonsLiteDbRepository(BuildDbPath(MainPeopleRepositoryDbName));
......@@ -53,7 +75,8 @@ namespace MySocialPortalDesktop.Factory
public void Dispose()
{
(MainPeopleRepository as IDisposable)?.Dispose();
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
......@@ -67,13 +90,15 @@ namespace MySocialPortalDesktop.Factory
{
(AllPostsRepository as IDisposable)?.Dispose();
(LinkPreviewRepository as IDisposable)?.Dispose();
(LinkPreviewImageCacheRepository as IDisposable)?.Dispose();
(MainPeopleRepository as IDisposable)?.Dispose();
(ProfileImageCacheRepository as IDisposable)?.Dispose();
}
_disposed = true;
}
private string BuildDbPath(string databaseName)
private static string BuildDbPath(string databaseName)
{
return Path.Combine(DirectoryServices.Instance.UserAppBaseDirectory(), databaseName);
}
......
using System;
using MySocialPortalLib.Service;
namespace MySocialPortalDesktop.Factory
{
public class ServiceFactory
{
private static readonly Lazy<ServiceFactory> Singleton =
new Lazy<ServiceFactory>(() => new ServiceFactory());
public static readonly ServiceFactory Instance = Singleton.Value;
public LinkPreviewService LinkPreviewService => LinkPreviewServiceLazy.Value;
public ImageService ProfileImageService => PreviewImageServiceLazy.Value;
private ServiceFactory()
{
LinkPreviewServiceLazy = new Lazy<LinkPreviewService>(BuildLinkPreviewService);
PreviewImageServiceLazy = new Lazy<ImageService>(BuildProfileImageService);
}
private Lazy<LinkPreviewService> LinkPreviewServiceLazy { get; }
private Lazy<ImageService> PreviewImageServiceLazy { get; }
private LinkPreviewService BuildLinkPreviewService()
{
var imagePreviewCacheFolder = DirectoryServices.Instance.OpenGraphCacheDirectory();
var linkPreviewImageService = new ImageService(RepositoryFactory.Instance.LinkPreviewImageCacheRepository,
imagePreviewCacheFolder);
return new LinkPreviewService(RepositoryFactory.Instance.LinkPreviewRepository, linkPreviewImageService);
}
private ImageService BuildProfileImageService()
{
var profileImageDir = DirectoryServices.Instance.ProfileImagesDirectory();
return new ImageService(RepositoryFactory.Instance.ProfileImageCacheRepository, profileImageDir);
}
}
}
\ No newline at end of file
......@@ -44,7 +44,7 @@ namespace MySocialPortalLib.Model
return Id == other.Id;
}
public override bool Equals(object other)
public override bool Equals(object? other)
{
if (other == null)
{
......
......@@ -57,7 +57,7 @@ namespace MySocialPortalLib.Model
public override int GetHashCode()
{
return (Id != null ? Id.GetHashCode() : 0);
return (Id != null ? Id.GetHashCode(StringComparison.InvariantCulture) : 0);
}
public bool AllEquals(LinkPreview other)
......
......@@ -14,7 +14,7 @@ namespace MySocialPortalLib.Repository
private FileCacheLiteDbRepository()
{
throw new NotImplementedException();
throw new MemberAccessException();
}
public FileCacheLiteDbRepository (Stream dbStream,
......
......@@ -15,7 +15,7 @@ namespace MySocialPortalLib.Repository
private LinkPreviewLiteDbRepository()
{
throw new NotImplementedException();
throw new MethodAccessException();
}
public LinkPreviewLiteDbRepository (Stream dbStream,
......
......@@ -12,7 +12,7 @@ namespace MySocialPortalLib.Repository
private PersonsLiteDbRepository()
{
throw new NotImplementedException();
throw new MethodAccessException();
}
public PersonsLiteDbRepository (Stream dbStream,
......
......@@ -16,7 +16,7 @@ namespace MySocialPortalLib.Repository
private PostsLiteDbRepository()
{
throw new NotImplementedException();
throw new MethodAccessException();
}
public PostsLiteDbRepository (Stream dbStream,
......
......@@ -13,6 +13,8 @@ namespace MySocialPortalLib.Service
{
private const string DefaultExtension = ".jpg";
public string LocalImageFolderPath { get; }
private ImageService()
{
throw new NotSupportedException();
......@@ -24,9 +26,14 @@ namespace MySocialPortalLib.Service
LocalImageFolderPath = localImageFolderPath;
}
public async Task<(bool found, FileInfo info)> GetRemoteImage(Uri imageUri, bool reuseExisting = true)
public async Task<(bool found, FileInfo? info)> GetRemoteImage(Uri imageUri, bool reuseExisting = true)
{
string imageFilePath = null;
if (imageUri == null)
{
return (false, null);
}
string imageFilePath = "";
try
{
var imageUrl = imageUri.AbsoluteUri;
......@@ -52,7 +59,10 @@ namespace MySocialPortalLib.Service
await client.DownloadFileTaskAsync(imageUrl, imageFilePath).ConfigureAwait(true);
if (!File.Exists(imageFilePath))
{
CacheRepository.Remove(imageCacheItem);
if (imageCacheItem != null)
{
CacheRepository.Remove(imageCacheItem);
}
return (false, null);
}
......@@ -86,14 +96,12 @@ namespace MySocialPortalLib.Service
return (false, null);
}
private string GetRandomName(string imageExtension)
private static string GetRandomName(string imageExtension)
{
return $"{Guid.NewGuid().ToString()}{imageExtension}";
}
private IFileCacheRepository CacheRepository { get; }
private string LocalImageFolderPath { get; }
}
}
\ No newline at end of file
......@@ -12,9 +12,12 @@ namespace MySocialPortalLib.Service
[SuppressMessage("ReSharper", "CA2234")]
public class LinkPreviewService
{
public string ImageCacheFolderPath => ImageService?.LocalImageFolderPath ?? "";
private LinkPreviewService()
{
throw new NotImplementedException();
throw new MethodAccessException();
}
public LinkPreviewService(ILinkPreviewRepository repository, ImageService imageService)
......
......@@ -17,7 +17,7 @@ namespace MySocialPortalLib.Service.SocialMediaConnectors
private TwitterConnector()
{
throw new NotImplementedException();
throw new MethodAccessException();
}
public TwitterConnector(IPersonsRepository personsRepository)
......
......@@ -25,11 +25,24 @@ namespace MySocialPortalLibTest.Factory
{
Assert.NotNull(RepositoryFactory.Instance.LinkPreviewRepository);
}
[Fact]
public void TestLinkPreviewImageCacheRepository()
{
Assert.NotNull(RepositoryFactory.Instance.LinkPreviewImageCacheRepository);
}
[Fact]
public void TestMainPeopleRepository()
{
Assert.NotNull(RepositoryFactory.Instance.MainPeopleRepository);
}
[Fact]
public void TestProfileImageServiceRepository()
{
Assert.NotNull(RepositoryFactory.Instance.ProfileImageCacheRepository);
}
}
}
\ No newline at end of file
using MySocialPortalDesktop.Factory;
using Xunit;
namespace MySocialPortalLibTest.Factory
{
public class ServiceFactoryTest
{
[Fact]
public void TestInstance()
{
Assert.NotNull(ServiceFactory.Instance);
}
[Fact]
public void TestLinkPreviewService()
{
Assert.NotNull(ServiceFactory.Instance.LinkPreviewService);
}
[Fact]
public void TestProfileImageService()
{
Assert.NotNull(ServiceFactory.Instance.ProfileImageService);
}
}
}
\ No newline at end of file
namespace MySocialPortalLibTest.Service
{
public class ServiceFactoryTest
{
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment