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();
}
}
1 Answer 1
I have discovered, with thanks to you each, that it is better to call the display method within the DataAccess.ReadMethod() than to pass strings, Here's what I think i should do:
public class DataAccess
{
public bool CreateMan(TestDatabaseEntities dbEntities, Man M)
{
return TryDataBase(dbEntities, "Man created successfully",
() =>
{
dbEntities.Men.Add(new Man { ManID = M.ManID, Name = M.Name });
});
}
public bool UpdateMan(TestDatabaseEntities dbEntities, IQueryable<Man> query, Man man)
{
return TryDataBase(dbEntities, "Man updated Successfully",
() =>
{
foreach (Man M in query)
{
M.Name = man.Name;
}
});
}
public bool DeleteMan(TestDatabaseEntities dbEntities, IQueryable myQuery)
{
return TryDataBase(dbEntities, "Man deleted Successfully",
() =>
{
foreach (Man M in myQuery)
{
dbEntities.Men.Remove(M);
}
});
}
public bool ReadMan(TestDatabaseEntities dbEntities, IQueryable myQuery)
{
UserInterface MyUI = new UserInterface();
bool bSuccessful;
bSuccessful = TryDataBase(dbEntities, "Records read successfully",
() =>
{
MyUI.DisplayDivider();
foreach (Man m in myQuery)
{
MyUI.DisplayMessage(new string[] { m.ManID.ToString(), m.Name });
}
MyUI.DisplayDivider();
});
return bSuccessful;
}
public bool TryDataBase(TestDatabaseEntities MyDBEntities, string SuccessMessage, Action MyDBAction)
{
UserInterface MyUI = new UserInterface();
try
{
MyDBAction();
MyUI.DisplayMessage(SuccessMessage);
return true;
}
catch (Exception e)
{
MyUI.DisplayMessage(e.ToString());
return false;
}
}
}
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\$