Skip to content

IStorageObject Interface

Naveen Dharmathunga edited this page Jan 19, 2026 · 1 revision

IStorageObject Interface

Represents a general abstraction for a storage object, such as a file or directory, providing access to metadata, path information, and change notifications.

Namespace: Rheo.Storage.Contracts

Assembly: Rheo.Storage.dll

Definition

public interface IStorageObject : IDisposable

Derived

Properties

Property Type Description
Information IStorageInformation Gets metadata information about the storage object, such as size, attributes, and timestamps.
Name String Gets the name of the storage object, typically the file or directory name.
ParentDirectory String Gets the full path of the parent directory for the current file or directory.
FullPath String Gets the full path of the file or directory.
IsDisposed Boolean Gets a value indicating whether this storage object has been disposed.

Events

Event Description
Changed Occurs when the storage content changes.

Methods

Method Description
GetBufferSize() Calculates the recommended buffer size based on the current storage information.
ThrowIfDisposed() Throws an ObjectDisposedException if this storage object has been disposed.
Dispose() Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Remarks

Implementations of this interface provide a unified way to interact with storage entities, allowing consumers to retrieve metadata, monitor changes, and manage resource lifetimes. The interface supports event-driven notifications for content changes and enforces proper disposal patterns. Thread safety and event handling behavior may vary by implementation; callers should consult specific documentation for details.


Property Details

Information

Gets metadata information about the storage object, such as size, attributes, and timestamps.

IStorageInformation Information { get; }

Property Value

IStorageInformation

An object containing detailed metadata about the storage item.

Remarks

Implementations typically return platform-specific storage information, such as FileInformation or DirectoryInformation, which provide file system metadata including creation time, modification time, attributes, and size.


Name

Gets the name of the storage object, typically the file or directory name.

string Name { get; }

Property Value

String

The name of the file or directory without the path.

Examples

For a file at C:\Documents\Report.pdf, this property returns Report.pdf.


ParentDirectory

Gets the full path of the parent directory for the current file or directory.

string ParentDirectory { get; }

Property Value

String

The absolute path to the parent directory.

Examples

For a file at C:\Documents\Reports\Annual.pdf, this property returns C:\Documents\Reports.


FullPath

Gets the full path of the file or directory.

string FullPath { get; }

Property Value

String

The complete absolute path to the storage object.


IsDisposed

Gets a value indicating whether this storage object has been disposed.

bool IsDisposed { get; }

Property Value

Boolean

true if the object has been disposed; otherwise, false.

Remarks

Use this property to check the disposal state before performing operations. Most methods will throw an ObjectDisposedException if called after disposal.


Event Details

Changed

Occurs when the storage content changes.

event EventHandler<StorageChangedEventArgs>? Changed

Event Type

EventHandler<StorageChangedEventArgs>

Remarks

Subscribers are notified whenever an item is added, removed, or updated in the storage. The event provides details about the change through the StorageChangedEventArgs parameter. This event is typically raised on the thread where the change occurs; callers should ensure thread safety when handling the event.


Method Details

GetBufferSize()

Calculates the recommended buffer size based on the current storage information.

int GetBufferSize()

Returns

Int32

An integer representing the recommended buffer size in bytes.

Remarks

This method provides an optimal buffer size for I/O operations based on the storage object's characteristics. The recommended buffer size may vary depending on the file size, storage type, and platform.


ThrowIfDisposed()

Throws an ObjectDisposedException if this storage object has been disposed.

void ThrowIfDisposed()

Exceptions

ObjectDisposedException

Thrown if the object has already been disposed.

Remarks

This method is typically called at the beginning of public methods to ensure the object is in a valid state before performing operations.


Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

void Dispose()

Remarks

Inherited from IDisposable. After calling this method, the object should not be used. Always call Dispose() when finished with an instance to ensure proper cleanup of resources.


Examples

Working with Storage Objects Polymorphically

The following example demonstrates using IStorageObject to handle both files and directories:

using Rheo.Storage;
using Rheo.Storage.Contracts;

void ProcessStorageObject(IStorageObject storage)
{
    Console.WriteLine($"Name: {storage.Name}");
    Console.WriteLine($"Path: {storage.FullPath}");
    Console.WriteLine($"Parent: {storage.ParentDirectory}");
    Console.WriteLine($"Size: {storage.Information.FormattedSize}");
    Console.WriteLine($"Created: {storage.Information.CreationTime}");
}

using var file = new FileObject(@"C:\Documents\file.txt");
using var directory = new DirectoryObject(@"C:\Documents");

ProcessStorageObject(file);
ProcessStorageObject(directory);

Monitoring Storage Changes

The following example shows how to subscribe to change notifications:

using Rheo.Storage;
using Rheo.Storage.Contracts;

void MonitorDirectory(string path)
{
    using var dir = new DirectoryObject(path);
    
    dir.Changed += (sender, e) =>
    {
        Console.WriteLine($"Change detected: {e.ChangeType}");
        Console.WriteLine($"Path: {e.StorageObject?.FullPath}");
    };
    
    // Keep the application running to monitor changes
    Console.WriteLine("Monitoring directory. Press Enter to exit.");
    Console.ReadLine();
}

MonitorDirectory(@"C:\Documents");

Checking Disposal State

The following example demonstrates checking disposal state:

using Rheo.Storage;
using Rheo.Storage.Contracts;

IStorageObject storage = new FileObject(@"C:\Documents\file.txt");

if (!storage.IsDisposed)
{
    Console.WriteLine($"Processing: {storage.Name}");
}

storage.Dispose();

if (storage.IsDisposed)
{
    Console.WriteLine("Object has been disposed.");
}

// This would throw ObjectDisposedException
try
{
    storage.ThrowIfDisposed();
}
catch (ObjectDisposedException ex)
{
    Console.WriteLine($"Error: {ex.Message}");
}

Using Recommended Buffer Size

The following example shows using the recommended buffer size for I/O operations:

using Rheo.Storage;

using var file = new FileObject(@"C:\Documents\large-file.dat");

int bufferSize = file.GetBufferSize();
Console.WriteLine($"Recommended buffer size: {bufferSize} bytes");

// Use the buffer size for reading
using var stream = file.Read();
byte[] buffer = new byte[bufferSize];

int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
    // Process data
}

Comparing Storage Objects

The following example demonstrates working with multiple storage objects:

using Rheo.Storage;
using Rheo.Storage.Contracts;

void CompareStorageObjects(IStorageObject obj1, IStorageObject obj2)
{
    Console.WriteLine("Comparison:");
    Console.WriteLine($"  {obj1.Name}: {obj1.Information.Size} bytes");
    Console.WriteLine($"  {obj2.Name}: {obj2.Information.Size} bytes");
    
    if (obj1.Information.Size > obj2.Information.Size)
    {
        Console.WriteLine($"{obj1.Name} is larger.");
    }
    else
    {
        Console.WriteLine($"{obj2.Name} is larger or equal.");
    }
}

using var file1 = new FileObject(@"C:\Documents\file1.txt");
using var file2 = new FileObject(@"C:\Documents\file2.txt");

CompareStorageObjects(file1, file2);

Thread Safety

Thread safety guarantees are implementation-specific. Consult the documentation for concrete implementations such as FileObject and DirectoryObject.

See Also

Clone this wiki locally