Re: How to I access a logging class from any other class.



Responding to Ssg31415926...

I want to be able to write log data.  How can I access my logging class
from any class in the app, without passing a reference to it?

I'll assume you only have one instance of the logging class.

First let me point out that implementing relationships by passing objects is usually not a good idea. The problem is that conceptually one has:

             R1                   R2
[Client] --------- [Service1] ----------- [Service2]

where the Client is instantiating the R2 object when it passes a reference to Service2 to Service1. However, participation in the R2 relationship is a personal matter between [Service1] and [Service2]. But for Client to pass the /right/ instance of [Service2], the Client must understand the rules and policies related to defining participation in the R2 relationship. Typically those rules and policies are unrelated to the collaboration context between Client and Service1 via the R1 relationship. So one has effectively given [Client] responsibilities that probably belong elsewhere (e.g., in an object from the GoF factory patterns whose mission in life is to understand instantiation of Service1 and/or Service2 and the nature of their participation in the R2 relationship). IOW, one has created unnecessary implementation dependencies that violate OO encapsulation. So one should only pass object references ot constructors or setters for referential attributes.

I think what you are really asking is how you can access it without instantiating an explicit relationship to it from every object that needs logging facilities. The short answer is that you can't. However, there are ways to reduce the tedium.

One way is via relationship paths. In a class model it is highly unusual to have a model where all classes are not somehow connected through relationship paths. IOW, one has

[Client] ----- [A] ------ [B] ----- ... ----- [Z] ------ [Logger]

so rather than passing a reference to the Client, the Client can "walk" the relationships to get to the logger (assuming the relationships are all instantiated by reference attributes). If one judiciously relates [Logger] to a couple of classes in the middle of the model, then the path to navigate will probably be quite short from any given client.

Another possibility is to use the GoF Singleton pattern. Singleton essentially disguises the notion of a global instance as a local implementation instance. Each object needing logging creates its private [Logger] but Singleton takes care of the redundancy. While this Just Works, from an aesthetic viewpoint it really isn't what Singleton was designed to handle (i.e., enforcing a business rule that /requires/ only a single instantiation). However, one can rationalize this at the OOP level as an optimization to avoid the path walking of the first solution when there is only going to be one instance anyway.

[Note that the relationship between [Client] and [Logger] still exists and it is being implemented. But that is being done at the private object implementation level so, by convention, we don't worry about it. (E.g., we wouldn't dream of cluttering a UML Class Model with it).]

Yet another possibility is to use composition inheritance so that all objects needing such facilities inherit from the [Logger] class. That produces the same relationship clutter at the OOA/D level but it is very simple at the OOP level. Alas, there are a number of reasons why it is a Bad Idea, mostly related to cohesion, logical indivisibility, and maintainability. So don't go there.


************* There is nothing wrong with me that could not be cured by a capful of Drano.

H. S. Lahman
hsl@xxxxxxxxxxxxxxxxx
Pathfinder Solutions  -- Put MDA to Work
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
(888)OOA-PATH



.



Relevant Pages

  • Re: C# ServicedComponent Singleton
    ... > has released the reference to the object. ... reference a COM+ singleton object. ... Each client creation call will get a reference to the same instance provided ... call will be suspended (activation time-out) whenever another client is ...
    (microsoft.public.dotnet.languages.csharp)
  • COM component event interaction question
    ... remoted reference to the singleton COM object. ... works except for events on the client. ... // Brokerage Reference ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Searching OO Associations with RDBMS Persistence Models
    ... and the Client needs access to locations for a particular Company. ... In this case I would argue that it should be a reference. ... There are three aspects to relationships: implementation, instantiation, and navigation. ... This sort of instantiation is ubiquitous in OO applications so that one very rarely ever sees a FIND WHERE construct. ...
    (comp.object)
  • Re: VS2008 creates a 2nd endpoint when actualising a web reference
    ... you are developing an WCF service and a client through VS 2008. ... The service used to use wshttpbinding, and when you switch it to netTcpBinding and regenerate the service reference at client, you found the client generate two configuration section for the service endpoint, correct? ... We welcome your comments and suggestions about how we can improve the support we provide to you. ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Help on remoting clarification ???
    ... IPurchaseOrderService on the client side that handles getting the ... object reference fromt he remote service, ... They get a reference of the remote object by using the ... Lets say i want to create a purchase order application where the ...
    (microsoft.public.dotnet.framework.remoting)