// Каждая локаль должна иметь в себе словарь с переводами
public interface class ILocale {
Dictionary<string, string> Translation { get; }
}
// реализации разных локалей
public class English : ILocale {
public Dictionary<string, string> Translation {get; set;} = new Dictionary<string, string> {
["test"] = "Test"
};
}
public class Russian : ILocale {
public Dictionary<string, string> Translation {get; set;} = new Dictionary<string, string> {
["test"] = "Тест"
};
}
public class Ukrainian : ILocale {
public Dictionary<string, string> Translation {get; set;} = new Dictionary<string, string> {
["test"] = "Тест"
};
}
public class Kazakh : ILocale{
public Dictionary<string, string> Translation {get; set;} = new Dictionary<string, string> {
["test"] = "Сынақ"
};
}
namespace TestLib{
class Program {
static void Main(string[] args) {
ILocale locale = GetLocale();
WriteLine(locale.Translation["test"]);
ReadKey();
}
}
private static ILocale GetLocale() {
// Мы можем получать ее динамически из конфига
// но вызывающий код знает лишь, что это будет некая локаль, но какая
// точно - неизвестно
return Configuration.GetLocaleFromXml(); // псевдокод
}
}
<div id="form-container">
<form ng-repeat="f in forms">
<input ng-model="f.title">
</form>
</div>
<input type="button" ng-click="submitForms('#form-container')">
function controller(){
var self = this;
self.submitForms = function(selector){
let formContainer = angular.element(selector);
let forms = formContainer.find('form');
// и проверяйте валидность форм
}
}
// Собственно, сам интерфейс оповещений
public interface INotification
{
void Notify(string text);
}
public class EmailNotification : INotification
{
public void Notify(string text)
{
// код по отправке почты
}
}
public class SmsNotification : INotification
{
public void Notify(string text)
{
// код по отправке смс
}
}
// ... Еще какие-нибудь классы оповещений
// В каком-нибудь классе, где может появиться ошибка
public class MaybeErrorClass
{
private INotification _notification;
public MaybeErrorClass(INotification notification)
{
// Класс не должен знать, по какому каналу оповещать об ошибках.
// Он работает с абстракцией
this._notification = notification;
}
// Очень простой пример метода, в котором ожидаем генерацию ошибки
public void DoSomething()
{
try {
// какой-то блок, в котором можем получить ошибку
}
catch (Exception e)
{
this._notification.Notify("А у нас тут ошибка!");
}
}
}
var maybeErrorEmail = new MaybeErrorClass(new EmailNotification());
var maybeErrorSms = new MaybeErrorClass(new SmsNotification());
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace JsonTest
{
public class Deserialized
{
public class Region
{
public class Domain
{
public string code { get; set; }
public string name { get; set; }
public int id { get; set; }
}
public string code { get; set; }
public string countryCode { get; set; }
public string name { get; set; }
public int id { get; set; }
public Domain domain { get; set; }
}
public Dictionary<int, Region> regions { get; set; }
}
class Program
{
static void Main(string[] args)
{
string json = @"{
""regions"": {
""937"": {
""code"": ""Russia"",
""countryCode"": ""ru"",
""name"": ""Россия"",
""id"": 937,
""domain"": {
""code"": "".ru"",
""name"": ""Росийская Федерация"",
""id"": 175
}
},
""979"": {
""code"": ""Bryansk,Bryansk Oblast,Russia"",
""countryCode"": ""ru"",
""name"": ""Брянск"",
""id"": 979,
""domain"": {
""code"": "".ru"",
""name"": ""Росийская Федерация"",
""id"": 175
}
}
}
}";
Deserialized data = JsonConvert.DeserializeObject<Deserialized>(json);
}
}
}
class AbstractExpressionList<T>: Dictionary<string, Func<T, T>> { }
public T Aggregate(IEnumerable<T> values, T accumulator, Func<T, T> projection)
{
var current = accumulator;
foreach(var value in values)
current = projection(current, value);
return current;
}
var data = new AbstractExpressionList<T>
{
["a"] = ...,
["b"] = ...
};
var result = data.Aggregate(...);