Skip to content

Commit 4cc98ba

Browse files
committed
- Refactor FilePathFilter to support enumeration by using yield return
- Refactor `EventAggregator`. Replace all `Action` delegates in API signatures with `EventHandler<T>`
1 parent b4dbaf1 commit 4cc98ba

File tree

4 files changed

+114
-97
lines changed

4 files changed

+114
-97
lines changed

BionicCode.Net/BionicCode.Utilities.Net.Standard/EventAggregator.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public bool TryRegisterObserver(string eventName, Type eventSourceType, Delegate
126126
}
127127

128128
/// <inheritdoc />
129-
public bool TryRegisterObserver<TEventSource>(string eventName, Type eventSourceType, Action<object, TEventSource> eventHandler) => TryRegisterObserver(eventName, eventSourceType, (Delegate) eventHandler);
129+
public bool TryRegisterObserver<TEventArgs>(string eventName, Type eventSourceType, EventHandler<TEventArgs> eventHandler) => TryRegisterObserver(eventName, eventSourceType, (Delegate) eventHandler);
130130

131131
/// <inheritdoc />
132132
public bool TryRegisterGlobalObserver(string eventName, Delegate eventHandler)
@@ -137,7 +137,7 @@ public bool TryRegisterGlobalObserver(string eventName, Delegate eventHandler)
137137
}
138138

139139
/// <inheritdoc />
140-
public bool TryRegisterGlobalObserver<TEventArgs>(string eventName, Action<object, TEventArgs> eventHandler)
140+
public bool TryRegisterGlobalObserver<TEventArgs>(string eventName, EventHandler<TEventArgs> eventHandler)
141141
{
142142
Type normalizedEventHandlerType = NormalizeEventHandlerType<TEventArgs>(eventHandler.GetType());
143143
var fullyQualifiedEventName = CreateFullyQualifiedEventIdOfGlobalSource(normalizedEventHandlerType, eventName);
@@ -153,7 +153,7 @@ public bool TryRegisterGlobalObserver(Delegate eventHandler)
153153
}
154154

155155
/// <inheritdoc />
156-
public bool TryRegisterGlobalObserver<TEventArgs>(Action<object, TEventArgs> eventHandler)
156+
public bool TryRegisterGlobalObserver<TEventArgs>(EventHandler<TEventArgs> eventHandler)
157157
{
158158
Type normalizedEventHandlerType = NormalizeEventHandlerType<TEventArgs>(eventHandler.GetType());
159159
var fullyQualifiedEventName = CreateFullyQualifiedEventIdOfGlobalSource(normalizedEventHandlerType, string.Empty);
@@ -169,10 +169,10 @@ public bool TryRemoveObserver(string eventName, Type eventSourceType, Delegate e
169169
}
170170

171171
/// <inheritdoc />
172-
public bool TryRemoveObserver<TEventSource>(
172+
public bool TryRemoveObserver<TEventArgs>(
173173
string eventName,
174174
Type eventSourceType,
175-
Action<object, TEventSource> eventHandler) =>
175+
EventHandler<TEventArgs> eventHandler) =>
176176
TryRemoveObserver(eventName, eventSourceType, (Delegate) eventHandler);
177177

178178
/// <inheritdoc />
@@ -187,7 +187,7 @@ public bool TryRemoveGlobalObserver(string eventName, Delegate eventHandler)
187187

188188

189189
/// <inheritdoc />
190-
public bool TryRemoveGlobalObserver<TEventArgs>(string eventName, Action<object, TEventArgs> eventHandler)
190+
public bool TryRemoveGlobalObserver<TEventArgs>(string eventName, EventHandler<TEventArgs> eventHandler)
191191
{
192192
Type normalizedEventHandlerType = NormalizeEventHandlerType< TEventArgs>(eventHandler.GetType());
193193

@@ -206,7 +206,7 @@ public bool TryRemoveGlobalObserver(Delegate eventHandler)
206206
}
207207

208208
/// <inheritdoc />
209-
public bool TryRemoveGlobalObserver<TEventArgs>(Action<object, TEventArgs> eventHandler)
209+
public bool TryRemoveGlobalObserver<TEventArgs>(EventHandler<TEventArgs> eventHandler)
210210
{
211211
Type normalizedEventHandlerType = NormalizeEventHandlerType< TEventArgs>(eventHandler.GetType());
212212
return TryRemoveGlobalObserverInternal(normalizedEventHandlerType);
@@ -391,7 +391,7 @@ private Type NormalizeEventHandlerType(Type eventHandlerType) =>
391391
private Type NormalizeEventHandlerType<TEventArgs>(Type eventHandlerType) =>
392392
eventHandlerType == typeof(EventHandler) || eventHandlerType == typeof(Action<object, EventArgs>)
393393
? typeof(EventHandler<EventArgs>)
394-
: eventHandlerType == typeof(Action<object, TEventArgs>)
394+
: eventHandlerType == typeof(EventHandler<TEventArgs>)
395395
? typeof(EventHandler<TEventArgs>)
396396
: eventHandlerType;
397397

BionicCode.Net/BionicCode.Utilities.Net.Standard/IEventAggregator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public interface IEventAggregator
3030
/// <param name="eventSourceType">The type of the observable.</param>
3131
/// <param name="eventHandler">A delegate that handles the specified event.</param>
3232
/// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
33-
bool TryRegisterObserver<TEventArgs>(string eventName, Type eventSourceType, Action<object, TEventArgs> eventHandler);
33+
bool TryRegisterObserver<TEventArgs>(string eventName, Type eventSourceType, EventHandler<TEventArgs> eventHandler);
3434

3535
/// <summary>
3636
/// Register an event delegate to handle a specific event which could be published by any type.
@@ -47,7 +47,7 @@ public interface IEventAggregator
4747
/// <param name="eventName">The name of the observed event.</param>
4848
/// <param name="eventHandler">A delegate that handles the specified event.</param>
4949
/// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
50-
bool TryRegisterGlobalObserver<TEventArgs>(string eventName, Action<object, TEventArgs> eventHandler);
50+
bool TryRegisterGlobalObserver<TEventArgs>(string eventName, EventHandler<TEventArgs> eventHandler);
5151

5252
/// <summary>
5353
/// Registers a handler for any registered event source with a compatible event delegate signature.
@@ -62,7 +62,7 @@ public interface IEventAggregator
6262
/// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
6363
/// <param name="eventHandler">A delegate that handles the specified event.</param>
6464
/// <returns><c>true</c> when registration was successful, otherwise <c>false</c>.</returns>
65-
bool TryRegisterGlobalObserver<TEventArgs>(Action<object, TEventArgs> eventHandler);
65+
bool TryRegisterGlobalObserver<TEventArgs>(EventHandler<TEventArgs> eventHandler);
6666

6767
/// <summary>
6868
/// Unregister the event publisher for a collection of specified events.
@@ -101,7 +101,7 @@ bool TryRemoveObservable(
101101
/// <param name="eventSourceType">The type of the event publisher.</param>
102102
/// <param name="eventHandler">The event handler to remove.</param>
103103
/// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
104-
bool TryRemoveObserver<TEventArgs>(string eventName, Type eventSourceType, Action<object, TEventArgs> eventHandler);
104+
bool TryRemoveObserver<TEventArgs>(string eventName, Type eventSourceType, EventHandler<TEventArgs> eventHandler);
105105

106106
/// <summary>
107107
/// Removes all event handlers for a specified event no matter event publisher type.
@@ -140,7 +140,7 @@ bool TryRemoveObservable(
140140
/// <param name="eventName">The event name of the event that the delegate is handling.</param>
141141
/// <param name="eventHandler">The event handler to remove</param>
142142
/// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
143-
bool TryRemoveGlobalObserver<TEventArgs>(string eventName, Action<object, TEventArgs> eventHandler);
143+
bool TryRemoveGlobalObserver<TEventArgs>(string eventName, EventHandler<TEventArgs> eventHandler);
144144

145145
/// <summary>
146146
/// Removes the event handler for all registered events with a compatible event delegate signature.
@@ -155,6 +155,6 @@ bool TryRemoveObservable(
155155
/// <typeparam name="TEventArgs">The type of the event args object.</typeparam>
156156
/// <param name="eventHandler">The event handler to remove.</param>
157157
/// <returns><c>true</c> when removal was successful, otherwise <c>false</c>.</returns>
158-
bool TryRemoveGlobalObserver<TEventArgs>(Action<object, TEventArgs> eventHandler);
158+
bool TryRemoveGlobalObserver<TEventArgs>(EventHandler<TEventArgs> eventHandler);
159159
}
160160
}

BionicCode.Net/BionicCode.Utilities.Net.Standard/IO/FilePathFilter.cs

Lines changed: 35 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ namespace BionicCode.Utilities.Net.Standard.IO
77
{
88
public class FilePathFilter : IFilePathFilter
99
{
10-
public IEnumerable<string> FilterFilePathsFromFolder(
10+
/// <inheritdoc />
11+
public IEnumerable<FileInfo> EnumerateFilesInFolder(
1112
string folderPath,
1213
FileExtensions fileExtensionsToCollect,
1314
bool isIncludingSubdirectories)
@@ -33,141 +34,95 @@ public IEnumerable<string> FilterFilePathsFromFolder(
3334
currentDirectorySearchOption);
3435
}
3536

36-
protected IEnumerable<string> CollectFilePathsFromFolderByFileExtension(
37+
protected IEnumerable<FileInfo> CollectFilePathsFromFolderByFileExtension(
3738
string folderPath,
3839
FileExtensions fileExtensionsToCollect,
3940
SearchOption directorySearchOption)
4041
{
4142
if (!Directory.Exists(folderPath))
4243
{
43-
return new List<string>();
44+
yield break;
4445
}
4546

4647
var directoryInfo = new DirectoryInfo(folderPath);
47-
var fileInfoCollection = new List<FileInfo>();
4848

49-
List<string> extensionsToCollect = fileExtensionsToCollect.ToString().Split(',').ToList();
49+
IEnumerable<string> extensionsToCollect = fileExtensionsToCollect.ToString().Split(',');
5050
foreach (string fileExtension in extensionsToCollect)
5151
{
52-
fileInfoCollection.AddRange(directoryInfo.GetFiles("*." + fileExtension, directorySearchOption));
52+
foreach (FileInfo fileInfo in directoryInfo.EnumerateFiles("*." + fileExtension, directorySearchOption))
53+
{
54+
yield return fileInfo;
55+
}
5356
}
54-
55-
return (from fileInfo in fileInfoCollection select fileInfo.FullName).ToList();
5657
}
5758

58-
protected IEnumerable<string> CollectAllFilePathsIgnoreFileExtension(
59+
protected IEnumerable<FileInfo> CollectAllFilePathsIgnoreFileExtension(
5960
string folderPath,
6061
SearchOption directorySearchOption)
6162
{
6263
if (!Directory.Exists(folderPath))
6364
{
64-
return new List<string>();
65+
return new List<FileInfo>();
6566
}
6667

6768
var directoryInfo = new DirectoryInfo(folderPath);
68-
List<FileInfo> fileInfoCollection =
69-
directoryInfo.GetFiles("*", directorySearchOption).Where(fileInfo => fileInfo.Exists).ToList();
70-
return (from fileInfo in fileInfoCollection select fileInfo.FullName).ToList();
69+
return
70+
directoryInfo.EnumerateFiles("*", directorySearchOption);
7171
}
7272

73-
/// <summary>
74-
/// Extracts valid paths or paths with a specified extension from a collection of paths.
75-
/// The path collection can be a mix-up of files and folders. In case the path describes a folder, the extension filter
76-
/// will be applied to all containing files.
77-
/// </summary>
78-
/// <param name="pathEntries">A string collection holding folder and/ or file paths filter.</param>
79-
/// <param name="fileExtensionsToCollect">
80-
/// A flagged Enum type that defines one or more extensions to filter from the
81-
/// collection. <see cref="FileExtensions" />
82-
/// </param>
83-
/// <param name="isIncludingSubdirectories">
84-
/// Sets the filter whether to apply to sub directories or not.
85-
/// <c>True</c> includes subdirectories and <c>False</c> ignores them.
86-
/// If value is passed the parameter defaults to <c>Tue</c>.
87-
/// </param>
88-
/// <returns>IEnumerable</returns>
89-
/// <remarks>
90-
/// To ignore file extensions and collect all files found specify the <c>any</c> file extension.
91-
/// <see cref="FileExtensions" />
92-
/// </remarks>
93-
public IEnumerable<string> FilterFilePathsFromMixedPathsIncludingFolders(
73+
/// <inheritdoc />
74+
public IEnumerable<FileInfo> EnumerateFilesIncludingFolders(
9475
IEnumerable<string> pathEntries,
9576
FileExtensions fileExtensionsToCollect,
96-
bool isIncludingSubdirectories)
77+
bool isIncludingSubdirectories = false)
9778
{
98-
var filePathCollection = new List<string>();
9979
foreach (string path in pathEntries)
10080
{
10181
if (Directory.Exists(path))
10282
{
103-
filePathCollection.AddRange(
104-
CollectFilePathsFromFolderByFileExtension(
105-
path,
106-
fileExtensionsToCollect,
107-
isIncludingSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly));
83+
foreach (FileInfo fileInfo in CollectFilePathsFromFolderByFileExtension(
84+
path,
85+
fileExtensionsToCollect,
86+
isIncludingSubdirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
87+
{
88+
yield return fileInfo;
89+
}
10890
}
10991
else if (File.Exists(path))
11092
{
11193
var fileInfo = new FileInfo(path);
112-
FileExtensions foundFileExtension;
113-
if (Enum.TryParse(fileInfo.Extension.Substring(1), true, out foundFileExtension) &&
94+
if (Enum.TryParse(fileInfo.Extension.Substring(1), true, out FileExtensions foundFileExtension) &&
11495
fileExtensionsToCollect.HasFlag(foundFileExtension))
11596
{
116-
filePathCollection.Add(path);
97+
yield return fileInfo;
11798
}
11899
}
119100
}
120-
121-
return filePathCollection;
122101
}
123102

124-
/// <summary>
125-
/// Extracts valid paths or paths with a specified extension from a collection of paths.
126-
/// The path collection can be a mix-up of files and folders. In case the path describes a folder, the filter will ignore
127-
/// it including all containing files.
128-
/// </summary>
129-
/// <param name="pathEntries">A string collection holding folder and/ or file paths filter.</param>
130-
/// <param name="fileExtensionsToCollect">
131-
/// A flagged Enum type that defines one or more extensions to filter from the
132-
/// collection. <see cref="FileExtensions" />
133-
/// </param>
134-
/// <returns>IEnumerable</returns>
135-
/// <remarks>
136-
/// To ignore file extensions and collect all files found specify the <c>any</c> file extension.
137-
/// <see cref="FileExtensions" />
138-
/// </remarks>
139-
public IEnumerable<string> FilterFilePathsFromMixedPathsIgnoringFolders(
103+
/// <inheritdoc />
104+
public IEnumerable<FileInfo> EnumerateFilesIgnoringFolders(
140105
IEnumerable<string> pathEntries,
141106
FileExtensions fileExtensionsToCollect)
142107
{
143-
var filePathCollection = new List<string>();
144108
foreach (string path in pathEntries)
145109
{
146110
if (Directory.Exists(path))
147111
{
148112
continue;
149113
}
150114

151-
if (File.Exists(path))
115+
if (!File.Exists(path))
152116
{
153-
FilePathFilter.CollectFilePaths(fileExtensionsToCollect, path, filePathCollection);
117+
continue;
154118
}
155-
}
156-
157-
return filePathCollection;
158-
}
159119

160-
private static void CollectFilePaths(
161-
FileExtensions fileExtensionsToCollect,
162-
string path,
163-
List<string> filePathCollection)
164-
{
165-
var fileInfo = new FileInfo(path);
166-
FileExtensions currentFileExtension;
167-
if (Enum.TryParse(fileInfo.Extension, true, out currentFileExtension) &&
168-
fileExtensionsToCollect.HasFlag(currentFileExtension))
169-
{
170-
filePathCollection.Add(path);
120+
var fileInfo = new FileInfo(path);
121+
if (Enum.TryParse(fileInfo.Extension, true, out FileExtensions currentFileExtension) &&
122+
fileExtensionsToCollect.HasFlag(currentFileExtension))
123+
{
124+
yield return fileInfo;
125+
}
171126
}
172127
}
173128
}
Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,73 @@
11
using System.Collections.Generic;
2+
using System.IO;
23

34
namespace BionicCode.Utilities.Net.Standard.IO
45
{
56
public interface IFilePathFilter
67
{
7-
IEnumerable<string> FilterFilePathsFromFolder(string folderPath, FileExtensions fileExtensionsToCollect, bool isIncludingSubdirectories);
8-
IEnumerable<string> FilterFilePathsFromMixedPathsIncludingFolders(IEnumerable<string> pathEntries, FileExtensions fileExtensionsToCollect, bool isIncludingSubdirectories);
9-
IEnumerable<string> FilterFilePathsFromMixedPathsIgnoringFolders(IEnumerable<string> pathEntries, FileExtensions fileExtensionsToCollect);
8+
/// <summary>
9+
/// Extracts valid paths or paths with a specified extension from a collection of paths.
10+
/// The path collection can be a mix-up of files and folders. In case the path describes a folder, the extension filter
11+
/// will be applied to all containing files.
12+
/// </summary>
13+
/// <param name="folderPath">The path to the folder to filter.</param>
14+
/// <param name="fileExtensionsToCollect">
15+
/// The flagged Enum type <see cref="FileExtensions" /> that defines one or more extensions to filter from the folder.
16+
/// </param>
17+
/// <param name="isIncludingSubdirectories">
18+
/// Sets the filter whether to apply to sub directories or not.
19+
/// <c>True</c> includes subdirectories and <c>False</c> ignores them.
20+
/// Is <c>false</c> by default.
21+
/// </param>
22+
/// <returns>A enumerable collection of <see cref="FileInfo"/>.</returns>
23+
/// <remarks>
24+
/// To ignore file extensions and collect all files found pass the <see cref="FileExtensions.Any" /> value.
25+
///
26+
/// </remarks>
27+
IEnumerable<FileInfo> EnumerateFilesInFolder(
28+
string folderPath,
29+
FileExtensions fileExtensionsToCollect,
30+
bool isIncludingSubdirectories);
31+
32+
/// <summary>
33+
/// Extracts valid paths or paths with a specified extension from a collection of paths.
34+
/// The path collection can be a mix-up of files and folders. In case the path describes a folder, the extension filter
35+
/// will be applied to all containing files.
36+
/// </summary>
37+
/// <param name="pathEntries">A string collection holding folder and/ or file paths filter.</param>
38+
/// <param name="fileExtensionsToCollect">
39+
/// The flagged Enum type <see cref="FileExtensions" /> that defines one or more extensions to filter from the folder.
40+
/// </param>
41+
/// <param name="isIncludingSubdirectories">
42+
/// Sets the filter whether to apply to sub directories or not.
43+
/// <c>True</c> includes subdirectories and <c>False</c> ignores them.
44+
/// Is <c>false</c> by default.
45+
/// </param>
46+
/// <returns>A enumerable collection of <see cref="FileInfo"/>.</returns>
47+
/// <remarks>
48+
/// To ignore file extensions and collect all files found pass the <see cref="FileExtensions.Any" /> value.
49+
///
50+
/// </remarks>
51+
IEnumerable<FileInfo> EnumerateFilesIncludingFolders(
52+
IEnumerable<string> pathEntries,
53+
FileExtensions fileExtensionsToCollect,
54+
bool isIncludingSubdirectories);
55+
56+
/// <summary>
57+
/// Extracts valid paths or paths with a specified extension from a collection of paths.
58+
/// The path collection can be a mix-up of files and folders. In case the path describes a folder, the filter will ignore
59+
/// it including all containing files.
60+
/// </summary>
61+
/// <param name="pathEntries">A string collection holding folder and/ or file paths filter.</param>
62+
/// <param name="fileExtensionsToCollect">
63+
/// The flagged Enum type <see cref="FileExtensions" /> that defines one or more extensions to filter from the folder.
64+
/// </param>
65+
/// <returns>A enumerable collection of <see cref="FileInfo"/>.</returns>
66+
/// <remarks>
67+
/// To ignore file extensions and collect all files found pass the <see cref="FileExtensions.Any"/> value.
68+
/// </remarks>
69+
IEnumerable<FileInfo> EnumerateFilesIgnoringFolders(
70+
IEnumerable<string> pathEntries,
71+
FileExtensions fileExtensionsToCollect);
1072
}
1173
}

0 commit comments

Comments
 (0)