I am asking this for coding style. I have a program class that calls a function(of a data access class) that reads from a table (using entity framework by the way), and I'm preparing to write the DataAccess.ReadMethod()
.
Is it better to pass the function a 2d string array string[rows][fields]
, reading each field and each row, and put it in the DataAccess.ReadMethod
's out parameter string[rows][fields]
to return to the main program and have the main program display it? or just repeatedly call the DataAccess.ReadMethod(string[fields])
and in the main program print each record out individually to the console?
I imagine that my ReadMethod()
will look something like this:
public class DataAccess
{
public Exception ReadMan(TestDatabaseEntities dbEntities, IQueryable myQuery, out string sManDetails)
{
Exception ex = null;
sManDetails = "";
try
{
foreach (Man m in myQuery)
{
sManDetails += m.ManID.ToString() + " " + m.Name + "\n";
}
}
catch (Exception myException)
{
ex = myException;
}
return ex;
}
}
the calling code in program class looks like this:
class program
{
static private void DoRead()
{
var dbEntities = new TestDatabaseEntities();
UserInterface MyUI = new UserInterface();
DataAccess MyDA = new DataAccess();
Exception ex = null;
string sMyMen = "";
var query = from person in dbEntities.Men
where true
select person;
MyUI.DisplayDivider();
ex = MyDA.ReadMan(dbEntities, query, out sMyMen);
if (ex != null)
sMyMen = ex.ToString();
MyUI.DisplayMessage(sMyMen);
MyUI.DisplayDivider();
}
}
DataAccess.ReadMethod()
needs to return a variable which stores the contents that it read from the entity, i figured on storing the data in a string array, so that it can be passed back to the program class, and the program class can call myUserInterface.DisplayMethod(string somestring)
method. I want to keep logic seperate from database access and UI. \$\endgroup\$