开发者

basic c# architecture and class handling question

Ok guys, this is very basic stuff I think.

I'm only getting familiar with C# and object oriented languages.

So here is a basic project I'm developping :

namespace MyProject  
{  
    class MyProject  
    {
        public Logger Logs = new Logger();
        public WebServer Web = new WebServer();

        static void Main()
        {  
            // blabla
        }  
开发者_JS百科    }  
}

The logger is there to simply create a log file and when its "Add(string str)" method is called, it adds a line in the log file. The webserver is my own webserver, opening a socket etc ... I'd like my webserver to be able to use my Add method of the logger... how am I supposed to achieve that ?

Inside my webserver class, using something like "MyProject.Logs.Add(string str)" doesn't seem to work. So how can I reference it ? I don't want to create a new instance of my Logger class inside my webserver, because I want to write everyting in the same text file (I might have more module running than just a webserver as well). So I'd like to use the instance I created in my Main method, inside my Webserver class...

I hope this makes sense ...


A couple of suggestions:

  1. Logs and Web should be private. Those are implementation details of your class. They're nobody else's business.
  2. Pass the instance of your logger to your web server:

    public WebServer Web = new WebServer(Logs); 
    

Your web server can now have its very own copy:

private Logger Logs {get;set;}

public WebServer(Logger logs)
{
    Logs = logs;
}

public void DoSomething()
{
    Logs.Add("I did something!");
}


MyProject.Logs.Add(string str) does not work because Logs member is not static, which means it can be different for different instances of your class (which are created by operator new). The compiler can't know which instance you are referring to if you don't specify it.

If you mark Logs as static, then it will work.

However, making it static makes your project less customizable (for example you will not be able to start two web-servers with two different log-files in the same program). That's why what John Saunders suggested is usually considered better.


You can do this easily by passing a reference to the Logger instance you created in within MyProject to your instance of WebServer.

In your WebServer class, you can make a property (a getter/setter) of type Logger, so that WebServer knows about the Logger you want to use. Another alternative (probably better) is to add a parameter to the constructor of WebServer that takes the Logger. Pseudo code for adding the property:

public class WebServer
{
    public WebServer(Logger l)
    {
        Log = l;
    }

    //relevant code....
    public Logger Log { get; set; } //Short hand property declaration, see http://msdn.microsoft.com/en-us/library/bb384054.aspx
}

From inside the WebServer class, you can use the logger like Logger.Add("some text..."); Then, in your Main method, you can pass in your instance of Logger like so:

namespace MyProject  
{  
    class MyProject  
    {
        public static Logger Logs = new Logger();
        public static WebServer Web;

        static void Main()
        {  
            Web = new WebServer(Logs);
        }  
    }  
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜