Is an entity thread safe? I gather not. Are reads? Should I just worry about writes or related entities? What approach might I use to accomplish this beside using lock(Entity) every time it is used?
Just for giggles here is what I am doing: I have singleton classes which wrap an EntityCollection. The singleton loads data to provide a cached application scope version. The singleton can do various searches of the collection and return a matching Entity. Multiple threads will be accessing those entities, related entities, and in rare cases making additions/changes.
Example, and since I am new with LLBL I am all for a better mousetrap:
public sealed class ClientNetsList
{
public static readonly ClientNetsList m_instance = new ClientNetsList();
private static ClientNetCollection m_ClientNetCollection;
static ClientNetsList()
{
m_ClientNetCollection = new ClientNetCollection();
ISortExpression sorter = new SortExpression(ClientNetFields.ClientNetId | SortOperator.Descending);
m_ClientNetCollection.GetMulti(null, long.MaxValue, sorter);
}
public ClientNetsList()
{
}
public static ClientNetsList Instance
{
get
{
return m_instance;
}
}
public ClientNetEntity ClientNetById(int ClientNetId)
{
List<int> Ndx;
lock(m_ClientNetCollection)
{
IPredicate filter = (ClientNetFields.ClientNetId == ClientNetId);
Ndx = m_ClientNetCollection.FindMatches(filter);
if (Ndx.Count > 0)
return m_ClientNetCollection[Ndx[0]];
else
return null;
}
}
public ClientNetEntity this[int index]
{
get
{
lock (m_ClientNetCollection)
{
return m_ClientNetCollection[index];
}
}
}
public int Count
{
get
{
lock (m_ClientNetCollection)
{
return m_ClientNetCollection.Count;
}
}
}
}