初始化类实例 [英] Initialising class instance

查看:71
本文介绍了初始化类实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我正在使用反转控制的概念和依赖注入模式。

因为我使用的是构造函数注入。我已经完成了这个级别但现在问题是创建类的实例。当我创建类的实例时,它要求参数并说构造函数不能有0个参数。我在构造函数注入中使用接口作为参数。如何我可以创建一个类的实例,如果我需要任何类型的IoC容器,请告诉我它在项目中的实现方式。

解决方案

所以你创建了一个使用公共构造函数的类将您注入的对象作为参数 - 如果您还没有提供没有参数的默认构造函数(如果您使用DI,则大多数情况下不应该这样做),您无法使用实例注入的对象类型 - 即DI的意义。你是否真的理解这个概念?如果你真的需要构造一个没有注入对象的实例,你可以提供一个默认的构造函数,并在这种情况下用一个属性做DI,但是以一种可以处理注入对象/接口上的空引用的方式编写你的类。



示例:如果要注入记录器并提供不注入它的方法(通过调用默认构造函数),则必须检查记录器上的空引用-object每次你想在课堂上使用它...



虽然SA一般是正确的(如果你显示一些代码会有帮助)它显然是你的做错了(没事 - 你只是没有使用DI)



所以这是一个例子我的意思是:



 命名空间 DependencyInjection 
{
class 程序
{
静态 void Main( string [] args)
{
var a = new CompilerWillWriteDefaultConstructor();
// 不会编译 - 没有默认构造函数
// var b = new ClassWithOwnConstructorCompilerWontWriteDefaultConstructor();
var interfaceimplementation = new ImplementsInterface();
// 为每个构造函数注入的类注入接口
var b = new ClassWithOwnConstructorCompilerWontWriteDefaultConstructor(interfaceimplementation);
}
}

// 您要注入的某个界面
interface InterfaceToInject {};
// 实现此接口的具体类
class ImplementsInterface:InterfaceToInject {}

// 班级你想将接口依赖注入
class ClassWithOwnConstructorCompilerWontWriteDefaultConstructor
{
public ClassWithOwnConstructorCompilerWontWriteDefaultConstructor(InterfaceToInject obj)
{
}
}

// 其他一些类 - 编译器将编写构造函数
class CompilerWillWriteDefaultConstructor {}
}


Hello friends,
I am using the concept of inversion control with the patten of dependency injection.
In that i am using Constructor Injection.I had done upto this level but now the problem is creating an instance of the class.When i create an instance of the class its asking for the arguments and says "Constructor cannot have 0 arguments".I am using an interface as an argument in the constructor injection.How can i create an instance of the class and if i need any kind of IoC container so please tell me its way of implementing in the project.

解决方案

So you created a class with public constructor taking your injected object as parameter - if you didn't also provide a default constructor with no arguments (which most times you shouldn't if you are using DI) you can't instanciate your class with out an instance of the injected object type - thats the sense of DI. Are you shure you understand the concept?. If you really need to construct an instance without the injected object you could provide a default constructor and and do DI in this case with a property, but write your class in a way that it can handle null-references on the injected object/Interface.

Example: if you want to inject a logger and provide a way to not inject it (by calling a default constructor) you have to check for null reference on the logger-object every time you want to use it inside the class...

Although SA is correct in general (would help if you show some code) it's obviouse what you do wrong (nothing - you just didn't use DI as you should)

So here is an example what I mean:

namespace DependencyInjection
{
    class Program
    {
        static void Main(string[] args)
        {
            var a = new CompilerWillWriteDefaultConstructor();
            // wont compile - no default constructor
            // var b = new ClassWithOwnConstructorCompilerWontWriteDefaultConstructor();
            var interfaceimplementation = new ImplementsInterface();
            // Inject interface to class per constructor injection
            var b = new ClassWithOwnConstructorCompilerWontWriteDefaultConstructor(interfaceimplementation);
        }
    }

    // some interface you want to inject
    interface  InterfaceToInject { };
    // a concrete class implementing this interface
    class ImplementsInterface : InterfaceToInject { }

    // the class you want to inject the interface dependency to
    class ClassWithOwnConstructorCompilerWontWriteDefaultConstructor
    {
        public ClassWithOwnConstructorCompilerWontWriteDefaultConstructor(InterfaceToInject obj)
        {
        }
    }

    // some other class - compiler will write constructor
    class CompilerWillWriteDefaultConstructor{}
}


这篇关于初始化类实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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