We have a Basewebpage class having many common functions like filling up dropdowns, formating , return formatted datatable and so on..
What would be a good approach.
Public Class BaseWebPage
{
...
public static DataTable Static_GetPersonDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Age");
return dt;
}
...
public DataTable NonStatic_GetPersonDataTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Age");
return dt;
}
}
This is how both can be called
DataTable dt1 = BaseWebPage.Static_GetPersonDataTable(); // calling static method
...
BaseWebPage pageBase = new BaseWebPage();
DataTable dt2 = pageBase.NonStatic_GetPersonDataTable(); // calling non-static method
Few questions in above scenario
- Will both method return new instance of a datatable or static will always return the same?
- During heavy load (Many requests) case will there be any thread related issue for any of these?
- If there are no side effects which is preferable over other? Why?
if you can write little bit descriptive it will be really helpful.
•Will both method return new instance of a datatable or static will always return the same?
Yes they will return a new instance.
•During heavy load (Many requests) case will there be any thread related issue for any of these?
No unless you use shared state in your methods. Currently they don’t but they may. Even the instance methods may use shared state. This is not related to staticness (staticity?)
•If there are no side effects which is preferable over other? Why?
It doesn’t really matter but I would go for instance methods. At some point you may want to add tests and you can extract the methods in an interface and mock the object.
Bonus point:
DataTables suck! Avoid them and use classes/objects.