Skip to content

Instantly share code, notes, and snippets.

@hikalkan
Last active April 18, 2023 07:37
Show Gist options
  • Save hikalkan/9ef8bb64acdfae42f486857013cca333 to your computer and use it in GitHub Desktop.
Save hikalkan/9ef8bb64acdfae42f486857013cca333 to your computer and use it in GitHub Desktop.
How to replace IBinaryObjectManager
/* Add registration code to Initialize of your module */
//...
public override void Initialize()
{
IocManager.Register<IBinaryObjectManager, FileSystemBinaryObjectManager>(DependencyLifeStyle.Transient); //ADD THIS HERE
IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
}
//...
/* This is not needed if you delete DbBinaryObjectManager from your solution. */
using System;
using System.IO;
using System.Threading.Tasks;
using Abp.Runtime.Session;
namespace MyCompanyName.AbpZeroTemplate.Storage
{
/* This is a simple implementation. It's better to implement methods async ( https://msdn.microsoft.com/en-us/library/kztecsys(v=vs.110).aspx )
*/
public class FileSystemBinaryObjectManager : IBinaryObjectManager
{
private readonly IAbpSession _abpSession;
public FileSystemBinaryObjectManager(IAbpSession abpSession)
{
_abpSession = abpSession;
}
public Task<BinaryObject> GetOrNullAsync(Guid id)
{
var filePath = $@"C:\BinaryObjects\{id.ToString("D")}.bin";
if (!File.Exists(filePath))
{
return null;
}
var binaryObject = new BinaryObject(_abpSession.TenantId, File.ReadAllBytes(filePath));
return Task.FromResult(binaryObject);
}
public Task SaveAsync(BinaryObject file)
{
var filePath = $@"C:\BinaryObjects\{file.Id.ToString("D")}.bin";
File.WriteAllBytes(filePath, file.Bytes);
return Task.CompletedTask;
}
public Task DeleteAsync(Guid id)
{
var filePath = $@"C:\BinaryObjects\{id.ToString("D")}.bin";
if (!File.Exists(filePath))
{
return null;
}
File.Delete(filePath);
return Task.CompletedTask;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment