Home Abstract Class and Async, should I make a new base class?
Reply: 1

Abstract Class and Async, should I make a new base class?

Ambidex
1#
Ambidex Published in 2017-12-07 04:34:16Z

I have a base class that I will be using to iterate though files and directories as shown here:

public abstract class FileRecursionBase
{
    protected DirectoryInfo Info;

    protected FileRecursionBase(string sourceDirectory, string targetDirectory)
    {
        SetSourceDirectory(sourceDirectory);
        TargetDirectory = targetDirectory;
    }

    private string _sourceDirectory;

    protected string GetSourceDirectory()
    {
        return _sourceDirectory;
    }

    protected void SetSourceDirectory(string value)
    {
        _sourceDirectory = value;
        Info =  new DirectoryInfo(value);
    }

    protected string TargetDirectory { get; set; }

    protected abstract void ProcessFile(FileInfo file);
    protected abstract void ProcessDirectory(DirectoryInfo directory);

    protected void EnumerateFiles()
    {
        foreach (var file in Info.EnumerateFiles())
        {
            ProcessFile(file);
        }
    }

    protected void EnumerateDirectories()
    {
        foreach (var directory in Info.EnumerateDirectories())
        {
            ProcessDirectory(directory);
        }
    }

    public virtual void Initialize()
    {
        EnumerateDirectories();
    }
}

And for simplicity let's make a simple counting class that implements this class

public class FileCounter : FileRecursionBase
{
    protected ICounter Counter;


    public FileCounter(string sourceDirectory, string targetDirectory, ICounter counter) : base(sourceDirectory, targetDirectory)
    {
        Counter = counter;
    }

    protected override void ProcessFile(FileInfo file)
    {
        Counter.Files++;
        Counter.FileSize += file.Length;
    }

    protected override void ProcessDirectory(DirectoryInfo directory)
    {
        SetSourceDirectory(directory.FullName);
        try
        {
            EnumerateFiles();
            EnumerateDirectories();
        }
        catch (Exception)
        {
            //ignore
        }
    }
}

The class works as intended and recursively enumerates based off the ProcessDirectory and ProcessFiles methods.

However, what can I do for cases where I wanted to ProcessFiles asynchronously? Since the abstract class is using protected methods, I cannot await a void on the Initialization.

Would I have to change the entire base class to use Tasks, even if the class has children that isn't asynchronous?

Or is the correct way is to just create a new base class that uses Tasks and copy and paste most of the code? Just seeing if anyone had ideas or if I am overlooking something.

Ambidex
2#
Ambidex Reply to 2017-12-07 05:07:15Z

Actually on second thought, there really isn't a scenario where I would need to make the base methods asynchronous.

I can simply make both ProcessFile and ProcessDirectory async in the child class.

The vast majority of async tasks are I/O bound in nature, so it should not lock the UI thread.

If in the event it does, by best practice I would call the blocking code in a Task.Run since it is CPU bound code at that point; changing the base class to Task methods wouldn't help any at all because it's running in a separate thread at that point.

Sorry, for wasting people's time.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.309082 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO