当前位置: 首页 > news >正文

Unity——VContainer的依赖注入

一、IOC控制反转和DI依赖倒置

1、IOC框架核心原理是依赖倒置原则

C#设计模式的六大原则

使用这种思想方式,可以让我们无需关心对象的生成方式,只需要告诉容器我需要的对象即可,而告诉容器我需要对象的方式就叫做DI(依赖注入)

 今天主要想研究一下DI(依赖注入),这里我选了VContainer

地址:https://github.com/hadashiA/VContainer

文档地址:https://vcontainer.hadashikick.jp/

 二、VContainer介绍

 由于我们使用的是Unity,而主要的Mono不支持构造函数。所以我们这里选择注入方式主要是特性注入和方法注入

  三、VContainer案例

(1)简单方法注入:

1、注册类型

public class GameLifetimeScope : LifetimeScope
{protected override void Configure(IContainerBuilder builder){builder.RegisterEntryPoint<ActorPresenter>();}
}

2、方法注入

这里实现接口 IStartableITickable。它是不依赖于Mono的接口,因此性能上更好,另一方面,IStartable与Mono的Start,ITickable与Mono的Update都是相同的。

public class ActorPresenter : IStartable,ITickable
{public void Start(){Debug.Log("Start ActorPresenter");}public void Tick(){Debug.Log("Update ActorPresenter");}
}

(2)基础MVC功能:

Model层

public class UIModel
{public void Hello() {Debug.Log("Hello World");}
}

View层

public class UIView : MonoBehaviour
{public Button button;
}

 Control层

public class UIControl : IStartable
{readonly UIModel _model;readonly UIView _view;public UIControl(UIModel model){this._model = model;}public UIControl(UIModel model, UIView view){this._model = model;this._view = view;}public void Start(){_view.button.onClick.AddListener(() => _model.Hello());}
}

通过这样做,我们成功地分离了领域控制/控制流/显示组件

在VContainter中,记得注册

public class GameLifetimeScope : LifetimeScope
{public UIView helloScreen;protected override void Configure(IContainerBuilder builder){builder.RegisterEntryPoint<UIControl>();builder.Register<UIModel>(Lifetime.Singleton);builder.RegisterComponent(helloScreen);}
}

(3)构造注入

        构造函数里,只需要写一个需要依赖注入的函数,成员变量里就可以随时获得对象。如下例子ClassB构造函数的参数是ClassA,我们的classA变量就可以随时使用

class ClassB : IStartable,ITickable
{readonly ClassA a;public ClassB(ClassA a){Debug.Log("ClassA构造函数注入");this.a = a;}public void Start(){a.Start();}public void Tick(){a.Update();}
}
class ClassA
{public ClassA(){Debug.Log("ClassA构造");}public void Start(){Debug.Log("Start");}public void Update() {Debug.Log("Update");}
}
public class GameLifetimeScope : LifetimeScope
{//public UIView helloScreen;protected override void Configure(IContainerBuilder builder){builder.RegisterEntryPoint<ClassB>();builder.Register<ClassA>(Lifetime.Singleton);}
}
(4)方法注入(其他的和上边一样)
class ClassB : IStartable,ITickable
{private ClassA a;[Inject]public void GetClassA(ClassA a) {Debug.Log("方法注入");this.a = a;}public void Start(){a.Start();}public void Tick(){a.Update();}
}
(5)字段/属性注入
class ClassB : IStartable,ITickable
{[Inject]private ClassA a;public void Start(){a.Start();}public void Tick(){a.Update();}
}

http://www.lryc.cn/news/281386.html

相关文章:

  • 【面试突击】Spring 面试实战
  • 【Linux】Ubuntu 22.04 上安装最新版 Nextcloud Hub 7 (28.0.1)
  • PHP项目如何自动化测试
  • WEB 3D技术 three.js 3D贺卡(1) 搭建基本项目环境
  • 短视频IP运营流程架构SOP模板PPT
  • python爬虫之线程与多进程知识点记录
  • 基于Java (spring-boot)的停车场管理系统
  • 微软Office 2019 批量授权版
  • ChatGLM2-6B 大语言模型本地搭建
  • WindowsServer安装mysql最新版
  • gin切片表单验证
  • openssl3.2 - 官方demo学习 - certs
  • Datawhale 大模型基础理论 Day1 引言
  • HarmonyOS应用开发学习笔记 UIAbility组件与UI的数据同步 EventHub、globalThis
  • leetcode每日一题44
  • idea写sql语句快捷键提醒,mapper注解开发,mybatis
  • 002 Golang-channel-practice
  • MFC为对话框资源添加类
  • SpringBoot新手入门完整教程和项目示例
  • PHP留言板实现
  • ssm+vue的物流配送人员车辆调度管理系统的设计与实现(有报告)。Javaee项目,ssm vue前后端分离项项目。
  • day1·算法-双指针
  • 在vue中,切换页面之后如何关闭定时器
  • 观测云产品更新 | 日志、场景仪表板、监控器等
  • 【JupyterLab】在 conda 虚拟环境中 JupyterLab 的安装与使用
  • HTML--JavaScript--引入方式
  • 第28关 k8s监控实战之Prometheus(七)
  • SSC | Blue Prism报告:2024年智能自动化(IA)7大趋势预测
  • el-tree定义左边箭头,包括下级出现连线
  • C++ 多线程顺序打印