在没有第三方的情况下实施IOC和DI [英] Implement IOC and DI without third Party

查看:61
本文介绍了在没有第三方的情况下实施IOC和DI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在研究IOC和DI的概念并试图实现它。有许多第三方工具,如AutoFac,Ninject,UnitCOntroller等。但是可以在不使用任何第三方工具的情况下实现IOC和DI。

解决方案

请阅读这个

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


请看这里:

http://joelabrahamsson.com/inversion-of-control-an-introduction-with-examples-in-net/

使用依赖注入和服务定位器模式介绍控制反转,使用C#中的简单示例。



这个网站也有一些关于IoC& amp; DI:

依赖性倒置原则,IoC容器和依赖注入(第1部分) [ ^ ]

依赖倒置原则,IoC Container&依赖注入(第2部分)

依赖性倒置原则,IoC容器和依赖注入(第3部分)

依赖倒置原理,IoC容器和依赖注入(第4部分)



下面是一个很好的视频,它展示了控制的反转以及它与DI(依赖注入)的不同之处:

https://www.facebook.com/photo.php?v=690253231015623&set=vb.341019362605680&type=2&theater

这里有更多例子:

https:// www。 facebook.com/DotNetInterviewQuestions/videos?f ref = photo



我希望这能帮到你))

祝你好运!


我刚刚实现了我的自定义IController方法。然后我在Application_Start中使用了该类。



公共类ControllerFactory:DefaultControllerFactory

{

public override IController CreateController(RequestContext requestContext,string controllerName)

{

IController controllerObject = null;



类型controllerType = GetTypeForController (controllerName);

if(controllerType!= null)

{

IEmployee employee = new EmployeeService((IRepository)new Repository()) ;

controllerObject =(IController)Activator.CreateInstance(controllerType,employee);

}

返回controllerObject;

}





公共覆盖void ReleaseController(IController控制器)

{

var disposable = controll呃作为IDisposable;

if(一次性!= null)

{

disposable.Dispose();

}
}



private static类型GetTypeForController(字符串controllerName)

{

string fullTypeName;

类型type = null;



var ioCDictionary = new Dictionary();

ioCDictionary .Add(Employee,HRApplication.Controllers.EmployeeController,HRApplication);



if(ioCDictionary.TryGetValue(controllerName,out fullTypeName))

{

type = Type.GetType(fullTypeName);

}

返回类型;

}

}



然后在app开始我们需要这个代码

ControllerBuilder.Current.SetControllerFactory( typeof运算(CONTRO llerFactory));

Hi,

I was studying the concept of IOC and DI and tried to implement it. There are many third party tools like AutoFac,Ninject,UnitCOntroller etc. But is it possible to implement IOC and DI without using any those third party tool.

解决方案

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


Please look here :
http://joelabrahamsson.com/inversion-of-control-an-introduction-with-examples-in-net/
an introduction to Inversion of Control, using the Dependency Injection and Service Locator patterns, with simple examples in C#.

This site also has some interesting articles about the IoC & DI:
Dependency Inversion Principle, IoC Container, and Dependency Injection (Part - 1)[^]
Dependency Inversion Principle, IoC Container & Dependency Injection (Part - 2)
Dependency Inversion Principle, IoC Container, and Dependency Injection (Part - 3)
Dependency Inversion Principle, IoC Container, and Dependency Injection (Part - 4)

Below is a nice video which demonstrates Inversion of control and how its is different from DI (Dependency Injection):
https://www.facebook.com/photo.php?v=690253231015623&set=vb.341019362605680&type=2&theater
And here look more examples:
https://www.facebook.com/DotNetInterviewQuestions/videos?fref=photo

I hope this will help you))
Good Luck!


I just implemented my custom IController methods. And then I used that class in Application_Start.

public class ControllerFactory : DefaultControllerFactory
{
public override IController CreateController(RequestContext requestContext, string controllerName)
{
IController controllerObject = null;

Type controllerType = GetTypeForController(controllerName);
if (controllerType != null)
{
IEmployee employee = new EmployeeService((IRepository) new Repository());
controllerObject = (IController)Activator.CreateInstance(controllerType, employee);
}
return controllerObject;
}


public override void ReleaseController(IController controller)
{
var disposable = controller as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}

private static Type GetTypeForController(string controllerName)
{
string fullTypeName;
Type type = null;

var ioCDictionary = new Dictionary();
ioCDictionary.Add("Employee", "HRApplication.Controllers.EmployeeController,HRApplication");

if (ioCDictionary.TryGetValue(controllerName, out fullTypeName))
{
type = Type.GetType(fullTypeName);
}
return type;
}
}

Then in app start we need to have this code
ControllerBuilder.Current.SetControllerFactory(typeof(ControllerFactory));


这篇关于在没有第三方的情况下实施IOC和DI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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