Skip to content

C# library for managing shared ownership of Disposable reosources

License

Notifications You must be signed in to change notification settings

bashrc-real/SharedOwner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages