/// <inheritdoc />
        protected override void ManagedDisposal()
        {
            this.shareable.Unshare();

            // in case the disposable share is held onto after disposing, the shareable is set to null to allow garbage collection
            this.shareable = null;
        }
    /// <summary>
    /// Shares the instance with another client and returns a token that can be disposed of later to make an accompanying call
    /// to <see cref="IShareable.Unshare"/>.
    /// </summary>
    /// <param name="shareable">The shareable resource.</param>
    /// <returns>An <see cref="IDisposable"/> that will call <see cref="IShareable.Unshare"/> when disposed.</returns>
    /// <remarks>
    /// If sharing in a Using block, use <see cref="ShareInUsingBlock"/> for less object creation and garbage collection.
    /// </remarks>
    public static IDisposable ShareAsDisposable(this IShareable shareable)
    {
        Contracts.Requires.That(shareable != null);

        shareable.Share();
        return(new DisposableShare(shareable));
    }
    /// <summary>
    /// Share this instance with another client in the context of a Using block, guaranteeing that
    /// an accompanying call to <see cref="IShareable.Unshare"/> is made upon exiting the Using block.
    /// </summary>
    /// <param name="shareable">The shareable resource.</param>
    /// <returns>
    /// A <see cref="ShareStruct"/> that will manage calling <see cref="IShareable.Unshare"/>.
    /// </returns>
    /// <remarks>
    /// <para>
    /// This should be used within a Using block and you should not call <see cref="IShareable.Share"/> or
    /// <see cref="IShareable.Unshare"/> on the shareable resource, nor should you manually call
    /// <see cref="IDisposable.Dispose"/> on the <see cref="ShareStruct"/> returned.
    /// </para><para>
    /// Declare the disposable resource in the Using block as <c>var</c> or <c>ShareStruct</c> and not as <c>IDisposable</c>
    /// or else boxing will occur.
    /// </para>
    /// </remarks>
    public static ShareStruct ShareInUsingBlock(this IShareable shareable)
    {
        Contracts.Requires.That(shareable != null);

        shareable.Share();
        return(new ShareStruct(shareable));
    }
Exemplo n.º 4
0
        internal static IEnumerable <PropertyInfo> GetProperties(IShareable obj)
        {
            var type = obj.GetType();

            if (!_classProperties.ContainsKey(type))
            {
                _classProperties[type] = type.GetProperties().Where(p => (typeof(IShareable).IsAssignableFrom(p.PropertyType)));
            }
            return(_classProperties[type]);
        }
Exemplo n.º 5
0
 internal static void Build(IShareable obj, Cache cache)
 {
     foreach (var prop in GetProperties(obj.GetType()))
     {
         if (!cache.ContainsKey(prop.PropertyType))
         {
             IShareable shareable = New(prop.PropertyType);
             cache[prop.PropertyType] = shareable;
             Build(shareable, cache);
         }
         SetPropertyValue(obj, prop, cache[prop.PropertyType]);
         prop.SetValue(obj, cache[prop.PropertyType]);
     }
 }
Exemplo n.º 6
0
        public void BuildHierarchy()
        {
            var props = Hierarchy.GetProperties(this);

            foreach (var prop in props)
            {
                if (!Cache.ContainsKey(prop.PropertyType))
                {
                    //dynamic ctor = Activator.CreateInstance(prop.PropertyType);
                    IShareable ctor = Hierarchy.New(prop.PropertyType);
                    Cache[prop.PropertyType] = ctor;
                    ctor.Cache = this.Cache;
                    ctor.BuildHierarchy();
                }
                // var setter = Injector.GetSetter(prop);
                prop.SetValue(this, Cache[prop.PropertyType]);
            }
            //this.Cache = cache;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ShareStruct"/> struct.
        /// </summary>
        /// <param name="shareable">The shareable resource.</param>
        internal ShareStruct(IShareable shareable)
        {
            Contracts.Requires.That(shareable != null);

            this.shareable = shareable;
        }
Exemplo n.º 8
0
 public Owners(IShareable parent)
 {
     this.parent = parent as Factory;
 }
Exemplo n.º 9
0
 public static void Unshare(IShareable instance)
 {
     Contracts.Requires.That(instance != null);
     Contracts.Requires.That(instance.IsShared);
 }
Exemplo n.º 10
0
 private static void SetPropertyValue <T>(T obj, PropertyInfo prop, IShareable shareable) where T : IShareable
 {
     throw new NotImplementedException();
 }
Exemplo n.º 11
0
        internal static void BuildHierarchy(IShareable obj)
        {
            var cache = new Cache();

            Build(obj, cache);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="DisposableShare"/> class.
        /// </summary>
        /// <param name="shareable">The shareable resource.</param>
        public DisposableShare(IShareable shareable)
        {
            Contracts.Requires.That(shareable != null);

            this.shareable = shareable;
        }