I am using DI for getting the Data table. But the difficulty is that the DataTable is re initialized every time and the rows already added are not available.
public DataTable GetDatatable()
{
var dt = new DataTable("test1");
dt.Columns.Add("signature", typeof(string));
dt.Columns.Add("timestamp", typeof(string));
return dt;
}
Is there any way we can initialize data table in the same name only if data table not exists?
As @Mihail suggested, here is a Singleton (give it a better name)
public sealed class Singleton
{
private static readonly Lazy<Singleton> Lazy = new(() => new Singleton());
public static Singleton Instance => Lazy.Value;
public DataTable DateTable { get; set; }
private Singleton()
{
DateTable = new DataTable();
DateTable.Columns.Add("signature", typeof(string));
DateTable.Columns.Add("timestamp", typeof(string));
}
}
Usage
var dataTable = Singleton.Instance.DateTable;