Skip to content

Instantly share code, notes, and snippets.

  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save circleupx/909ef2dc77f830fdf59791a335651667 to your computer and use it in GitHub Desktop.
Source code for my "How to build a fluent interface in C#" blog post.
using System.Collections.Generic;
using BuildAFluentInterface.Interfaces;
namespace BuildAFluentInterface
{
public class DeleteQueryWithGrammar : ICanAddCondition, ICanAddWhereValue, ICanAddWhereOrRun
{
private readonly string _tableName;
private readonly List<WhereCondition> _whereConditions = new List<WhereCondition>();
private string _currentWhereConditionColumn;
// Private constructor, to force object instantiation from the fluent method(s)
private DeleteQueryWithGrammar(string tableName)
{
_tableName = tableName;
}
#region Initiating Method(s)
public static ICanAddCondition DeleteRowsFrom(string tableName)
{
return new DeleteQueryWithGrammar(tableName);
}
#endregion
#region Chaining Method(s)
public ICanAddWhereValue Where(string columnName)
{
_currentWhereConditionColumn = columnName;
return this;
}
public ICanAddWhereOrRun IsEqualTo(object value)
{
_whereConditions.Add(new WhereCondition(_currentWhereConditionColumn, WhereCondition.ComparisonMethod.EqualTo, value));
return this;
}
public ICanAddWhereOrRun IsNotEqualTo(object value)
{
_whereConditions.Add(new WhereCondition(_currentWhereConditionColumn, WhereCondition.ComparisonMethod.NotEqualTo, value));
return this;
}
#endregion
#region Executing Method(s)
public void AllRows()
{
ExecuteThisQuery();
}
public void RunNow()
{
ExecuteThisQuery();
}
#endregion
private void ExecuteThisQuery()
{
// Code to build and execute the delete query
}
}
}
using System.Collections.Generic;
namespace BuildAFluentInterface
{
public class DeleteQueryWithoutGrammar
{
private readonly string _tableName;
private readonly List<WhereCondition> _whereConditions = new List<WhereCondition>();
private string _currentWhereConditionColumn;
// Private constructor, to force object instantiation from the fluent method(s)
private DeleteQueryWithoutGrammar(string tableName)
{
_tableName = tableName;
}
#region Initiating Method(s)
public static DeleteQueryWithoutGrammar DeleteRowsFrom(string tableName)
{
return new DeleteQueryWithoutGrammar(tableName);
}
#endregion
#region Chaining Method(s)
public DeleteQueryWithoutGrammar Where(string columnName)
{
_currentWhereConditionColumn = columnName;
return this;
}
public DeleteQueryWithoutGrammar IsEqualTo(object value)
{
_whereConditions.Add(new WhereCondition(_currentWhereConditionColumn, WhereCondition.ComparisonMethod.EqualTo, value));
return this;
}
public DeleteQueryWithoutGrammar IsNotEqualTo(object value)
{
_whereConditions.Add(new WhereCondition(_currentWhereConditionColumn, WhereCondition.ComparisonMethod.NotEqualTo, value));
return this;
}
#endregion
#region Executing Method(s)
public void AllRows()
{
ExecuteThisQuery();
}
public void RunNow()
{
ExecuteThisQuery();
}
#endregion
private void ExecuteThisQuery()
{
// Code to build and execute the delete query
}
}
}
namespace BuildAFluentInterface.Interfaces
{
public interface ICanAddCondition
{
ICanAddWhereValue Where(string columnName);
void AllRows();
}
}
namespace BuildAFluentInterface.Interfaces
{
public interface ICanAddWhereOrRun
{
ICanAddWhereValue Where(string columnName);
void RunNow();
}
}
namespace BuildAFluentInterface.Interfaces
{
public interface ICanAddWhereValue
{
ICanAddWhereOrRun IsEqualTo(object value);
ICanAddWhereOrRun IsNotEqualTo(object value);
}
}
namespace BuildAFluentInterface
{
public class WhereCondition
{
public enum ComparisonMethod
{
EqualTo,
NotEqualTo
}
public string ColumnName { get; private set; }
public ComparisonMethod Comparator { get; private set; }
public object Value { get; private set; }
public WhereCondition(string columnName, ComparisonMethod comparator, object value)
{
ColumnName = columnName;
Comparator = comparator;
Value = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment