I have an app with repository pattern. It allows me to manipulate objects via LINQ, objects are stored in memory, so I can access them very fast. Here a sample code:
private Measurement ParseExact(AgentParameter agentParameter)
{
ControlledElement ce;
using (var repositoryBase = Datastore.GetRepository<ControlledElement>())
{
var mvId = Convert.ToInt32(agentParameter.ControlledParameterId);
var sId = Convert.ToInt32(agentParameter.FacilityId);
ce =
repositoryBase.Query(
t => t.FirstOrDefault(elem => elem.Sensor.Id == sId && elem.MeasuringValue.Id == mvId));
}
}
When I profiled my code with dotTrace I found that on high load I get a performance lack on creating delegate elem => elem.Sensor.Id == sId && elem.MeasuringValue.Id == mvId. My Query method looks like:
public TOut Query<TOut>(Func<IQueryable<TEntity>, TOut> specification) which means, that I really pass a Func<> object every time I use it. So, the question is, how can I optimize this?
EDIT proof of lack on creation and compilation

You can eliminate the compilation step by explicitly tracking the state in an object, rather than using a closure.
private Measurement ParseExact(AgentParameter agentParameter)
{
ControlledElement ce;
using (var repositoryBase = Datastore.GetRepository<ControlledElement>())
{
var mvId = Convert.ToInt32(agentParameter.ControlledParameterId);
var sId = Convert.ToInt32(agentParameter.FacilityId);
var query = new ParseExactQuery(mvId, sId);
ce = repositoryBase.Query(t => t.FirstOrDefault(query.Query));
}
}
private class ParseExactQuery {
private int mvId;
private int sId;
public ParseExactQuery (int mvId, int sId) {
this.mvId = mvId;
this.sId = sId;
}
public bool Query(ControlledElement elem) {
return elem.Sensor.Id == sId && elem.MeasuringValue.Id == mvId;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With