Click here to Skip to main content
15,896,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
var _webService = New WebServiceHost(GetType(MyWebServiceImpl), New Uri("http://localhost:8000"));


What I have tried:

I need to inject a DB connection and a logger into the object of class MyWebServiceImpl, but I don't have a reference to it, because this object is instanciated by the WebServiceHost constructor.

How can I get a reference to the MyWebServiceImpl object? Or is there some pattern I can use to get it?

Thanks
Posted
Updated 30-Sep-16 3:59am
v2
Comments
David_Wimbley 30-Sep-16 9:38am    
Do you have access to the source of MyWebServiceImpl? Why not just alter it to get what you need out of it.
AlessandroF73 30-Sep-16 9:51am    
That's what I've done to get around the problem. But in this way the MyWebServiceImpl object must know some external class, and I would like to avoid it. Thank you


1 solution

Ok based on your comments I think i understand now.

I think what you need to look into is Dependency injection. For example, Ninject library. There are others but i prefer ninject.

A beginner article: An Absolute Beginner's Tutorial on Dependency Inversion Principle, Inversion of Control and Dependency Injection[^]

The gist of it is what you are saying. Your class (MyWebServiceImpl) has to know that it is receiving the Log class. Instead what you would do is create an interface called ILog and have the class MyWebServiceImpl receive ILog as a param.

In doing that, your class can use any class that inherits from ILog for logging. You could have a DB Logger called DBLog, a File logger called Log, or a console output logger called ConsoleLog, but as long as the class inherits from ILog, your MyWebServiceImpl doesn't have to know the specific concrete class it is using.

You can use manual dependency injection in a way such as this


C#
public interface ILog
{
    void Log(string msg);
}

public class SqlLog : ILog
{
    public void Log(string msg)
    {
       //Write to sql.
    }
}

public class FileLog : ILog
{
    public void Log(string msg)
    {
        //Write log to file
    }
}


Then in your class MyWebServiceImpl you would do something like

C#
public class MyWebServiceImpl
{
     private readonly ILog _log = null;
     public MyWebServiceImpl(ILog logger)
     {
        this._log = logger;
     }
}



This way, your class MyWebServiceImpl isn't dependent upon only using SqlLog or FileLog...it can use either. The usage would be something like

C#
ILog logger = new SqlLog();
var srv = new MyWebServiceImpl(logger);

//Then you can also do

ILog filelogger = new FileLog();
var srv1 = new MyWebServiceImpl(filelogger);


And not your class isn't dependent upon any one logger type due to the manual dependency injection being done by using ILog.

I do encourage you to look into ninject though.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900