StrangeIoC(四)

2019-12-02  本文已影响0人  虫小白

常用绑定:
1,绑定接口和类

injectionBinder.Bind<IWeapon>().To<PhaserGun>();

2,实例注入

IClass myInstance = injectionBinder.GetInstance<IClass>() as IClass;

3,单例注入

injectionBinder.Bind<ISocialService>().To<TwitterService>().ToSingleton();

4,名称注入

injectionBinder.Bind<ISocialService>() .To<TwitterService>().ToSingleton() .ToName(ServiceTypes.PRIMARY);

//使用
[Inject (ServiceTypes.PRIMARY)] //We mapped TwitterService to TERTIARY 
public ISocialService socialService{get;set;}

5,特定值注入

Configuration myConfig = loadConfiguration(); injectionBinder.Bind<IConfig>().ToValue(myConfig);

6,多接口注入

injectionBinder.Bind<IHittable>().Bind<IUpdateable>().To<Romulan>();

常用标签:
1,[Construct]:当存在多个构造函数时,使用此标签指定实例化时使用的构造函数
2,[PostConstruct]:使用此标签标记的函数在属性注入后立即被调用,使用[PostConstruct(1)]、[PostConstruct(2)]...可以指定函数执行顺序

上一篇 下一篇

猜你喜欢

热点阅读