简述控制反转ioc,什么是控制反转(IoC)?

Can someone guide me to some basic tutorials on inversion of control (IoC)? (preferably .net/c#).

I need some hands on code to wrap my head around it :)

解决方案

IOC, or inversion of control, is, in most apps, inverting the route through the code from UI to database. This si not a complete answer, but it is one of the easiest ways to get your head around the concept.

If you want to learn IOC, get into TDD, as you will find setting up tests a lot easier once you invert.

Example:

Typical flow of most .NET apps I have seen is something like this:

UserCollection col = BusinessLayer.Class.GetLoggedInUsers();

//Business logic

return col;

then business is like this:

UserTable table = DataLayer.Class.GetLoggedInUsers();

return table;

etc. This is all pseudocode. To use IOC in this example, you add an interface for the data layer class, like IUserRepository. You can use generics, and I would recommend underneath the hood.

You can then do something like this:

IUserRepository repository = SetUpRepository();

UserCollection col = BusinessLayer.Class.GetUsers(repository);

Why is this important? For testing, you can create a mock repository and feed it to the business class. The mock contains data that is always the same, which means you are exercising your code, not testing end to end.

If you want C#, here is a basic example on weblogs.asp.net :

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值