Skip to content

Instantly share code, notes, and snippets.

@vdovinanton
Last active February 12, 2018 11:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdovinanton/5bfd48c7fb31d7a933936b6042a24019 to your computer and use it in GitHub Desktop.
Save vdovinanton/5bfd48c7fb31d7a933936b6042a24019 to your computer and use it in GitHub Desktop.
try to help
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();
}
}
}
@vdovinanton
Copy link
Author

like describe:
C# 7 syntax , indexers, used auto getters a lot of times, the pattern matching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment