如何将一些外部行为注入通过webservicehost实例化的对象? [英] How can I inject some external behaviour into an object instantiated via webservicehost?

查看:55
本文介绍了如何将一些外部行为注入通过webservicehost实例化的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var _webService = New WebServiceHost(GetType(MyWebServiceImpl), New Uri("http://localhost:8000"));





我尝试了什么:



我需要将一个数据库连接和一个记录器注入MyWebServiceImpl类的对象,但我不知道没有对它的引用,因为这个对象是由WebServiceHost构造函数实现的。



如何获取对MyWebServiceImpl对象的引用?或者是否有一些我可以使用的模式来获取它?



谢谢



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

推荐答案

Ok基于你的评论我想我现在明白了。



我认为你需要研究的是依赖注入。例如,Ninject库。还有其他但我更喜欢ninject。



一篇初学文章:绝对初学者关于依赖倒置原理,控制和依赖注入反转的教程 [ ^ ]



你要说的就是它的要点。您的类(MyWebServiceImpl)必须知道它正在接收Log类。相反,你要做的是创建一个名为ILog的接口,并让类MyWebServiceImpl接收ILog作为参数。



这样做,你的类可以使用任何继承的类来自ILog的日志记录。您可以拥有一个名为DBLog的DB Logger,一个名为Log的文件记录器,或一个名为ConsoleLog的控制台输出记录器,但只要该类继承自ILog,您的MyWebServiceImpl就不必知道它正在使用的具体具体类。 br />


你可以用这种方式使用手动依赖注入





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


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
    }
}





然后在你的班级MyWebServiceImpl中你会做类似的事情





Then in your class MyWebServiceImpl you would do something like

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







这样,您的类MyWebServiceImpl不是仅依赖于使用SqlLog或FileLog ...它可以使用任何一个。用法类似于






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

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

//Then you can also do

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





并非你的类不依赖于任何一种记录器类型,因为使用ILog手动依赖注入。



我鼓励你研究ninject。



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.


这篇关于如何将一些外部行为注入通过webservicehost实例化的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆