TransactionProcessing / Shared

0 stars 0 forks source link

Changes to TypeProvider.LoadDomainEventsTypeDynamically #261

Closed StuartFerguson closed 7 months ago

StuartFerguson commented 7 months ago

New Code

public static void LoadDomainEventsTypeDynamically(Assembly[] assemblies)
{
    IEnumerable<Type> allTypes = assemblies.SelectMany(a => a.GetTypes());

    List<Type> filteredTypes = allTypes
                               .Where(t => t.IsSubclassOf(typeof(DomainEvent)))
                               .OrderBy(e => e.Name).ToList();

    foreach (Type type in filteredTypes)
    {
        TypeMap.AddType(type, type.Name);
    }
}

Code example to call this

Assembly[] assemblies = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*DomainEvents*.dll")
                                 .Select(x => Assembly.Load(AssemblyName.GetAssemblyName(x))).ToArray();
LoadDomainEventsTypeDynamically(assemblies);