news 2026/7/18 9:07:01

Win Application Framework (WAF)设计模式应用:工厂模式、单例模式与依赖注入

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Win Application Framework (WAF)设计模式应用:工厂模式、单例模式与依赖注入

Win Application Framework (WAF)设计模式应用:工厂模式、单例模式与依赖注入

【免费下载链接】wafWin Application Framework (WAF) is a lightweight Framework that helps you to create well structured XAML Applications.项目地址: https://gitcode.com/gh_mirrors/waf/waf

Win Application Framework (WAF) 是一个轻量级框架,专门用于创建结构良好的XAML应用程序(支持MAUI、WPF和WinUI)。这个框架通过巧妙应用设计模式,为开发者提供了构建可维护、可测试应用程序的完整解决方案。WAF框架的核心优势在于其精心设计的架构模式实现,特别是工厂模式、单例模式和依赖注入的应用,让开发者能够专注于业务逻辑而非基础设施代码。

🏗️ WAF框架架构概述

WAF采用分层架构,将应用程序逻辑清晰地分离为表示层、应用层和领域层。这种分离不仅提高了代码的可维护性,还使得设计模式的应用更加自然和高效。

核心架构组件:

  • 表示层:处理用户界面和交互
  • 应用层:协调业务逻辑和流程控制
  • 领域层:封装核心业务规则和实体

🏭 工厂模式在WAF中的精妙应用

工厂模式在WAF中广泛应用于ViewModel的创建和管理,特别是在需要延迟初始化或按需创建对象的场景中。

Lazy初始化工厂

在BookLibrary示例中,WAF使用Lazy<T>实现工厂模式,确保ViewModel只在需要时才被创建:

// 在ShellService中定义Lazy工厂 public Lazy<object>? LazyReportingView { get; set; } // 在ModuleController中注入Lazy工厂 private readonly Lazy<ShellViewModel> shellViewModel;

委托工厂方法

WAF还使用委托作为工厂方法,提供更灵活的创建方式:

// BookController中的工厂方法注入 private readonly Func<LendToViewModel> lendToViewModelFactory; public BookController(IMessageService messageService, IShellService shellService, IEntityService entityService, BookListViewModel bookListViewModel, BookViewModel bookViewModel, Func<LendToViewModel> lendToViewModelFactory) { this.lendToViewModelFactory = lendToViewModelFactory; } // 使用时通过工厂方法创建实例 private void LendTo(Book book) { var lendToViewModel = lendToViewModelFactory(); lendToViewModel.Book = book; // ... 其他初始化逻辑 }

这种设计使得:

  1. 依赖解耦:调用者不需要知道具体的创建细节
  2. 生命周期控制:可以灵活控制对象的创建时机
  3. 测试友好:可以轻松注入模拟工厂进行单元测试

👑 单例模式的最佳实践

WAF框架中,单例模式通过依赖注入容器(Autofac)优雅地实现,确保关键服务在整个应用程序生命周期中只有一个实例。

依赖注入容器配置

ApplicationsModule.cs中,WAF使用Autofac配置单例服务:

public class ApplicationsModule : Module { protected override void Load(ContainerBuilder builder) { // 注册为单例实例 builder.RegisterType<ShellService>() .As<IShellService>() .AsSelf() .SingleInstance(); builder.RegisterType<FileService>() .As<IFileService>() .AsSelf() .SingleInstance(); builder.RegisterType<MainViewModel>() .AsSelf() .SingleInstance(); } }

静态单例:ApplicationInfo

WAF还提供了静态单例模式的经典实现——ApplicationInfo类:

public static class ApplicationInfo { private static readonly Lazy<string> productName = new(GetProductName); private static readonly Lazy<string> version = new(GetVersion); public static string ProductName => productName.Value; public static string Version => version.Value; private static string GetProductName() { var entryAssembly = Assembly.GetEntryAssembly(); if (entryAssembly != null) { var attribute = (AssemblyProductAttribute?)Attribute.GetCustomAttribute( entryAssembly, typeof(AssemblyProductAttribute)); return attribute?.Product ?? ""; } return ""; } }

这种实现方式提供了:

  • 线程安全:使用Lazy<T>确保线程安全初始化
  • 延迟加载:只在第一次访问时创建实例
  • 全局访问:通过静态属性提供全局访问点

🔧 依赖注入:WAF的核心设计哲学

依赖注入是WAF框架的基石,它贯穿整个应用程序的各个层次,实现了高度的解耦和可测试性。

构造函数注入

WAF广泛使用构造函数注入来管理依赖关系:

// ModuleController中的依赖注入示例 public ModuleController(IMessageService messageService, IEntityController entityController, BookController bookController, PersonController personController, ShellService shellService, Lazy<ShellViewModel> shellViewModel) { this.messageService = messageService; this.entityController = entityController; this.bookController = bookController; this.personController = personController; this.shellService = shellService; this.shellViewModel = shellViewModel; }

接口隔离原则

WAF通过接口定义服务契约,实现依赖倒置:

// 服务接口定义 public interface IShellService { object? ShellView { get; set; } object? BookListView { get; set; } object? BookView { get; set; } // ... 其他属性 } // 具体实现 internal class ShellService : Model, IShellService { public object? ShellView { get; set; } public object? BookListView { get; set; } public object? BookView { get; set; } // ... 实现细节 }

控制反转容器

WAF使用Autofac作为控制反转容器,在ApplicationsModule中集中管理所有依赖:

// 注册服务和ViewModel builder.RegisterType<EntityService>() .As<IEntityService>() .AsSelf() .SingleInstance(); builder.RegisterType<BookListViewModel>() .AsSelf() .SingleInstance(); builder.RegisterType<LendToViewModel>() .AsSelf(); // 非单例,每次请求新实例

🎯 设计模式组合应用示例

场景:图书借阅功能

让我们通过BookLibrary示例中的借阅功能,看看WAF如何组合应用设计模式:

// 1. 工厂模式创建ViewModel private readonly Func<LendToViewModel> lendToViewModelFactory; // 2. 依赖注入获取所需服务 public BookController(IMessageService messageService, IShellService shellService, IEntityService entityService, BookListViewModel bookListViewModel, BookViewModel bookViewModel, Func<LendToViewModel> lendToViewModelFactory) { // 依赖注入 this.lendToViewModelFactory = lendToViewModelFactory; // ... 其他初始化 } // 3. 使用工厂创建实例 private void LendTo(Book book) { // 工厂方法创建ViewModel var lendToViewModel = lendToViewModelFactory(); // 配置ViewModel lendToViewModel.Book = book; lendToViewModel.Persons = entityService.Persons; // 单例服务 lendToViewModel.SelectedPerson = book.LendTo; // 显示对话框 if (lendToViewModel.ShowDialog(shellService.ShellView!)) { book.LendTo = lendToViewModel.SelectedPerson; } }

📊 设计模式优势对比

设计模式在WAF中的应用带来的优势
工厂模式ViewModel创建、延迟初始化解耦创建逻辑、支持延迟加载、提高性能
单例模式服务类、ApplicationInfo资源重用、全局状态管理、线程安全
依赖注入构造函数注入、接口隔离松耦合、可测试性、易于维护

🚀 实践建议:如何在你的项目中应用

1.定义清晰的接口

src/System.Waf/Samples/BookLibrary/BookLibrary.Library.Applications/Services/目录中学习如何定义服务接口。

2.使用依赖注入容器

参考ApplicationsModule.cs配置你的依赖注入容器。

3.合理使用单例

对于无状态服务或全局配置,使用单例模式。

4.工厂模式处理复杂创建

当对象创建逻辑复杂或需要延迟初始化时,使用工厂模式。

5.遵循WAF的架构模式

学习WAF的分层架构,将设计模式应用到合适的层次。

💡 总结

Win Application Framework通过精心设计的工厂模式、单例模式和依赖注入实现,为XAML应用程序开发提供了坚实的架构基础。这些设计模式的应用不仅提高了代码的可维护性和可测试性,还使得应用程序更加灵活和可扩展。

WAF框架的设计模式实现展示了如何在实际项目中优雅地应用这些经典模式,特别是在src/System.Waf/Samples/目录下的示例应用程序中,你可以找到更多设计模式的最佳实践。

无论你是WPF、MAUI还是WinUI开发者,学习和应用WAF框架中的设计模式思想,都将显著提升你的应用程序架构质量。通过工厂模式管理对象创建,单例模式控制全局资源,依赖注入实现松耦合,你可以构建出更加健壮、可维护的XAML应用程序。

【免费下载链接】wafWin Application Framework (WAF) is a lightweight Framework that helps you to create well structured XAML Applications.项目地址: https://gitcode.com/gh_mirrors/waf/waf

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/18 9:06:51

4D成像雷达直接处理原始张量实现3D占据栅格预测

1. 项目概述&#xff1a;为什么我们需要用4D成像雷达来做3D占据栅格预测&#xff1f; 如果你在自动驾驶或者机器人感知领域摸爬滚打过几年&#xff0c;一定对“3D占据栅格预测”这个概念不陌生。简单来说&#xff0c;它就是把我们周围的三维空间&#xff0c;像切豆腐一样划分成…

作者头像 李华
网站建设 2026/7/18 9:06:39

React Native Tabs社区贡献指南:如何参与项目开发与问题修复

React Native Tabs社区贡献指南&#xff1a;如何参与项目开发与问题修复 【免费下载链接】react-native-tabs React Native platform-independent tabs. Could be used for bottom tab bars as well as sectioned views (with tab buttons) 项目地址: https://gitcode.com/gh…

作者头像 李华
网站建设 2026/7/18 9:05:58

如何在qBittorrent中一键搜索20+种子站:search-plugins终极指南

如何在qBittorrent中一键搜索20种子站&#xff1a;search-plugins终极指南 【免费下载链接】search-plugins Search plugins for qBittorrent search feature 项目地址: https://gitcode.com/gh_mirrors/se/search-plugins 还在为寻找资源而烦恼吗&#xff1f;qBittorre…

作者头像 李华
网站建设 2026/7/18 9:05:04

HyperCube NFT铸造实战:低成本数字艺术创作的完整解决方案

HyperCube NFT铸造实战&#xff1a;低成本数字艺术创作的完整解决方案 【免费下载链接】hypercube HyperCube is a revolutionary, high-performance decentralized computing platform. HyperCube has powerful computing capabilities to provide high-performance computing…

作者头像 李华
网站建设 2026/7/18 9:03:24

Dev-C++新手入门:零基础C++开发环境配置与Hello World实战

1. 项目概述&#xff1a;为什么选择Dev-C作为你的第一个C开发环境&#xff1f; 如果你刚刚踏入C编程的大门&#xff0c;面对Visual Studio、VS Code、CLion这些功能强大但配置复杂的“庞然大物”&#xff0c;感到无从下手&#xff0c;那么这篇文章就是为你准备的。今天&#xf…

作者头像 李华
网站建设 2026/7/18 9:00:41

并查集与图算法:LeetCode连通性问题与拓扑排序模板终极指南

并查集与图算法&#xff1a;LeetCode连通性问题与拓扑排序模板终极指南 【免费下载链接】leetcode python 数据结构与算法 leetcode 算法题与书籍 刷算法全靠套路与总结&#xff01;Crack LeetCode, not only how, but also why. 项目地址: https://gitcode.com/gh_mirrors/l…

作者头像 李华