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

📅 2026/7/18 9:07:02
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/wafWin Application Framework (WAF) 是一个轻量级框架专门用于创建结构良好的XAML应用程序支持MAUI、WPF和WinUI。这个框架通过巧妙应用设计模式为开发者提供了构建可维护、可测试应用程序的完整解决方案。WAF框架的核心优势在于其精心设计的架构模式实现特别是工厂模式、单例模式和依赖注入的应用让开发者能够专注于业务逻辑而非基础设施代码。️ WAF框架架构概述WAF采用分层架构将应用程序逻辑清晰地分离为表示层、应用层和领域层。这种分离不仅提高了代码的可维护性还使得设计模式的应用更加自然和高效。核心架构组件表示层处理用户界面和交互应用层协调业务逻辑和流程控制领域层封装核心业务规则和实体 工厂模式在WAF中的精妙应用工厂模式在WAF中广泛应用于ViewModel的创建和管理特别是在需要延迟初始化或按需创建对象的场景中。Lazy初始化工厂在BookLibrary示例中WAF使用LazyT实现工厂模式确保ViewModel只在需要时才被创建// 在ShellService中定义Lazy工厂 public Lazyobject? LazyReportingView { get; set; } // 在ModuleController中注入Lazy工厂 private readonly LazyShellViewModel shellViewModel;委托工厂方法WAF还使用委托作为工厂方法提供更灵活的创建方式// BookController中的工厂方法注入 private readonly FuncLendToViewModel lendToViewModelFactory; public BookController(IMessageService messageService, IShellService shellService, IEntityService entityService, BookListViewModel bookListViewModel, BookViewModel bookViewModel, FuncLendToViewModel lendToViewModelFactory) { this.lendToViewModelFactory lendToViewModelFactory; } // 使用时通过工厂方法创建实例 private void LendTo(Book book) { var lendToViewModel lendToViewModelFactory(); lendToViewModel.Book book; // ... 其他初始化逻辑 }这种设计使得依赖解耦调用者不需要知道具体的创建细节生命周期控制可以灵活控制对象的创建时机测试友好可以轻松注入模拟工厂进行单元测试 单例模式的最佳实践WAF框架中单例模式通过依赖注入容器Autofac优雅地实现确保关键服务在整个应用程序生命周期中只有一个实例。依赖注入容器配置在ApplicationsModule.cs中WAF使用Autofac配置单例服务public class ApplicationsModule : Module { protected override void Load(ContainerBuilder builder) { // 注册为单例实例 builder.RegisterTypeShellService() .AsIShellService() .AsSelf() .SingleInstance(); builder.RegisterTypeFileService() .AsIFileService() .AsSelf() .SingleInstance(); builder.RegisterTypeMainViewModel() .AsSelf() .SingleInstance(); } }静态单例ApplicationInfoWAF还提供了静态单例模式的经典实现——ApplicationInfo类public static class ApplicationInfo { private static readonly Lazystring productName new(GetProductName); private static readonly Lazystring 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 ; } }这种实现方式提供了线程安全使用LazyT确保线程安全初始化延迟加载只在第一次访问时创建实例全局访问通过静态属性提供全局访问点 依赖注入WAF的核心设计哲学依赖注入是WAF框架的基石它贯穿整个应用程序的各个层次实现了高度的解耦和可测试性。构造函数注入WAF广泛使用构造函数注入来管理依赖关系// ModuleController中的依赖注入示例 public ModuleController(IMessageService messageService, IEntityController entityController, BookController bookController, PersonController personController, ShellService shellService, LazyShellViewModel 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.RegisterTypeEntityService() .AsIEntityService() .AsSelf() .SingleInstance(); builder.RegisterTypeBookListViewModel() .AsSelf() .SingleInstance(); builder.RegisterTypeLendToViewModel() .AsSelf(); // 非单例每次请求新实例 设计模式组合应用示例场景图书借阅功能让我们通过BookLibrary示例中的借阅功能看看WAF如何组合应用设计模式// 1. 工厂模式创建ViewModel private readonly FuncLendToViewModel lendToViewModelFactory; // 2. 依赖注入获取所需服务 public BookController(IMessageService messageService, IShellService shellService, IEntityService entityService, BookListViewModel bookListViewModel, BookViewModel bookViewModel, FuncLendToViewModel 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),仅供参考