Last active
February 12, 2018 11:56
-
-
Save vdovinanton/5bfd48c7fb31d7a933936b6042a24019 to your computer and use it in GitHub Desktop.
try to help
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
using System; | |
using System.Collections.Generic; | |
namespace index_property | |
{ | |
#region resource implementations | |
public interface IResourceManager | |
{ | |
void UseSomething(); | |
} | |
public class ResourceRub : IResourceManager | |
{ | |
public void UseSomething() | |
{ | |
Console.WriteLine("Gets RUB"); | |
} | |
} | |
public class ResourceEUR : IResourceManager | |
{ | |
public void UseSomething() | |
{ | |
Console.WriteLine("Gets EURO"); | |
} | |
} | |
#endregion | |
public interface IService | |
{ | |
Action this[string index] { get; } | |
} | |
public class CurrencyService : IService | |
{ | |
private readonly IDictionary<string, IResourceManager> _values; | |
public CurrencyService(IEnumerable<IResourceManager> managers) => _values = FillDictionary(managers); | |
private IDictionary<string, IResourceManager> FillDictionary(IEnumerable<IResourceManager> resources) | |
{ | |
var result = new Dictionary<string, IResourceManager>(); | |
foreach (var resource in resources) | |
switch (resource) | |
{ | |
case ResourceRub rb: result.Add("UsdRub", rb); break; | |
case ResourceEUR re: result.Add("UsdEur", re); break; | |
} | |
return result; | |
} | |
public Action this[string index] => _values[index].UseSomething; | |
} | |
public class ServiceManager | |
{ | |
private readonly IService _cService; | |
public ServiceManager(IService service) => _cService = service; | |
public IService Foo => _cService; | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var managersList = new List<IResourceManager> | |
{ | |
new ResourceRub(), | |
new ResourceEUR() | |
}; | |
var cService = new CurrencyService(managersList); | |
var serviceManager = new ServiceManager(cService); | |
serviceManager.Foo["UsdRub"].Invoke(); | |
serviceManager.Foo["UsdEur"].Invoke(); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
like describe:
C# 7 syntax , indexers, used auto getters a lot of times, the pattern matching.