依赖注入框架的C#兑现(连载之二)赤裸裸的实现
依赖注入框架的C#实现(连载之二)赤裸裸的实现
原贴不能发了,只好重起一个。
完整代码
1。 依赖关系就是使用关系。
2。依赖关系的解耦,需要用接口查找实现,字典是最好的工具。
3. 以下就是赤裸裸的实现,不堪如目,但是简单好理解。最主要是可以让测试通过!!!
先放接口,便于理解:
这是实现:
然后测试也要作出相应的修改。
------解决方案--------------------
好啊 非常好好
------解决方案--------------------
------解决方案--------------------
继续不懂。。
原贴不能发了,只好重起一个。
完整代码
1。 依赖关系就是使用关系。
2。依赖关系的解耦,需要用接口查找实现,字典是最好的工具。
3. 以下就是赤裸裸的实现,不堪如目,但是简单好理解。最主要是可以让测试通过!!!
先放接口,便于理解:
- C# code
namespace Skight.LightWeb.Domain { public interface Resolver { Dependency get<Dependency>(); } }
这是实现:
- C# code
using System; using System.Collections.Generic; namespace Skight.LightWeb.Domain { public class ResolverImpl:Resolver { private readonly IDictionary<Type, object> item_resolvers; public ResolverImpl(IDictionary<Type, object> itemResolvers) { item_resolvers = itemResolvers; } public Dependency get<Dependency>() { return (Dependency) item_resolvers[typeof (Dependency)]; } }
然后测试也要作出相应的修改。
- C# code
using System; using System.Collections.Generic; using Machine.Specifications; namespace Skight.LightWeb.Domain.Specs { public class ResolverSpecs { private Establish context = () => { var dictioary = new Dictionary<Type, object>(); dictioary.Add(typeof (MockInterface), new MockImplementaion()); subject = new ResolverImpl(dictioary); }; private It Container_get_by_interface_should_return_its_implementation_class = () => subject.get<MockInterface>().ShouldBeOfType<MockImplementaion>(); private static ResolverImpl subject; private interface MockInterface { } private class MockImplementaion : MockInterface { } } }
------解决方案--------------------
好啊 非常好好
------解决方案--------------------
------解决方案--------------------
继续不懂。。