Last active
July 2, 2020 02:28
-
-
Save justinyoo/e5b18f907f6a1b22614c4e487cfe185f to your computer and use it in GitHub Desktop.
5 Ways Injecting Multiple Instances of Same Interface on ASP.NET Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddTransient<IFeedReader, BlogFeedReader>(); | |
services.AddTransient<IFeedReader, PodcastFeedReader>(); | |
services.AddTransient<IFeedReader, YouTubeFeedReader>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddTransient<BlogFeedReader>(); | |
services.AddTransient<PodcastFeedReader>(); | |
services.AddTransient<YouTubeFeedReader>(); | |
services.AddTransient<IFeedReaderResolver, FeedReaderResolver>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlogFeedService | |
{ | |
private readonly IFeedReader _reader; | |
public BlogFeedService(IFeedReaderResolver resolver) | |
{ | |
this._reader = resolver.Resolve("Blog"); | |
} | |
public string GetTitle() | |
{ | |
return this._reader.GetSingleFeedTitle(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddTransient<IFeedReaderResolver, FeedReaderResolver>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public delegate IFeedReader FeedReaderDelegate(string name); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlogFeedService | |
{ | |
private readonly FeedReaderDelegate _delegate; | |
public BlogFeedService(FeedReaderDelegate @delegate) | |
{ | |
this._delegate = @delegate; | |
} | |
public string GetTitle() | |
{ | |
return this._delegate("Blog").GetSingleFeedTitle(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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