Skip to content

Instantly share code, notes, and snippets.

@trayburn
Created August 27, 2014 20:49
Show Gist options
  • Save trayburn/ba5e09baad260b677c76 to your computer and use it in GitHub Desktop.
Save trayburn/ba5e09baad260b677c76 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
var c1 = new AccountsController();
var d1 = c1.AutoComplete();
foreach (var item in d1)
{
Console.WriteLine(item.Text);
}
Console.WriteLine("------------------------------------------");
var c2 = new CompaniesController();
var d2 = c2.AutoComplete();
foreach (var item in d2)
{
Console.WriteLine(item.Text);
}
Console.WriteLine("Done");
Console.ReadLine();
}
}
public interface IProjectStrategy<TEntity, TModel>
{
IEnumerable<TModel> Project(IEnumerable<TEntity> data);
}
public interface IAutoCompleteStrategy<TEntity, TModel> : IProjectStrategy<TEntity, TModel>
{
IEnumerable<TEntity> GetData();
}
public abstract class BaseController : IProjectStrategy<string, AutoCompleteModel>
{
public IEnumerable<TModel> AutoComplete<TEntity, TModel>(IAutoCompleteStrategy<TEntity, TModel> strategy)
{
var data = strategy.GetData();
return strategy.Project(data);
}
public IEnumerable<AutoCompleteModel> Project(IEnumerable<string> data)
{
return data.Select(s => new AutoCompleteModel { Id = Guid.NewGuid(), Text = s });
}
}
public class DemoController : BaseController, IAutoCompleteStrategy<string, AutoCompleteModel>
{
IEnumerable<string> IAutoCompleteStrategy<string, AutoCompleteModel>.GetData()
{
throw new NotImplementedException();
}
}
public class CompaniesController : BaseController, IAutoCompleteStrategy<string,AutoCompleteModel>
{
public IEnumerable<AutoCompleteModel> AutoComplete() { return base.AutoComplete(this); }
public IEnumerable<string> GetData()
{
return new[] {
"Chevron",
"Exxon"
};
}
}
public class AccountsController : BaseController, IAutoCompleteStrategy<string, AutoCompleteModel>
{
public IEnumerable<AutoCompleteModel> AutoComplete() { return base.AutoComplete(this); }
public IEnumerable<string> GetData()
{
return new[] {
"Hello",
"World"
};
}
}
public class AutoCompleteModel
{
public Guid Id { get; set; }
public string Text { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment