using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace EfCodeF.FactoryPatern
{
/// <summary>
/// Class contains input params
/// </summary>
public class FileDto
{
public string FileName { get; set; }
public string StorageContainer { get; set; }
public string FileBase64 { get; set; }
public string StorageType { get; set; }
}
/// <summary>
/// abstract class with common implementaion
/// </summary>
public abstract class AStorage
{
public abstract string StorageRepository { get; }
/// comon methods will provide implementation within base abstract class
public virtual string GetFileExtension(string fileName)
{
return fileName.Substring(fileName.LastIndexOf('.'));
}
}
public interface IStorage
{
bool SaveFile(FileDto fileDto);
string GetFileBase64(string storageContainer,string fileName);
}
class Azure : AStorage, IStorage
{
public override string StorageRepository
{
get { return "Azure"; }
}
public bool SaveFile(FileDto fileDto)
{
/// save file in Azure
return true;
}
public string GetFileBase64(string storageContainer, string fileName)
{
///retrieve logic here
return fileName;
}
}
class Onprem : AStorage,IStorage
{
public override string StorageRepository
{
get { return "Onprem"; }
}
public bool SaveFile(FileDto fileDto)
{
/// save file in onprem
return true;
}
public string GetFileBase64(string storageContainer, string fileName)
{
///retrieve logic here
return fileName;
}
}
public abstract class StorageFactory
{
public abstract IStorage GetStorageInstance();
public abstract AStorage GetStorage();
}
class AzureFactory : StorageFactory
{
public override IStorage GetStorageInstance()
{
return new Azure();
}
public override AStorage GetStorage()
{
return new Azure();
}
}
class OnpremFactory : StorageFactory
{
public override IStorage GetStorageInstance()
{
return new Onprem();
}
public override AStorage GetStorage()
{
return new Onprem();
}
}
////controller or main class save file
public class main
{
public bool SaveFile(FileDto fileDto)
{
StorageFactory _storageFactory = GetStorageFactory(fileDto.StorageType);
AStorage _storage = _storageFactory.GetStorage();
///calling abstract class common method
_storage.GetFileExtension("arul.txt");
IStorage storage = _storageFactory.GetStorageInstance();
storage.SaveFile(fileDto);
return true;
}
public StorageFactory GetStorageFactory(string storageType)
{
StorageFactory _storageFactory = null;
switch (storageType)
{
case "Azure":
_storageFactory = new AzureFactory();
break;
case "Onprem":
_storageFactory = new OnpremFactory();
break;
}
return _storageFactory;
}
}
}
No comments:
Post a Comment