bashrc-real / SharedOwner

C# library for managing shared ownership of Disposable reosources

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SharedOwner

A library for managing shared resources in C# almost equivalent to shared_ptr

Overview

Despite having the luxury of garbage collector, items that derive from IDisposable needs to be almost treated like a native resource. The owner must take the responsibility of calling Dispose once all the consumers of the resource are no longer using the api. But for some shared reosources their is no implicit owner and hence it becomes difficult to clean up resources in a deterministic manner. This library provides ref counted handle objects managed by a owner class which has the responsibility of calling Dispose once the ref count of the underlying object becomes 0.

Usage

Passing resources to other classes:

var sharedConnection = SharedOwner.SharedHandle<HttpClient>.MakeSharedHandle(() => new HttpClient());
this.m_obj = new Object1(sharedClient);
this.m_obj2 = new Object1(sharedClient);

Getting the object in the client side:

using (var sharedClient = this.m_sharedClient.GetHandle()) // m_sharedClient is the SharedHandle passed
{
     var httpClient = (HttpClient)sharedClient;
     // use httpClient
}
// **Do not use httpClient beyond the using scope of the intermediate handle from GetHandle**

Check BasicTests.cs for sample usage.

About

C# library for managing shared ownership of Disposable reosources

License:MIT License


Languages

Language:C# 100.0%