public void LoadById(string SearchItem)
{
var myTechnicTasks = engTaskCtx.Tasks.Where(task => task.MyTechnicReference.StartsWith(SearchItem)).Select(task => new MyTask()
{
id = task.id,
MyTechnicReference = task.MyTechnicReference,
MPDReference = task.MPDReference,
tasktypeid = task.tasktypeid,
shortdesc = task.shortdesc,
interval = task.interval,
critical = task.critical,
mandatory = task.mandatory,
dupinsp = task.dupinsp,
dualsystemmaint = task.dualsystemmaint,
MPDSkill = task.MPDSkill,
MPDCrew = task.MPDCrew,
MPDAccessMH = task.MPDAccessMH,
MPDTotalMH = task.MPDTotalMH,
extratime = task.extratime,
Team = task.Team,
MaintData = EngGetCalculatedTaskField.GetMaintData(task.id),
AliSpReqs = EngGetCalculatedTaskField.GetAliSpReqs(task.id),
Access = EngGetCalculatedTaskField.GetAccess(task.id),
preperation = task.preperation,
longdesc = task.longdesc,
applnotes = task.applnotes
});
MyTechnicTaskList = myTechnicTasks.ToList();
}
public static class EngGetCalculatedTaskField
{
private static TaskMaintenanceDataDataContext engTaskCtx { get; set; }
public static string GetMaintData(int taskID)
{
try
{
using (TaskCardContext.TaskMaintenanceDataDataContext dc = new TaskCardContext.TaskMaintenanceDataDataContext())
{
string maintenanceData = String.Empty;
foreach (var item in dc.TaskRelations.Where(tableRaletions => tableRaletions.TaskId == taskID && tableRaletions.RelTypeId == 12))
{
maintenanceData += item.RefMaintenance.shortdesc + "; ";
}
return maintenanceData.Substring(0, maintenanceData.Length - 2);
}
}
catch
{
return String.Empty;
}
}
public static string GetAliSpReqs(int taskID)
{
#region Old
try
{
using (TaskCardContext.TaskMaintenanceDataDataContext dc = new TaskCardContext.TaskMaintenanceDataDataContext())
{
string aliSpReqs = String.Empty;
foreach (var item in dc.TaskRelations.Where(tableRaletions => tableRaletions.TaskId == taskID && tableRaletions.RelTypeId == 13))
{
aliSpReqs += item.RefAliSpReq.shortdesc + "; ";
}
return aliSpReqs.Substring(0, aliSpReqs.Length - 2);
}
}
catch
{
return String.Empty;
}
#endregion
}
public static string GetAccess(int taskID)
{
#region Old
try
{
using (TaskCardContext.TaskMaintenanceDataDataContext dc = new TaskCardContext.TaskMaintenanceDataDataContext())
{
string access = String.Empty;
foreach (var item in dc.TaskRelations.Where(tableRaletions => tableRaletions.TaskId == taskID && tableRaletions.RelTypeId == 15))
{
access += item.RefAccessPanel.shortdesc + "; ";
}
return access.Substring(0, access.Length - 2);
}
}
catch
{
return String.Empty;
}
#endregion
}
}
Refactorings
No refactoring yet !
Ants
August 12, 2010, August 12, 2010 19:26, permalink
In your other post "How can i use generic class to write useful codes via linq" it looks like you are already exploring a technique to refactor lines 34-109.
My only other recommendation is consider the following things revolving delaying work to be done:
1) Does MyTechnicTaskList that you fill up on line 28 really have to be a List<MyTask>? If it can simply be an IEnumerable<MyTask>, all you need to do build up the query and assign it to MyTechnicCollection. The query will do things lazily and only return items as you request each item. With your current code, when you call .ToList() this forces all the data to be requested at once.
2) When you fill in the MaintData, AliSpReqs, and Access fields, you are doing all the work at MyTask() object initialization time. Have you considered making these properties that are Lazy<string> ?
3) Do you really need to copy all the properties from Task to MyTask? Can't you just forward the calls to the Task?
class MyTask
{
Task _task;
// Return lazily computed properties
Lazy<string> _maintData;
Lazy<string> _aliSpReqs;
Lazy<string> _access;
public string MaintData { get { return _maintData.Value; } }
public string AliSpReqs { get { return _aliSpReqs.Value; } }
public string Access { get { return _access.Value; } }
// Forward calls to the original Task
public int id { get { return _task.id; } }
public string MyTechnicReference { get { return _task.MyTechnicReference; } }
public string MPDReference { get { return _task.MPDReference; } }
public string tasktypeid { get { return _task.tasktypeid; } }
public string shortdesc { get { return _task.shortdesc; } }
public string interval { get { return _task.interval; } }
public string critical { get { return _task.critical; } }
public string mandatory { get { return _task.mandatory; } }
public string dupinsp { get { return _task.dupinsp; } }
public string dualsystemmaint { get { return _task.dualsystemmaint; } }
public string MPDSkill { get { return _task.MPDSkill; } }
public string MPDCrew { get { return _task.MPDCrew; } }
public string MPDAccessMH { get { return _task.MPDAccessMH; } }
public string MPDTotalMH { get { return _task.MPDTotalMH; } }
public string extratime { get { return _task.extratime; } }
public string Team { get { return _task.Team; } }
public string preperation { get { return _task.preperation; } }
public string longdesc { get { return _task.longdesc; } }
public string applnotes { get { return _task.applnotes; } }
public MyTask(Task task)
{
_task = task;
// Teach lazy computed properties how to compute their values
_maintData = new Lazy<string>(() => EngGetCalculatedTaskField.GetMaintData(_task.id));
_aliSpReqs = new Lazy<string>(() => EngGetCalculatedTaskField.GetAliSpReqs(_task.id));
_access = new Lazy<string>(() => EngGetCalculatedTaskField.GetAccess(_task.id));
}
}
class MyTechnicTaskEnumeration
{
public IEnumerable<MyTask> LoadById(string searchItem)
{
using (TaskMaintenanceDataDataContext dc = new TaskMaintenanceDataDataContext())
{
return dc.Tasks
.Where(task => task.MyTechnicReference
.StartsWith(searchItem))
.Select(task => new MyTask(task));
}
}
}
public static class EngGetCalculatedTaskField
{
static string GetData(int taskId, int relTypeId, Func<TaskRelation, string> select)
{
try
{
using (TaskMaintenanceDataDataContext dc = new TaskMaintenanceDataDataContext())
{
var query = dc.TaskRelations
.Where(r => r.TaskId == taskId && r.RelTypeId == relTypeId)
.Select(select);
return string.Join("; ", query.ToArray());
}
}
catch
{
return String.Empty;
}
}
public static string GetMaintData(int taskID)
{
return GetData(taskID, 12, r => r.RefMaintenance.shortdesc);
}
public static string GetAliSpReqs(int taskID)
{
return GetData(taskID, 13, r => r.RefAliSpReq.shortdesc);
}
public static string GetAccess(int taskID)
{
return GetData(taskID, 15, r => r.RefAccessPanel.shortdesc);
}
}
AnonaMoose
August 14, 2010, August 14, 2010 20:38, permalink
AutoMapper Mapping it inside of your select statement :D
Ants
August 16, 2010, August 16, 2010 05:28, permalink
@AnonaMoose: AutoMapper looks pretty cool for cutting down on the number of lines and repetitive stuff, but how will it achieve the goal of improving performance?
As far as I know, invoking Mapper.Map<>() will do the assignment for all the mapped fields at that time. No work is deferred. Additionally, you now have the overhead of AutoMapper performing reflection calls to do sets/gets which is slower than compiled sets/gets.
hi; i try to run my codes. My program more slowly runnig. i need to give performance also write less codes in GetAliSpReqs(), GetMaintData();GetAccess....GET(...
How can i write more effective below codes. They are too slow also not useful. i try to write les than 1-2 line? How can i ? please help me...