< Summary

Information
Class: SolidEdgeCommunity.ConnectionPointController
Assembly: SolidEdgeCommunity
File(s): D:\a\SolidEdge.Community\SolidEdge.Community\src\SolidEdgeCommunity\ConnectionPointController.cs
Line coverage
100%
Covered lines: 55
Uncovered lines: 0
Coverable lines: 55
Total lines: 154
Line coverage: 100%
Branch coverage
95%
Covered branches: 19
Total branches: 20
Branch coverage: 95%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
AdviseSink(...)100%66100%
IsSinkAdvised(...)75%44100%
UnadviseSink(...)100%66100%
UnadviseAllSinks()100%44100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Runtime.InteropServices.ComTypes;
 4using System.Threading;
 5
 6namespace SolidEdgeCommunity;
 7
 8/// <summary>
 9/// Default controller that handles connecting\disconnecting to COM events via IConnectionPointContainer and IConnection
 10/// </summary>
 11public class ConnectionPointController
 12{
 13    private readonly object _sink;
 414    private readonly Dictionary<IConnectionPoint, int> _connectionPointDictionary = [];
 15
 416    public ConnectionPointController(object sink)
 17    {
 418        ArgumentNullException.ThrowIfNull(sink);
 19
 420        _sink = sink;
 421    }
 22
 23    /// <summary>
 24    /// Establishes a connection between a connection point object and the client's sink.
 25    /// </summary>
 26    /// <typeparam name="TInterface">Interface type of the outgoing interface whose connection point object is being req
 27    /// <param name="container">An object that implements the IConnectionPointContainer inferface.</param>
 28    public void AdviseSink<TInterface>(object container) where TInterface : class
 29    {
 530        bool lockTaken = false;
 31
 32        try
 33        {
 534            Monitor.Enter(this, ref lockTaken);
 35
 36            // Prevent multiple event Advise() calls on same sink.
 537            if (IsSinkAdvised<TInterface>(container))
 38            {
 139                return;
 40            }
 41
 42            IConnectionPointContainer cpc = null;
 443            int cookie = 0;
 44
 445            cpc = (IConnectionPointContainer)container;
 446            var item = typeof(TInterface).GUID;
 447            cpc.FindConnectionPoint(ref item, out IConnectionPoint cp);
 48
 449            if (cp != null)
 50            {
 451                cp.Advise(_sink, out cookie);
 452                _connectionPointDictionary.Add(cp, cookie);
 53            }
 454        }
 55        finally
 56        {
 557            if (lockTaken)
 58            {
 559                Monitor.Exit(this);
 60            }
 561        }
 562    }
 63
 64    /// <summary>
 65    /// Determines if a connection between a connection point object and the client's sink is established.
 66    /// </summary>
 67    /// <param name="container">An object that implements the IConnectionPointContainer inferface.</param>
 68    public bool IsSinkAdvised<TInterface>(object container) where TInterface : class
 69    {
 970        bool lockTaken = false;
 71
 72        try
 73        {
 974            Monitor.Enter(this, ref lockTaken);
 75
 76            IConnectionPointContainer cpc = null;
 77            int cookie = 0;
 78
 979            cpc = (IConnectionPointContainer)container;
 980            var item = typeof(TInterface).GUID;
 981            cpc.FindConnectionPoint(ref item, out IConnectionPoint cp);
 82
 983            return (cp != null) && _connectionPointDictionary.ContainsKey(cp);
 84        }
 85        finally
 86        {
 987            if (lockTaken)
 88            {
 989                Monitor.Exit(this);
 90            }
 991        }
 992    }
 93
 94    /// <summary>
 95    /// Terminates an advisory connection previously established between a connection point object and a client's sink.
 96    /// </summary>
 97    /// <typeparam name="TInterface">Interface type of the interface whose connection point object is being requested to
 98    /// <param name="container">An object that implements the IConnectionPointContainer inferface.</param>
 99    public void UnadviseSink<TInterface>(object container) where TInterface : class
 100    {
 1101        bool lockTaken = false;
 102
 103        try
 104        {
 1105            Monitor.Enter(this, ref lockTaken);
 106
 107            IConnectionPointContainer cpc = null;
 108
 1109            cpc = (IConnectionPointContainer)container;
 1110            var item = typeof(TInterface).GUID;
 1111            cpc.FindConnectionPoint(ref item, out IConnectionPoint cp);
 112
 1113            if (cp != null && _connectionPointDictionary.TryGetValue(cp, out int cookie))
 114            {
 1115                cp.Unadvise(cookie);
 1116                _connectionPointDictionary.Remove(cp);
 117            }
 1118        }
 119        finally
 120        {
 1121            if (lockTaken)
 122            {
 1123                Monitor.Exit(this);
 124            }
 1125        }
 1126    }
 127
 128    /// <summary>
 129    /// Terminates all advisory connections previously established.
 130    /// </summary>
 131    public void UnadviseAllSinks()
 132    {
 1133        bool lockTaken = false;
 134
 135        try
 136        {
 1137            Monitor.Enter(this, ref lockTaken);
 1138            Dictionary<IConnectionPoint, int>.Enumerator enumerator = _connectionPointDictionary.GetEnumerator();
 2139            while (enumerator.MoveNext())
 140            {
 1141                enumerator.Current.Key.Unadvise(enumerator.Current.Value);
 142            }
 1143        }
 144        finally
 145        {
 1146            _connectionPointDictionary.Clear();
 147
 1148            if (lockTaken)
 149            {
 1150                Monitor.Exit(this);
 151            }
 1152        }
 1153    }
 154}