I have a generic Get
function that returns a list of objects based on the name of the desired type of object. This has let to an enormous case/switch statement that delegates out to the specific Get
functions for the given type. I'd like use reflection or something similar to avoid this huge case/switch. I can't directly cast from the child types to Record
.
Here's the case/switch
public static IEnumerable<Record> GetChildren(string tenantName, string parentCollection, string parentId,
string childCollection)
{
M5Record parent;
try
{
parent = GetById(tenantName, parentCollection, parentId);
}
catch (InvalidOperationException)
{
throw new WebFaultException<ErrorResponse>(
new ErrorResponse { Error = "Not found" }, HttpStatusCode.NotFound);
}
childCollection = childCollection.ToLower();
if (parent == null)
return new List<Record>();
switch (childCollection)
{
case "assets":
case "asset":
return parent.GetAssets();
case "applications":
case "application":
return parent.GetApplications();
case "appointment":
return parent.GetAppointments();
case "bonds":
case "bond":
return parent.GetBonds();
case "buildings":
case "building":
return parent.GetBuildings();
case "certificates":
case "certificate":
return parent.GetCertificates();
case "checklistitems":
case "checklistitem":
return parent.GetCheckListItems();
case "companies":
case "company":
return parent.GetCompanies();
case "complaints":
case "complaint":
return parent.GetComplaints();
case "condition":
return parent.GetConditions();
case "contacts":
case "contact":
return parent.GetContacts();
case "courtappearance":
return parent.GetCourtAppearances();
case "document":
return parent.GetDocuments();
case "electronicsignatures":
case "electronicsignature":
return parent.GetElectronicSignatures();
case "escrowaccount":
return parent.GetEscrowAccounts();
case "events":
case "event":
return parent.GetEvents();
case "fees":
case "fee":
return parent.GetFees();
case "history":
return parent.GetHistory();
case "inspections":
case "inspection":
return parent.GetInspections();
case "issue":
return parent.GetIssues();
case "licenses":
case "license":
return parent.GetLicenses();
case "maintenances":
case "maintenance":
return parent.GetMaintenances();
case "meetings":
case "meeting":
return parent.GetMeetings();
case "occupants":
case "occupant":
return parent.GetOccupants();
case "owners":
case "owner":
return parent.GetOwners();
case "parcels":
case "parcel":
return parent.GetParcels();
case "project":
return parent.GetProjects();
case "permits":
case "permit":
return parent.GetPermits();
case "renewals":
case "renewal":
return parent.GetRenewals();
case "tasks":
case "task":
return parent.GetTasks();
case "time":
return parent.GetTimes();
// Sorry, this got chaotic fast.
case "stockpiletransaction":
string[] approvedParents = { "Stockpile", "Vehicle", "Maintenance", "WorkOrder" };
if (approvedParents.Contains(parent.GetType().Name))
{
return parent.GetStockpileTransactions();
}
return null;
case "subcode":
case "subcodes":
return parent.GetSubcodes();
case "summons":
return parent.GetSummons();
case "subscription":
case "subscriptions":
return parent.GetSubscriptions();
case "stopworkorder":
return parent.GetStopWorkOrders();
case "variances":
case "variance":
return parent.GetVariances();
case "violations":
case "violation":
return parent.GetViolations();
case "workflowstep":
return parent.GetWorkflowSteps();
case "workorders":
case "workorder":
return parent.GetWorkOrders();
default:
return new List<Record>();
}
}
Here's an example of one of the delegate functions. The delegate functions are different enough where it's not feasible to merge them into one function. It gets all the applications that are mapped to the parent record.
public IEnumerable<Record> GetApplications()
{
using (var conn = new SqlConnection(_tenant.ConnectionString))
{
const string sql = "SELECT * FROM ApplicationView WHERE Application_ID IN " +
"(SELECT Entity2Code_ID FROM mpEntity WHERE COALESCE(Deleted, 0) = 0 " +
"AND TableName1 = @TableName1 AND Entity1Code_ID = @Entity1Code_ID AND TableName2 = 'Application') " +
"AND COALESCE(Tenant_ID, 0) = COALESCE(@Tenant_ID, 0) ";
return conn.Query<Application>(sql, new
{
_tenant.Tenant_ID,
TableName1 = GetType().Name,
Entity1Code_ID = GetId()
});
}
}