[英]How would I use this Factory class and whats the point?
This is my first ever post to SO and am very new to C# with most of my IT experience in databases.这是我第一次在 SO 上发帖,我对 C# 非常陌生,因为我在数据库方面拥有大部分 IT 经验。 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 ie var dt = new Datatable()?为什么不创建一个数据表的新实例,即 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谢谢
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.
相反,它从数据表中获取数据并将其转换为“Dto”(数据传输对象),您可以在其中了解所有存在的属性(列)及其类型,因为它们是显式声明的。 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.
直接创建这个 Dto 类比创建一个 Datatable 然后映射它更有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.