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

GetInstanceAssociation() public method

public GetInstanceAssociation ( System.Guid instanceKey ) : System.Guid
instanceKey System.Guid
return System.Guid
        public Guid GetInstanceAssociation(Guid instanceKey)
        {
            Guid instanceId = Guid.Empty;
            try
            {
                var files = Directory.GetFiles(_dataDirectory, string.Format("Key.{0}.*.xml", instanceKey));
                if(files.Length > 0)
                {
                    // TWR: Changed to use filename only as full path might also include periods!!
                    var fileName = Path.GetFileName(files[0]);
                    if(fileName != null)
                    {
                        var nodes = fileName.Split('.');
                        if(nodes.Length == 4)
                        {
                            Guid.TryParse(nodes[2], out instanceId);
                        }
                    }
                }
            }
            catch(Exception exception)
            {
                Dev2Logger.Log.Error(exception);
                throw new InstancePersistenceException(exception.Message, exception);
            }
            return instanceId;
        }

Usage Example

        // ReSharper disable InconsistentNaming - Unit Tests
        public void GetInstanceAssociation_With_InvalidFileName_Expected_ReturnsEmptyGuid()
        // ReSharper restore InconsistentNaming
        {
            const string InvalidInstanceId = "99999";
            var instanceKey = Guid.NewGuid();
            var path = Path.Combine(TestPath, string.Format("Key.{0}.{1}.xml", instanceKey, InvalidInstanceId));

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

                var store = new FileSystemInstanceStoreIO();
                var result = store.GetInstanceAssociation(instanceKey);
                Assert.AreEqual(Guid.Empty, result);
            }
            finally
            {
                if(File.Exists(path))
                {
                    File.Delete(path);
                }
            }
        }