Dev2.DynamicServices.FileSystemInstanceStoreIO.LoadInstance C# (CSharp) Method

LoadInstance() public method

public LoadInstance ( System.Guid instanceId, InstanceValue>.IDictionary &instanceData, InstanceValue>.IDictionary &instanceMetadata ) : System.Boolean
instanceId System.Guid
instanceData InstanceValue>.IDictionary
instanceMetadata InstanceValue>.IDictionary
return System.Boolean
        public Boolean LoadInstance(Guid instanceId, out IDictionary<XName, InstanceValue> instanceData, out IDictionary<XName, InstanceValue> instanceMetadata)
        {
            try
            {
                instanceData = new Dictionary<XName, InstanceValue>();
                instanceMetadata = new Dictionary<XName, InstanceValue>();

                String fileName = String.Format("{0}.xml", instanceId);
                String fullPath = Path.Combine(_dataDirectory, fileName);

                if(!File.Exists(fullPath))
                {
                    return false;
                }

                NetDataContractSerializer serializer = new NetDataContractSerializer();

                //load instance data
                XElement xml = XElement.Load(fullPath);
                var xElement = xml.Element("InstanceData");
                if(xElement != null)
                {
                    var entries =
                        (from e in xElement.Elements("Entry")
                         select e).ToList();
                    foreach(XElement entry in entries)
                    {
                        LoadSingleEntry(serializer, instanceData, entry);
                    }

                    //load instance metadata
                    fileName = String.Format("{0}.meta.xml", instanceId);
                    fullPath = Path.Combine(_dataDirectory, fileName);
                    xml = XElement.Load(fullPath);
                    var element = xml.Element("InstanceMetadata");
                    if(element != null)
                    {
                        entries =
                            (from e in element.Elements("Entry")
                             select e).ToList();
                    }
                    foreach(XElement entry in entries)
                    {
                        LoadSingleEntry(serializer, instanceMetadata, entry);
                    }
                }
            }
            catch(Exception exception)
            {
                Dev2Logger.Log.Error(exception);
                throw new InstancePersistenceException(exception.Message, exception);
            }

            return true;
        }

Usage Example

        // ReSharper disable InconsistentNaming - Unit Tests
        public void LoadInstance_With_FileUsedByAnotherProcess_Expected_ThrowsInstancePersistenceException()
        // ReSharper restore InconsistentNaming
        {
            var instanceId = Guid.NewGuid();
            var path = Path.Combine(TestPath, instanceId + ".xml");
            var metaPath = Path.Combine(TestPath, instanceId + ".meta.xml");

            try
            {
                var xml = XmlResource.Fetch(TestFileName);
                xml.Save(path);
                xml = XmlResource.Fetch(TestMetaFileName);
                xml.Save(metaPath);

                // Force error: The process cannot access the file because it is being used by another process
                using(var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    var store = new FileSystemInstanceStoreIO();
                    IDictionary<XName, InstanceValue> instanceData;
                    IDictionary<XName, InstanceValue> instanceMetadata;

                    var result = store.LoadInstance(instanceId, out instanceData, out instanceMetadata);
                }
            }
            finally
            {
                if(File.Exists(path))
                {
                    File.Delete(path);
                }
                if(File.Exists(metaPath))
                {
                    File.Delete(metaPath);
                }
            }
        }