I have a console application that I'm trying to make with minimal redundant code.
I have 4 functions, Create, Read, Update, and Delete.
- is it possible or practical to combine the four functions into one function that takes different parameters to create, read, update, or delete?
- is it possible to also adjust that one combined function to use whichever of my two entity tables (Man or Location) I tell it to use?
I also have a helper static class that provides some functions to make some tasks easier.
4 CRUD FUNCTIONS
class Program
{
#region CRUD functions
static private void DoCreate()
{
int myID;
var dbEntities = new TestDatabaseEntities();
string sNewName = "";
myID = stcHelper.InsistOnValidInput<int>("Enter ID: ", "ID must be Valid: ", int.TryParse);
sNewName = stcHelper.GetInput<string>("Enter Name", x => x.Trim());
stcHelper.TryDataBase(dbEntities, () =>
{
dbEntities.Men.Add(new Man{ManID = myID, Name = sNewName});
Console.WriteLine("Insert Sucessful");
});
stcHelper.SaveDBChanges(dbEntities);
}
static private void DoRead()
{
var dbEntities = new TestDatabaseEntities();
string sDisplayDivider = "|||||||||||||||||||||||||||||||||||||||";
var query = from person in dbEntities.Men
where true
select person;
Console.WriteLine(sDisplayDivider);
stcHelper.TryDataBase(dbEntities, () =>
{
foreach (Man m in query)
{
Console.WriteLine(m.ManID + " " + m.Name);
}
});
Console.WriteLine(sDisplayDivider);
}
static private void DoUpdate()
{
int myID;
var dbEntities = new TestDatabaseEntities();
string sNewName = "";
// get The Target's ID for the user to update
myID = stcHelper.GetInput<int>("Enter ID to update: ", int.Parse);
// get the new name from user
sNewName = stcHelper.GetInput<string>("Enter new name: ", x => x.Trim());
// Query the database for the row to be updated.
var query =
from person in dbEntities.Men
where person.ManID == myID
select person;
// Execute the query, and change the column values
stcHelper.TryDataBase(dbEntities, () =>
{
foreach (Man M in query)
{
M.Name = sNewName;
Console.WriteLine("Update Sucessful!");
}
});
stcHelper.SaveDBChanges(dbEntities);
}
static private void DoDelete()
{
int myID;
//T myID;
var dbEntities = new TestDatabaseEntities();
myID = stcHelper.InsistOnValidInput<int>("Enter ID to delete: ", "ID Invalid, please re-enter", int.TryParse);
// Query the database for the rows to be deleted.
var Query =
from details in dbEntities.Men
where details.ManID == myID
select details;
stcHelper.TryDataBase(dbEntities, () =>
{
foreach (var detail in Query)
{
dbEntities.Men.Remove(detail);
Console.WriteLine("Removal successful.");
}
});
stcHelper.SaveDBChanges(dbEntities);
}
#endregion
}
HELPER CLASS
public static class stcHelper
{
/// <summary>
/// Delegate that matches the signature of TryParse, method defined for all primitives.
/// </summary>
/// <typeparam name="T">Output type of This Delegate</typeparam>
/// <param name="input">input for this Delegate to translate to type T</param>
/// <param name="output">The translated variable to return via out parameter</param>
/// <returns>Whether the Parse was successful or not, and output as output</returns>
public delegate bool TryParse<T>(string input, out T output);
/// <summary>
/// Prompts user for input with given message, and converts input to type T
/// </summary>
/// <typeparam name="T">Value type to convert to, and return</typeparam>
/// <param name="message">Message to be printed to console</param>
/// <param name="transform">The type conversion function to use on user's input</param>
/// <returns>Type T</returns>
static public T GetInput<T>(string message, Converter<string, T> transform)
{
Console.Write(message);
return transform(Console.ReadLine());
}
/// <summary>
/// Repeatedly asks the user for valid input
/// </summary>
/// <typeparam name="T">The type of result to return</typeparam>
/// <param name="message">The initial message to prompt the user with</param>
/// <param name="errorMessage">The message to prompt user with if input is invalid</param>
/// <param name="validator">The TryParse function to use to test the input.</param>
/// <returns>Type T</returns>
public static T InsistOnValidInput<T>(string message, string errorMessage, TryParse<T> validator)
{
Console.Write(message);
T result;
while (!validator(Console.ReadLine(), out result))
{
Console.Write(errorMessage);
}
return result;
}
static public void SaveDBChanges(TestDatabaseEntities MyDBEntities)
{
// Submit the changes to the database.
TryDataBase(MyDBEntities,
() =>
{
MyDBEntities.SaveChanges();
Console.WriteLine("Save Changes Successful.");
});
}
static public void TryDataBase(TestDatabaseEntities MyDBEntities, Action MyAction)
{
try
{
MyAction();
}
catch (Exception e)
{
Console.WriteLine("Database interaction failed: " + e.ToString());
return;
}
}
}
Man and Location. Auto-generated
by the entity framework
public partial class Man
{
public Man()
{
this.Locations = new HashSet<Location>();
}
public int ManID { get; set; }
public string Name { get; set; }
public virtual ICollection<Location> Locations { get; set; }
}
public partial class Location
{
public Location()
{
this.Men = new HashSet<Man>();
}
public int PlaceID { get; set; }
public string Place { get; set; }
public virtual ICollection<Man> Men { get; set; }
}
Man and Location, NON-Auto Generated
public partial class Location : IAmAnEntity
{
public void PromptNewName()
{
Place = stcHelper.GetInput<string>("Give Location Name: ", (x) => x.Trim());
}
public void PromptNewID()
{
PlaceID = stcHelper.InsistOnValidInput<int>("Give New ID: ", "ID must be integer: ", int.TryParse);
}
public void CreateMe()
{
PromptNewID();
PromptNewName();
}
public void ReadMe()
{
throw new NotImplementedException();
}
public void UpdateMe()
{
throw new NotImplementedException();
}
public void DeleteMe()
{
throw new NotImplementedException();
}
}
public partial class Man : IAmAnEntity
{
public void PromptNewName()
{
Name = stcHelper.GetInput<string>("Enter Name for new person", i => i.Trim());
}
public void PromptNewID()
{
ManID = stcHelper.InsistOnValidInput<int>("Enter ID for new person:", "ID must be integer: ", int.TryParse);
}
public void CreateMe()
{
PromptNewID();
PromptNewName();
}
public void ReadMe()
{
throw new NotImplementedException();
}
public void UpdateMe()
{
throw new NotImplementedException();
}
public void DeleteMe()
{
throw new NotImplementedException();
}
}
interface IAmAnEntity
public interface IAmAnEntity
{
void PromptNewName();
void PromptNewID();
void CreateMe();
void ReadMe();
void UpdateMe();
void DeleteMe();
}