vendredi 31 janvier 2020

return string when using the builder Pattern and Method chaining in C#

i want to use Builder Pattern and use that by Method chaining .

this is my class :

 public class SQLGenerator : ISQLGenerator
{
    private readonly SQLGeneratorModel generatorModel;
    public SQLGenerator()
    {
        generatorModel = new SQLGeneratorModel();
    }

    public ISQLGenerator From(string TableName)
    {
        generatorModel.TabelName = string.Format("FROM {0}", TableName);
        return this;
    }

    public ISQLGenerator Select(List<string> SelectItems)
    {
        StringBuilder select = new StringBuilder();
        var lastItem = SelectItems.Last();
        foreach (var item in SelectItems)
        {
            if (!item.Equals(lastItem))
            {
                select.Append(item).Append(" , ");
            }
            else
            {
                select.Append(item);
            }
        }
        generatorModel.Select =string.Format("SELECT {0}", select.ToString());
        return this;
    }

and class that by ths way :

        SQLGenerator generator = new SQLGenerator();
        List<string> select = new List<string>();
        select.Add("id");
        select.Add("Name");
        var str = generator.Select(select).From("AppUsers");
        Console.WriteLine(str);

now i except return to me string like this SELECT ID , NAME FROM APPUSER but it show me this result in the console :

SqlPagingGenerator.Pattern.SQLGenerator

how can i show the string result ????

Aucun commentaire:

Enregistrer un commentaire