如何在依赖注入中传递层之间的对象 [英] How to pass object between layers in dependency injection

查看:82
本文介绍了如何在依赖注入中传递层之间的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务。它调用一大堆控制器,然后又调用一大堆DAO。这些DAO创建了很多类实例,用于在那里工作。

我的问题是在尝试实现DI我在哪里创建这些对象



现在我在哪里移动创建所有obj类型对象,类似于DAO和Controller。

谢谢我一直很难用这个



只是为了澄清我理解我必须实现接口并在那些接口中传递对象。但在这种情况下,什么将充当我的容器。我将在哪里创建新对象。





I have a service .Which call a host of controllers and that in turn call a host of DAOs.These DAos have a lot of class instance created that they use to do there job.
My problem is while trying to implement DI where do i create these objects

Now where do i move creation of all obj types object and similarly for DAOs and Controller.
Thanks I have been realy having a hard time with this

Just to clarify I understand i have to implement Interface and pass object around in those interface.But what will act as my container in this scenario.Where will i do new object creation.


class ServiceDemo{
conlroller1= new controller1();
conlroller2= new controller2();
}
class controller1{
DAO1= new DAO1();
DAO2= new DAO2();
DAO3= new DAO3();
DAO4= new DAO4();
}
class DAO1{
obj1= new obj1();
obj2= new obj2();
obj3= new obj3();
obj4= new obj4();
obj5= new obj5();
}

推荐答案

请查看以下文章:



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



这将让您了解DP的内容。



基本上DP首先定义一个所谓的接口,

定义一个类必须实现的方法。



一旦定义了接口你可以编写实现这个接口的类。



例如一个用于模拟数据库访问的假类

和另一个用于实际访问的假类。
Please have a look at the following article:

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

This will allow you to learn what DP is about.

Basically DP starts by defining a so-called interface which
defines which methods a class must implement.

Once you have defined an interface you can write classes that implement this interface.

e.g. one fake class for simulating database access
and another one for real access.


这篇关于如何在依赖注入中传递层之间的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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