In my previous questions, I feel I have repeatedly been making the mistake of posting portions of my code that I felt was relevant to the immediate problem I was facing, and as a result, I suspect that people weren't seeing the big picture of the problem with my code overall.
My problem is I need: to know
- clear separation of logic
- is my code reusability
- cleanlinessin my CRUD functions structured well and effectively? I plan to implement CRUD for Locations entity too. hopefully without redundant code.
- single responsibility principleyou see how one CRUD function in the program class calls the other CRUD function in the DataAccess Class? I"m trying to set it up so that i can control which entity gets edited or read in the control class, and access it in the dataAccess class. Is this bad logic?
So I'm posting all my code with formatting to make it much easier to navigate.
class Program
{
static private UserInterface _MyUI;
static void Main(string[] args)
{
DbResult resultSQLRunning;
ServiceManager mySM = new ServiceManager();
_MyUI = new UserInterface();
resultSQLRunning = mySM.SQLRunning();
_MyUI.DisplayMessage(resultSQLRunning.Message);
if (resultSQLRunning.bSuccess)
{
using (TestDatabaseEntities myDB = new TestDatabaseEntities())
{
DataAccess myDA = new DataAccess(myDB);
DbResult myResult = myDA.DatabaseExists();
_MyUI.DisplayMessage(myResult.Message);
}
do
{
DecideFromMenuChoice(_MyUI.GetUserAction());
} while (_MyUI._MyMenuStage != UserInterface.MenuStage.MENU_EXIT);
}
Console.ReadLine(); // press any key to exit, basically.
}
static private void DecideFromMenuChoice(int choice)
{
if (_MyUI._MyMenuStage == UserInterface.MenuStage.MENU_CRUD)
{
switch (choice)
{
case 1: DoCreate(); break;
case 2: DoRead(); break;
case 3: DoUpdate(); break;
case 4: DoDelete(); break;
default:
break;
}
}
/* else if (_MyUI._MyMenuStage == UserInterface.MenuStage.MENU_TABLE)
{
switch (choice)
{
// not sure what to do here, somehow select a table to be used in CRUD functions?
// do i need to call more CRUD functions for each new table I add?
}
}*/
}
// ============================
// CRUD FUNCTIONS
// ============================
static private void DoCreate()
{
int myID;
bool isValidID;
var dbEntities = new TestDatabaseEntities();
string newName;
DataAccess MyDA = new DataAccess(dbEntities);
DbResult CreationResult, SaveResult;
do
{
isValidID = _MyUI.GetValidInput<int>("Enter ID: ", int.TryParse, out myID);
}
while (!isValidID);
newName = _MyUI.GetInput<string>("Enter Name:", x => x.Trim());
CreationResult = MyDA.Create(new Man() {ManID = myID, Name = newName });
_MyUI.DisplayMessage(CreationResult.Message);
if (!CreationResult.bSuccess)
return;
SaveResult = MyDA.SaveChanges();
_MyUI.DisplayMessage(SaveResult.Message);
}
static private void DoRead()
{
var dbEntities = new TestDatabaseEntities();
DataAccess myDA = new DataAccess(dbEntities);
string [,] records;
DbResult readResult;
var query = from person in dbEntities.Men
where true
select person;
readResult = myDA.Read(query, out records);
if (readResult.bSuccess)
{
_MyUI.DisplayRecords(records);
}
if (!readResult.bSuccess)
_MyUI.DisplayMessage(readResult.Message);
}
static private void DoUpdate()
{
/* The code in this DoUpdate function is "simplified" and "cleaner"
* than the commented block below, I'm told. is it?*/
int myID = _MyUI.GetInput<int>("Enter ID to update: ", int.Parse);
string newName = _MyUI.GetInput<string>("Enter new name: ", x => x.Trim());
try
{
using (var dbEntities = new TestDatabaseEntities())
{
//TODO: If ManID is unique then we can use FirstOrDefault here.
var allMatchingMen =
from person in dbEntities.Men
where person.ManID == myID
select person;
foreach (var man in allMatchingMen)
man.Name = newName;
dbEntities.SaveChanges();
_MyUI.DisplayMessage("Record(s) updated");
}
}
/*catch (OptimisticConcurrencyException ex)
{
//TODO: add auto-retry logic
_MyUI.DisplayMessage("Someone updated the record, let's retry");
}*/
catch (Exception ex)
{
_MyUI.DisplayMessage("Something went wrong, could not update");
}
}
/*{
int myID;
var dbEntities = new TestDatabaseEntities();
string newName = "";
DataAccess MyDA = new DataAccess(dbEntities);
DbResult updateResult, saveResult;
myID = _MyUI.GetInput<int>("Enter ID to update: ", int.Parse);
newName = _MyUI.GetInput<string>("Enter new name: ", x => x.Trim());
var query =
from person in dbEntities.Men
where person.ManID == myID
select person;
updateResult = myDA.Update(query, new Man() { ManID = myID, Name = sNewName });
_MyUI.DisplayMessage(updateResult.Message);
if (!UpdateResult.bSuccess)
return;
SaveResult = myDA.SaveChanges();
_MyUI.DisplayMessage(saveResult.Message);
}*/
static private void DoDelete()
{
int myID;
bool isValidInput;
var dbEntities = new TestDatabaseEntities();
DataAccess myDA = new DataAccess(dbEntities);
DbResult deleteResult, saveResult;
do
{
isValidInput = _MyUI.GetValidInput<int>("Enter ID to delete: ", int.TryParse, out myID);
} while (!isValidInput);
var Query =
from person in dbEntities.Men
where person.ManID == myID
select person;
deleteResult = myDA.Delete(Query);
_MyUI.DisplayMessage(deleteResult.Message);
if (!deleteResult.bSuccess)
return;
saveResult = myDA.SaveChanges();
_MyUI.DisplayMessage(saveResult.Message);
}
}