Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple fluent interface in C#
using System;
using System.Collections.Generic;
namespace SimpleFluentInterface
{
class Program
{
static void Main(string[] args)
{
string s = ListEnrollmentsSelectBuilder.Create()
.Data()
.HistoryOn(new DateTime(2016, 6, 1))
.Course(x => x.Data().HistoryInRange(new DateTime(2016, 5, 1), new DateTime(2016, 6, 1)))
.Domain()
.User(x => x.Session().Data().AllHistory())
.Metrics(x => x.AllHistory());
Console.WriteLine(s);
string t = ListEnrollmentsSelectBuilder.Create()
.Data()
.Domain()
.AllHistory()
.HistoryOn(new DateTime(2014, 3, 2))
.HistoryInRange(new DateTime(2015, 1, 2), new DateTime(2015, 1, 6))
.Course(x => x.Data()
.AllHistory()
.HistoryOn(new DateTime(2014, 3, 2))
.HistoryInRange(new DateTime(2015, 1, 2), new DateTime(2015, 1, 6)))
.User(x => x.Data()
.Session()
.AllHistory()
.HistoryOn(new DateTime(2014, 3, 2))
.HistoryInRange(new DateTime(2015, 1, 2), new DateTime(2015, 1, 6)))
.Metrics(x => x.AllHistory()
.HistoryOn(new DateTime(2014, 3, 2))
.HistoryInRange(new DateTime(2015, 1, 2), new DateTime(2015, 1, 6)));
Console.WriteLine(t);
}
}
public class ListEnrollmentsSelectBuilder : SelectBuilder<ListEnrollmentsSelectBuilder>
{
public ListEnrollmentsSelectBuilder Data()
{
SelectItems.Add("data");
return this;
}
public ListEnrollmentsSelectBuilder AllHistory()
{
SelectItems.Add("history(all)");
return this;
}
public ListEnrollmentsSelectBuilder HistoryOn(DateTime date)
{
SelectItems.Add($"history(on,{date.SelectFormat()})");
return this;
}
public ListEnrollmentsSelectBuilder HistoryInRange(DateTime from, DateTime to)
{
SelectItems.Add($"history(range,{from.SelectFormat()}{to.SelectFormat()})");
return this;
}
public ListEnrollmentsSelectBuilder Domain()
{
SelectItems.Add("domain");
return this;
}
public ListEnrollmentsSelectBuilder Course(Action<CourseSelectBuilder> action)
{
SelectItems.Add("course");
AddSubBuilder(action);
return this;
}
public ListEnrollmentsSelectBuilder User(Action<UserSelectBuilder> action)
{
SelectItems.Add("user");
AddSubBuilder(action);
return this;
}
public ListEnrollmentsSelectBuilder Metrics(Action<MetricsSelectBuilder> action)
{
SelectItems.Add("metrics");
AddSubBuilder(action);
return this;
}
}
public abstract class SelectBuilder<T> where T : new()
{
public List<string> SelectItems { get; } = new List<string>();
public static T Create()
{
return new T();
}
public string Build()
{
return string.Join(",", SelectItems);
}
protected void AddSubBuilder<TSubBuilder>(Action<TSubBuilder> action)
where TSubBuilder : SelectBuilder<TSubBuilder>, new()
{
var selectBuilder = new TSubBuilder();
action(selectBuilder);
SelectItems.AddRange(selectBuilder.SelectItems);
}
public static implicit operator string(SelectBuilder<T> d)
{
return d.Build();
}
}
public class CourseSelectBuilder : SelectBuilder<CourseSelectBuilder>
{
public CourseSelectBuilder Data()
{
SelectItems.Add("course.data");
return this;
}
public CourseSelectBuilder AllHistory()
{
SelectItems.Add("course.history(all)");
return this;
}
public CourseSelectBuilder HistoryOn(DateTime date)
{
SelectItems.Add($"course.history(on,{date.SelectFormat()})");
return this;
}
public CourseSelectBuilder HistoryInRange(DateTime from, DateTime to)
{
SelectItems.Add($"course.history(range,{from.SelectFormat()},{to.SelectFormat()})");
return this;
}
}
public class UserSelectBuilder : SelectBuilder<UserSelectBuilder>
{
public UserSelectBuilder Data()
{
SelectItems.Add("user.data");
return this;
}
public UserSelectBuilder AllHistory()
{
SelectItems.Add("user.history(all)");
return this;
}
public UserSelectBuilder HistoryOn(DateTime date)
{
SelectItems.Add($"user.history(on,{date.SelectFormat()})");
return this;
}
public UserSelectBuilder HistoryInRange(DateTime from, DateTime to)
{
SelectItems.Add($"user.history(range,{from.SelectFormat()},{to.SelectFormat()})");
return this;
}
public UserSelectBuilder Session()
{
SelectItems.Add("user.session");
return this;
}
}
public class MetricsSelectBuilder : SelectBuilder<MetricsSelectBuilder>
{
public MetricsSelectBuilder AllHistory()
{
SelectItems.Add("metrics.history(all)");
return this;
}
public MetricsSelectBuilder HistoryOn(DateTime date)
{
SelectItems.Add($"metrics.history(on,{date.SelectFormat()})");
return this;
}
public MetricsSelectBuilder HistoryInRange(DateTime from, DateTime to)
{
SelectItems.Add($"metrics.history(range,{from.SelectFormat()},{to.SelectFormat()})");
return this;
}
}
public static class DateTimeExtensions
{
public static string SelectFormat(this DateTime dateTime)
{
return dateTime.ToString("s") + "Z";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.