Source code for my "How to build a fluent interface in C#" blog post.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace BuildAFluentInterface.Interfaces | |
{ | |
public interface ICanAddCondition | |
{ | |
ICanAddWhereValue Where(string columnName); | |
void AllRows(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace BuildAFluentInterface.Interfaces | |
{ | |
public interface ICanAddWhereOrRun | |
{ | |
ICanAddWhereValue Where(string columnName); | |
void RunNow(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace BuildAFluentInterface.Interfaces | |
{ | |
public interface ICanAddWhereValue | |
{ | |
ICanAddWhereOrRun IsEqualTo(object value); | |
ICanAddWhereOrRun IsNotEqualTo(object value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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