Skip to content

Commit

Permalink
ModuleContainer review & documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
khalidsalomao committed Jun 7, 2016
1 parent 2d89498 commit 539fab3
Showing 1 changed file with 50 additions and 20 deletions.
70 changes: 50 additions & 20 deletions SimpleHelpers.ModuleContainer/ModuleContainer.cs
Expand Up @@ -64,20 +64,12 @@ class ModuleInfo
public delegate void ModuleErrorEventHandler (string message, Exception ex);

public event ModuleErrorEventHandler OnModuleLoadingError;

private bool HasEventListeners ()
{
if (OnModuleLoadingError != null)
{
return OnModuleLoadingError.GetInvocationList ().Length != 0;
}
return false;
}

private void FireModuleLoadingError (string message, Exception ex)

private void RaiseModuleLoadingError (string message, Exception ex)
{
if (HasEventListeners ())
OnModuleLoadingError (message, ex);
var localHandler = OnModuleLoadingError;
if (localHandler != null)
localHandler (message, ex);
}

#endregion
Expand Down Expand Up @@ -121,6 +113,16 @@ public void LoadModules (string modulesFolder, Type[] listOfInterfaces = null)
ContainerInitialization (new string[] { modulesFolder }, listOfInterfaces);
}

/// <summary>
/// Registers a interface by searching all derived types.<para/>
/// This method will rebuild the internal cache for a given type.
/// </summary>
/// <param name="listOfInterfaces">The list of interfaces.</param>
public void RegisterInterface (params Type[] listOfInterfaces)
{
SearchForImplementations (listOfInterfaces);
}

private void ContainerInitialization (string[] modulesFolder, Type[] listOfInterfaces)
{
lock (padlock)
Expand Down Expand Up @@ -218,7 +220,7 @@ private void LoadAssembly (FileInfo file, HashSet<string> parsedAssemblies)
}
catch (Exception ex)
{
FireModuleLoadingError ("Assembly ignored. Load error: " +
RaiseModuleLoadingError ("Assembly ignored. Load error: " +
ex.GetType ().Name + ", location: " + file.FullName.Replace (AppDomain.CurrentDomain.BaseDirectory, "./").Replace ('\\', '/'), ex);
return;
}
Expand All @@ -240,7 +242,7 @@ private void LoadAssembly (FileInfo file, HashSet<string> parsedAssemblies)
}
catch (Exception ex)
{
FireModuleLoadingError ("Assembly ignored. Load error: " +
RaiseModuleLoadingError ("Assembly ignored. Load error: " +
ex.GetType ().Name + ", location: " + file.FullName.Replace (AppDomain.CurrentDomain.BaseDirectory, "./").Replace ('\\', '/'), ex);
}
}
Expand Down Expand Up @@ -303,7 +305,7 @@ private void SearchForImplementations (IList<Type> listOfInterfaces)
}
catch (Exception ex)
{
FireModuleLoadingError ("Error loading assembly types.", ex);
RaiseModuleLoadingError ("Error loading assembly types.", ex);
}
}

Expand All @@ -327,7 +329,7 @@ private IEnumerable<Type> SearchForType (IList<string> fullTypeNameList)
{
foreach (var name in fullTypeNameList)
{
if (name == t.FullName)
if (name == t.FullName)
yield return t;
}
}
Expand Down Expand Up @@ -355,7 +357,7 @@ private IEnumerable<Type> ListLoadedAssemblyTypes()
}
catch (Exception ex)
{
FireModuleLoadingError ("Assembly .net version lower than .net 4.5. Assembly: " + a.FullName + ", location: " + a.Location.Replace (AppDomain.CurrentDomain.BaseDirectory, "./").Replace ('\\', '/'), ex);
RaiseModuleLoadingError ("Assembly .net version lower than .net 4.5. Assembly: " + a.FullName + ", location: " + a.Location.Replace (AppDomain.CurrentDomain.BaseDirectory, "./").Replace ('\\', '/'), ex);
}

// check if types were listed!
Expand Down Expand Up @@ -404,10 +406,11 @@ private static string ParseFirstNamespace (string name)
int i = name.IndexOf ('.');
return (i > 0) ? name.Substring (0, i) : name;
}

/// <summary>
/// Adjust file path.
/// </summary>
public static Tuple<string, string> PrepareFilePath (string path)
private static Tuple<string, string> PrepareFilePath (string path)
{
if (String.IsNullOrWhiteSpace (path))
return null;
Expand Down Expand Up @@ -438,7 +441,7 @@ private static string ParseFirstNamespace (string name)
return s;
}

public static Tuple<string, string> SplitByLastPathPart (string pattern)
private static Tuple<string, string> SplitByLastPathPart (string pattern)
{
if (pattern != null)
{
Expand Down Expand Up @@ -615,6 +618,33 @@ public IEnumerable<Type> GetTypesOf (Type type)
yield return list[i];
}

/// <summary>
/// Gets all registered types for a given interface or base type.
/// </summary>
/// <param name="typeName">Name of the type.</param>
/// <returns>List of registered types</returns>
public IEnumerable<Type> GetTypesOf (string typeName)
{
List<Type> list;
// load interface implementations
if (!exportedTypesByBaseType.TryGetValue (typeName, out list))
{
// if none was found, it was not initilized yet...
var tp = SearchForType (typeName);
if (tp != null)
{
foreach (var t in GetTypesOf (tp))
yield return t;
}
}
else
{
// return implementations
for (int i = 0; i < list.Count; i++)
yield return list[i];
}
}

/// <summary>
/// Gets the constructor for the desired type.
/// </summary>
Expand Down

0 comments on commit 539fab3

Please sign in to comment.