C
C#2w ago
SWEETPONY

✅ The interface has static abstract members without implementations. How to fix?

I have this interface:
public interface IEventProcessor
{
public static abstract IReadOnlyCollection<string> HandledEvents { get; }
public Task<CommandResult> ExecuteAsync(EventProcessorParameter parameter, EventHandlingContext context);
}
public interface IEventProcessor
{
public static abstract IReadOnlyCollection<string> HandledEvents { get; }
public Task<CommandResult> ExecuteAsync(EventProcessorParameter parameter, EventHandlingContext context);
}
Factory:
public sealed class EventProcessorFactory(IServiceProvider provider): IEventProcessorFactory
{
private static readonly Lazy<Dictionary<string, Type>> processors = new(DiscoverEventProcessors);

public IEventProcessor? GetProcessor(string eventName)
{
if(!processors.Value.TryGetValue(eventName, out var type))
return null;

return (IEventProcessor)provider.GetRequiredService(type);
}

private static Dictionary<string, Type> DiscoverEventProcessors()
{
var processorTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => typeof(IEventProcessor).IsAssignableFrom(t) && t.IsClass)
.ToArray();

var discoverEventProcessors = new Dictionary<string, Type>();

foreach(var processorType in processorTypes)
{
var handledEvents = processorType
.GetProperty(nameof(IEventProcessor.HandledEvents))
?.GetValue(null) as IReadOnlyCollection<string>;

foreach(var handledEvent in handledEvents ?? [])
discoverEventProcessors[handledEvent] = processorType;
}

return discoverEventProcessors;
}
}
public sealed class EventProcessorFactory(IServiceProvider provider): IEventProcessorFactory
{
private static readonly Lazy<Dictionary<string, Type>> processors = new(DiscoverEventProcessors);

public IEventProcessor? GetProcessor(string eventName)
{
if(!processors.Value.TryGetValue(eventName, out var type))
return null;

return (IEventProcessor)provider.GetRequiredService(type);
}

private static Dictionary<string, Type> DiscoverEventProcessors()
{
var processorTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => typeof(IEventProcessor).IsAssignableFrom(t) && t.IsClass)
.ToArray();

var discoverEventProcessors = new Dictionary<string, Type>();

foreach(var processorType in processorTypes)
{
var handledEvents = processorType
.GetProperty(nameof(IEventProcessor.HandledEvents))
?.GetValue(null) as IReadOnlyCollection<string>;

foreach(var handledEvent in handledEvents ?? [])
discoverEventProcessors[handledEvent] = processorType;
}

return discoverEventProcessors;
}
}
I can't remove static because without it I won't be able to get the meanings through reflection. I see error when try to put this interface inside List: List<IEventProcessor>
1 Reply
SWEETPONY
SWEETPONYOP2w ago
I can remove abstract from interface but then.. Processors can be without HandledEvents property (maybe someone forgot about it)

Did you find this page helpful?