< Summary

Information
Class: SolidEdgeCommunity.EventSink<T>
Assembly: SolidEdgeCommunity
File(s): D:\a\SolidEdge.Community\SolidEdge.Community\src\SolidEdgeCommunity\EventSink.cs
Line coverage
92%
Covered lines: 35
Uncovered lines: 3
Coverable lines: 38
Total lines: 112
Line coverage: 92.1%
Branch coverage
75%
Covered branches: 9
Total branches: 12
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%210%
Dispose()100%11100%
Dispose(...)100%11100%
Connect(...)66.66%6692.85%
Disconnect()83.33%6693.33%

File(s)

D:\a\SolidEdge.Community\SolidEdge.Community\src\SolidEdgeCommunity\EventSink.cs

#LineLine coverage
 1using System;
 2using System.Runtime.InteropServices.ComTypes;
 3using System.Threading;
 4
 5namespace SolidEdgeCommunity;
 6
 7public abstract class EventSink<T> : IDisposable where T : class
 8{
 9    private IConnectionPoint _connectionPoint;
 10    private int _cookie;
 11
 212    protected EventSink()
 13    {
 214    }
 15
 16    /// <summary>
 17    /// Establishes a connection between a connection point object and the client's sink.
 18    /// </summary>
 19    /// <param name="source">An event source that implements IConnectionPointContainer.</param>
 020    protected EventSink(object source) => Connect(source); // If event source is specified, automatically connect.
 21
 22    public void Dispose()
 23    {
 124        Dispose(true);
 125        GC.SuppressFinalize(this);
 126    }
 27
 28    protected virtual void Dispose(bool disposing)
 29    {
 130        if (disposing)
 31        {
 32            // Clean up all managed resources
 33        }
 34
 135        Disconnect();
 136    }
 37
 38    /// <summary>
 39    /// Establishes a connection between a connection point object and the client's sink.
 40    /// </summary>
 41    /// <param name="source">An event source that implements IConnectionPointContainer.</param>
 42    public void Connect(object source)
 43    {
 244        bool lockTaken = false;
 245        IConnectionPointContainer container = null;
 46
 47        try
 48        {
 249            Monitor.Enter(this, ref lockTaken);
 50
 51            // If previous call was made, disconnect existing connection.
 252            if (container != null)
 53            {
 054                Disconnect();
 55            }
 56
 57            // QueryInterface for IConnectionPointContainer.
 258            container = (IConnectionPointContainer)source;
 59
 260            var item = typeof(T).GUID;
 61            // Find the connection point by the GUID of type T.
 262            container.FindConnectionPoint(ref item, out _connectionPoint);
 63
 64            // Establish the sink connection.
 265            _connectionPoint?.Advise(this, out _cookie);
 266        }
 67        finally
 68        {
 269            if (lockTaken)
 70            {
 271                Monitor.Exit(this);
 72            }
 273        }
 274    }
 75
 76    /// <summary>
 77    /// Terminates the advisory connection between a connection point and a client's sink.
 78    /// </summary>
 79    public void Disconnect()
 80    {
 281        bool lockTaken = false;
 82
 83        try
 84        {
 285            Monitor.Enter(this, ref lockTaken);
 86
 87            // Terminate the sink connection.
 288            _connectionPoint?.Unadvise(_cookie);
 289        }
 90        finally
 91        {
 292            if (_connectionPoint != null)
 93            {
 94                try
 95                {
 296                    var count = System.Runtime.InteropServices.Marshal.ReleaseComObject(_connectionPoint);
 097                }
 298                catch
 99                {
 2100                }
 101
 2102                _connectionPoint = null;
 2103                _cookie = 0;
 104            }
 105
 2106            if (lockTaken)
 107            {
 2108                Monitor.Exit(this);
 109            }
 2110        }
 2111    }
 112}