Home » c# » c# – How would I use this Factory class and whats the point?-Exceptionshub

c# – How would I use this Factory class and whats the point?-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

This is my first ever post to SO and am very new to C# with most of my IT experience in databases. I am starting to look at some of our code and would like to understand how I would use this class and it methods for resusability purposes.

public class FileCreator
{
    public string Territory { get; set; }
    public string CV { get; set; }
    public string AdDate { get; set; }
    public string Category { get; set; }
    public string Advertiser { get; set; }
    public string Brand { get; set; }
    public decimal SumOfSpend { get; set; }
    public decimal SumOfVolume { get; set; }
    public string Spots { get; set; }
    public string PageNumber { get; set; }

    internal static List<FileCreator> Create(DataSet data)
    {
        var result = new List<FileCreator>();

        if (data.Tables.Count > 0)
        {
            result = Create(data.Tables[0]);
        }

        return result;
    }

    public static List<FileCreator> Create(DataTable dataTable)
    {
        var result = new List<FileCreator>();

        foreach (DataRow row in dataTable.Rows)
        {
            result.Add(Create(row));
        }

        return result;
    }

    private static FileCreator Create(DataRow row)
    {
        var fileCreator = new FileCreator();

        fileCreator.Territory = (row["Territory"].ToString());
        fileCreator.CV = row["CV"].ToString();
        fileCreator.AdDate = row["Ad_date"].ToString();
        fileCreator.Category = row["Category"].ToString();
        fileCreator.Advertiser = row["Advertiser"].ToString();
        fileCreator.Brand = row["Brand"].ToString();
        fileCreator.SumOfSpend = Convert.ToDecimal(row["SumOfSpend"].ToString());
        fileCreator.SumOfVolume = Convert.ToDecimal(row["SumOfVolume"].ToString());
        fileCreator.Spots = row["Spots"].ToString();
        fileCreator.PageNumber = row["Page Number"].ToString();

        return fileCreator;
    }
}

Why not just create a new instance of a datatable i.e. var dt = new Datatable()?

I must be missing the point of this approach. How will I benefit from this approach when I normally just create multiple datatables?

Go easy. It’s my first post 🙂

Thank you

How to&Answers:

The point of this is get a strongly typed class instead of relying on hard-coded strings to get the data out and then casting everything to the type you are expecting. Instead, its taking data from the Datatable and transforming it to a “Dto” (Data Transfer Object) where you know all the properties (columns) that exist and their types because they are explicitly declared. There is no guessing! 🙂

While the given approach here works, I hope it is more for legacy sake. It is more efficient to simply create this Dto class directly instead of creating a Datatable and then mapping it.