In my C# program, I have a class defined as follows:
public class DbResult
{
public bool bSuccess { get; private set; }
public String Message { get; private set; }
private DbResult(bool success, string message)
{
this.bSuccess = success;
this.Message = message;
}
public static DbResult Failed(string message)
{
return new DbResult(false, message);
}
public static DbResult Success(string message)
{
return new DbResult(true, message);
}
}
This is used in my DataAccess class, which has several methods whose return type is DbResult. They use LINQ and the Entity Framework. UPDATE Here is the entire DataAccess as of right now:
public class DataAccess
{
private readonly TestDatabaseEntities _MyDBEntities;
public DataAccess(TestDatabaseEntities entities)
{
_MyDBEntities = entities;
}
public DbResult DatabaseExists()
{
DbResult MyResult;
if (_MyDBEntities.Database.Exists())
MyResult = DbResult.Success("Database Found");
else
MyResult = DbResult.Failed("Database Not Found");
return MyResult;
}
// ============================
// CRUD FUNCTIONS for MAN TABLE
// ============================
public DbResult Create(Man M)
{
DbResult DbRes;
try
{
_MyDBEntities.Men.Add(new Man { ManID = M.ManID, Name = M.Name });
DbRes = DbResult.Success("Record created");
}
catch (Exception e)
{
DbRes = DbResult.Failed(e.ToString());
}
return DbRes;
}
public DbResult Update(IQueryable<Man> myQuery, Man man)
{
DbResult DbRes;
try
{
foreach (Man M in myQuery)
{
M.Name = man.Name;
}
DbRes = DbResult.Success("Record updated");
}
catch (Exception e)
{
DbRes = DbResult.Failed(e.ToString());
}
return DbRes;
}
public DbResult Delete(IQueryable myQuery)
{
DbResult DbRes;
try
{
foreach (Man M in myQuery)
{
_MyDBEntities.Men.Remove(M);
}
DbRes = DbResult.Success("Record deleted");
}
catch (Exception e)
{
DbRes = DbResult.Failed(e.ToString());
}
return DbRes;
}
public DbResult Read(IQueryable myQuery, out string[,] Records)
{
DbResult DbRes;
Records = null;
try
{
List<Man> men = myQuery.OfType<Man>().ToList();
Records = new string[men.Count, 2];
for (int i = 0; i < men.Count; i++)
{
Records[i, 0] = men[i].ManID.ToString();
Records[i, 1] = men[i].Name;
}
DbRes = DbResult.Success("Read Success");
}
catch (Exception e)
{
DbRes = DbResult.Failed(e.ToString());
}
return DbRes;
}
// ============================
// SAVECHANGES FUNCTION
// ============================
public DbResult SaveChanges()
{
DbResult DbRes;
try
{
_MyDBEntities.SaveChanges();
DbRes = DbResult.Success("Saved successfully");
}
catch (Exception e)
{
DbRes = DbResult.Failed(e.ToString());
}
return DbRes;
}
}
I also have a class called ServiceManager, which checks to see if the SQL Server (SQLEXPRESS) service is running on the local machine.
public class ServiceManager
{
public DbResult SQLRunning()
{
DbResult MyResult;
ServiceController sc = new ServiceController("SQL Server (SQLEXPRESS)");
if (sc.Status == ServiceControllerStatus.Running)
MyResult = DbResult.Success("SQL Server is running");
else
MyResult = DbResult.Failed("SQL Server is NOT running.");
return MyResult;
}
}
As you may have noticed in the code directly above, I use the DbResult class as a return type to carry the status message and bool back to the main program. I think this does not make sense since DbResult is a class created for returning results from trying to perform operations on the Database, not the SQL Server checkup. I could create a new class called ServiceResult, which does the exact same thing as DbResult, but that would be duplicating code. see example below:
public class ServiceResult
{
public bool bRunning { get; private set; }
public String Message { get; private set; }
private ServiceResult(bool bRunning, string message)
{
this.bRunning = success;
this.Message = message;
}
public static ServiceResult Running(string message)
{
return new ServiceResult(false, message);
}
public static ServiceResult Stopped(string message)
{
return new ServiceResult(true, message);
}
}
You see how the above class is very similar to the DbResult class and works relatively the same way? This is how I might make a class for Services, but is there any way I can make a class that does both DbResults and ServiceResults without becoming more ambiguous like the following class example below? or, is it ok to be ambiguous like the result I put below?
public class Result
{
public bool bYesNo { get; private set; }
public String Message { get; private set; }
private Result(bool YesNo, string message)
{
this.bYesNo = YesNo;
this.Message = message;
}
public static Result No(string message)
{
return new Result(false, message);
}
public static Result Yes(string message)
{
return new Result(true, message);
}
}
UPDATED
The methods that use my DataAccess
class looks like this
static private void DoCreate()
{
int myID;
bool bIsValidID;
var dbEntities = new TestDatabaseEntities();
string sNewName;
DataAccess MyDA = new DataAccess(dbEntities);
DbResult CreationResult, SaveResult;
do
{
bIsValidID = _MyUI.GetValidInput<int>("Enter ID: ", int.TryParse, out myID);
}
while (!bIsValidID);
sNewName = _MyUI.GetInput<string>("Enter Name:", x => x.Trim());
CreationResult = MyDA.Create(new Man() {ManID = myID, Name = sNewName });
_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()
{
int myID;
var dbEntities = new TestDatabaseEntities();
string sNewName = "";
DataAccess MyDA = new DataAccess(dbEntities);
DbResult UpdateResult, SaveResult;
myID = _MyUI.GetInput<int>("Enter ID to update: ", int.Parse);
sNewName = _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 bValidInput;
var dbEntities = new TestDatabaseEntities();
DataAccess MyDA = new DataAccess(dbEntities);
DbResult DeleteResult, SaveResult;
do
{
bValidInput = _MyUI.GetValidInput<int>("Enter ID to delete: ", int.TryParse, out myID);
} while (!bValidInput);
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);
}