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; // ... 其他初始化逻辑 }这种设计使得:
- 依赖解耦:调用者不需要知道具体的创建细节
- 生命周期控制:可以灵活控制对象的创建时机
- 测试友好:可以轻松注入模拟工厂进行单元测试
👑 单例模式的最佳实践
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),仅供参考