I have an assembly whose purpose is basic logging.
I have other assemblies that reference this assembly.
Is there a way to share an object instance across the referencing assemblies? So, that for one logging activity just one instance of the logging class is used?
For example, if the method inside Logger assembly/namespace is called AddInfo(). When Assembly A has a class that needs to log information it uses loggerInstance1.AddInfo() ... and when Assembly B needs to do the same...it re-uses the same loggerInstance1.AddInfo() ... and not loggerInstance2.AddInfo()
...and now for something completely different.
Another totally different strategy would be to reference system.web (yes, you can reference this assembly even in a desktop app):
using System.Web;
Then put any classes that you want to be "globally shared" into the Cache:
HttpRuntime.Cache.Insert("Logger", LoggingObject, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration);
Finally, to get an instance of your global object, just pull it from the Cache:
object LoggingObject = HttpRuntime.Cache.Get("Logger");
BAM. Instant globally accessible, single-use objects, no Singleton pattern required.
Of course, you could also change the logic a bit to attempt to get the Logger, and if it does not already exist then create it for the first time and put it into the cache for future use. It just depends on if you know the order of access or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With