我有一个反序列化 Xml 文档并从中创建对象的函数。
我希望将对象存储到缓存中,因此每次需要从中获取数据时我都不必反序列化 xml。
public class XMLDeserializer
{
public event OnElementDeserialized OnElementDeserializedCallback;
public void DeserializeXml(string xmlPath)
{
// implementation
}
}
public class XMLDeserializerFacade
{
private static object _lockObject = new object();
private XMLDeserializer xmlDeserializer;
private ICacheProvider cacheProvider;
public XMLDeserializerFacade(XMLDeserializer xmlDeserializer, ICacheProvider cacheProvider)
{
this.xmlDeserializer = xmlDeserializer;
this.cacheProvider = cacheProvider;
xmlDeserializer.OnElementDeserializedCallback += delegate(object element)
{
cacheProvider.Add("uniqueKey", element);
// is here in lock as well or i have to lock it again?
};
}
public void DeserializeXml(string xmlPath)
{
lock(_lockObject)
{
xmlDeserializer.DeserializeXml(xmlPath);
// From here it will go to
// cacheProvider.Add("uniqueKey", element); callback
}
}
}
当我想反序列化 xml 时,我调用
XMLDeserializerFacade.DeserializeXml("file.xml")
我的问题是我lock
也应该在OnElementDeserializedCallback
回调中使用?
谢谢