Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
5 Ways Injecting Multiple Instances of Same Interface on ASP.NET Core
public interface IFeedReader
{
string Name { get; }
string GetSingleFeedTitle();
}
public class BlogFeedReader : IFeedReader
{
public BlogFeedReader()
{
this.Name = "Blog";
}
public string Name { get; }
public string GetSingleFeedTitle()
{
return "This is blog item 1";
}
}
public class PodcastFeedReader : IFeedReader
{
public PodcastFeedReader()
{
this.Name = "Podcast";
}
public string Name { get; }
public string GetSingleFeedTitle()
{
return "This is audio item 1";
}
}
public class YouTubeFeedReader : IFeedReader
{
public YouTubeFeedReader()
{
this.Name = "YouTube";
}
public string Name { get; }
public string GetSingleFeedTitle()
{
return "This is video item 1";
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IFeedReader, BlogFeedReader>();
services.AddTransient<IFeedReader, PodcastFeedReader>();
services.AddTransient<IFeedReader, YouTubeFeedReader>();
}
public class BlogFeedService
{
private readonly IFeedReader _reader;
public BlogFeedService(IEnumerable<IFeedReader> readers)
{
this._reader = readers.SingleOrDefault(p => p.Name == "Blog");
}
public string GetTitle()
{
return this._reader.GetSingleFeedTitle();
}
}
public class BlogFeedService
{
private readonly IEnumerable<IFeedReader> _readers;
public BlogFeedService(IEnumerable<IFeedReader> readers)
{
this._readers = readers;
}
public string GetTitle()
{
foreach (reader in this._readers)
{
if (reader.Name != "Blog")
{
continue;
}
return reader.GetSingleFeedTitle();
}
}
}
public interface IFeedReaderResolver
{
IFeedReader Resolve(string name);
}
public class FeedReaderResolver : IFeedReaderResolver
{
private readonly IServiceProvider _provider;
public FeedReaderResolver(IServiceProvider provider)
{
this._provider = provider;
}
public IFeedReader Resolve(string name)
{
var type = Assembly.GetAssembly(typeof(FeedReaderResolver)).GetType($"{name}FeedReader");
var instance = this._provider.GetService(type);
return instance as IFeedReader;
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<BlogFeedReader>();
services.AddTransient<PodcastFeedReader>();
services.AddTransient<YouTubeFeedReader>();
services.AddTransient<IFeedReaderResolver, FeedReaderResolver>();
}
public class BlogFeedService
{
private readonly IFeedReader _reader;
public BlogFeedService(IFeedReaderResolver resolver)
{
this._reader = resolver.Resolve("Blog");
}
public string GetTitle()
{
return this._reader.GetSingleFeedTitle();
}
}
public class FeedReaderResolver : IFeedReaderResolver
{
public IFeedReader Resolve(string name)
{
var type = Assembly.GetAssembly(typeof(FeedReaderResolver)).GetType($"{name}FeedReader");
var instance = Activator.CreateInstance(type);
return instance as IFeedReader;
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IFeedReaderResolver, FeedReaderResolver>();
}
public delegate IFeedReader FeedReaderDelegate(string name);
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<BlogFeedReader>();
services.AddTransient<PodcastFeedReader>();
services.AddTransient<YouTubeFeedReader>();
services.AddTransient<FeedReaderDelegate>(provider => name =>
{
var type = Assembly.GetAssembly(typeof(FeedReaderResolver)).GetType($"FeedReaders.{name}FeedReader");
var instance = provider.GetService(type);
return instance as IFeedReader;
});
}
public class BlogFeedService
{
private readonly FeedReaderDelegate _delegate;
public BlogFeedService(FeedReaderDelegate @delegate)
{
this._delegate = @delegate;
}
public string GetTitle()
{
return this._delegate("Blog").GetSingleFeedTitle();
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<BlogFeedReader>();
services.AddTransient<PodcastFeedReader>();
services.AddTransient<YouTubeFeedReader>();
services.AddTransient<Func<string, IFeedReader>>(provider => name =>
{
var type = Assembly.GetAssembly(typeof(FeedReaderResolver)).GetType($"FeedReaders.{name}FeedReader");
var instance = provider.GetService(type);
return instance as IFeedReader;
});
}
public class BlogFeedService
{
private readonly Func<string, IFeedReader> _func;
public BlogFeedService(Func<string, IFeedReader> func)
{
this._func = func;
}
public string GetTitle()
{
return this._func("Blog").GetSingleFeedTitle();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.