.net8 net8webapi中 KeyedService 和Wpf 的 prism 框架 KeyedService 的比较

.net8webapi:

    public interface ITestKeyService
    {
        public int Id { get; set; }
        public string Name { get; set; } 
    }
    public class Test1KeyService : ITestKeyService
    {
        public int Id { get; set; } = 1;
        public string Name { get; set; } = "Name1";
    }
    public class Test2KeyService : ITestKeyService
    {
        public int Id { get; set; } = 2;
        public string Name { get; set; } = "Name2";
    }
builder.Services.AddKeyedSingleton<ITestKeyService, Test1KeyService>("ServiceA");
builder.Services.AddKeyedSingleton<ITestKeyService, Test2KeyService>("ServiceB");
    [Route("api/private/v1/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class TestKeyController : ControllerBase
    {
        private readonly ITestKeyService service1;
        private readonly ITestKeyService service2;

        public TestKeyController([FromKeyedServices("ServiceA")] ITestKeyService service1, [FromKeyedServices("ServiceB")] ITestKeyService service2)
        {
            this.service1 = service1;
            this.service2 = service2;
        }


        [HttpGet]
        public ActionResult<Dictionary<string, string>> MyTestKey()
        {
            Dictionary<string, string> dic = new();
            dic["服务1的名字"] = service1.Name;
            dic["服务2的名字"] = service2.Name;
            return dic;
        }
    }

结果:

在这里插入图片描述

在prism 8.0/9.0 中 :

   public partial class App : PrismApplication
   {
       protected override Window CreateShell() => null;

       protected override void OnInitialized()
       {
           //从容器当中获取MainView的实例对象
           var container = ContainerLocator.Container;
           var shell = container.Resolve<object>("MainView");
           if (shell is Window view)
           {
               //更新Prism注册区域信息
               var regionManager = container.Resolve<IRegionManager>();
               RegionManager.SetRegionManager(view, regionManager);
               RegionManager.UpdateRegions();

               //调用首页的INavigationAware 接口做一个初始化操作
               if (view.DataContext is INavigationAware navigationAware)
               {
                   navigationAware.OnNavigatedTo(null);
                   //呈现首页
                   App.Current.MainWindow = view;
               }
           }
           base.OnInitialized();
       }

       protected override void RegisterTypes(IContainerRegistry services)
       {

           //模板匹配服务 
           services.Register<ITemplateMatchService, ShapeModelService>(nameof(TempalteMatchType.ShapeModel));
           services.Register<ITemplateMatchService, NccModelService>(nameof(TempalteMatchType.NccModel));
           services.Register<ITemplateMatchService, LocalDeformableService>(nameof(TempalteMatchType.LocalDeformable));

       }

       protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
       {
           moduleCatalog.AddModule<TemplateMatchModule>();
           base.ConfigureModuleCatalog(moduleCatalog);
       }
   }

取出:

    internal class NccViewModel : NavigationViewModel
    {
        public ITemplateMatchService MatchService { get; set; }

        public NccViewModel()
        {
            MatchService = ContainerLocator.Current.Resolve<ITemplateMatchService>(nameof(TempalteMatchType.NccModel));
             
        }
     }

9.0 Prism的注册方法:

using DryIoc.Microsoft.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using net8wpf.Cad;
using net8wpf.Cad.Utils;
using net8wpf.Device;
using net8Wpf.Core.Data;
using net8Wpf.Core.Data.Yanji;
using net8Wpf.Core.Data.Yanji.Interface;
using net8Wpf.MyLiveChart;
using net8Wpf.MyLiveChart.Service;
using net8Wpf.Service;
using net8Wpf.Share.Communication.MyPlc.MySiemens;
using net8Wpf.Share.Models;
using net8Wpf.Share.RestSharp;
using net8Wpf.ViewModels;
using net8Wpf.Views;
using RestSharp;
using Serilog;
using Serilog.Formatting.Json;
using System.Windows;

namespace net8Wpf
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell() => null;

        protected override IContainerExtension CreateContainerExtension()
        {
            IServiceCollection serviceCollection = new ServiceCollection();
            serviceCollection.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
            serviceCollection.AddLogging(logbuilder =>
            {
                Log.Logger = new LoggerConfiguration()
                    .MinimumLevel.Debug()
                    .Enrich.FromLogContext()
                    .WriteTo.Console(new JsonFormatter())
                    .WriteTo.File(
                        new JsonFormatter(),
                        @"D:\serialLog\log-.txt",
                        rollingInterval: RollingInterval.Day
                    )
                    .CreateLogger();
                logbuilder.AddSerilog();
            });

            //var a = SerilogServiceCollectionExtensions.AddSerilog(serviceCollection);
            return new DryIocContainerExtension(
                new Container(CreateContainerRules()).WithDependencyInjectionAdapter(
                    serviceCollection
                )
            );
        }

        protected override void RegisterTypes(IContainerRegistry services)
        {
            //services.RegisterAssembly(Assembly.GetExecutingAssembly());
            services.RegisterSingleton<MyData>();
            services.RegisterSingleton<ApiData>();
            services.RegisterSingleton<SelfData>();

            services.RegisterSingleton<ChartService>();

            services.RegisterSingleton<IS7ConnService, S7ConnService>();
            services.RegisterSingleton<IDeviceService, DeviceServiceImpl>();
            services.RegisterSingleton<IAddGrahService, AddGrahService>();
            services.RegisterSingleton<RestClient>(sp => new RestClient("http://localhost:8889"));
            //services.Register<MyData>();


            services.RegisterForNavigation<MainView, MainViewModel>();
            services.RegisterForNavigation<SettingView, SettingViewModel>();
            services.RegisterSingleton<INavigationMenuService, NavigationMenuService>();
            services.RegisterForNavigation<DashboardView, DashboardViewModel>();

            services
                .GetContainer()
                .Register<HttpRestClient>(made: Parameters.Of.Type<string>(serviceKey: "webUrl"));
            services
                .GetContainer()
                .RegisterInstance(@"http://localhost:8889/", serviceKey: "webUrl");
        }

        protected override void OnInitialized()
        {
            var container = ContainerLocator.Container;
            var shell = container.Resolve<object>("MainView");
            if (shell is Window view)
            {
                var regionManager = container.Resolve<IRegionManager>();
                // 将 RegionManager 实例关联到 myContentControl 控件
                RegionManager.SetRegionManager(view, regionManager);
                //强制更新所有已注册的区域(Regions)
                RegionManager.UpdateRegions();
                //检查view的数据上下文(DataContext)是否实现了INavigationAware接口。如果是,则调用OnNavigatedTo方法,并设置应用程序的主窗口为view
                if (view.DataContext is INavigationAware navigationAware)
                {
                    navigationAware.OnNavigatedTo(null);
                    App.Current.MainWindow = view;
                }
            }
            base.OnInitialized();
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            moduleCatalog.AddModule<MyLiveChartModule>();
            moduleCatalog.AddModule<DeviceModule>();
            moduleCatalog.AddModule<MyCadModule>();
            base.ConfigureModuleCatalog(moduleCatalog);
        }
    }
}

异步编程及其取消:

            CancellationTokenSource cts = new();
            try
            {
                var a = await helper
                    .Db.Queryable<Circle2Impl>()
                    .ToListAsync(cts.Token)
                    .WaitAsync(TimeSpan.FromSeconds(2));
            }
            catch (TimeoutException ex)
            {
                cts.Cancel();
            }
            finally
            {
                cts.Cancel();
            }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: WPF Prism是一个用于开发灵活、可扩展和可测试的WPF应用程序的框架。在WPF Prism,权限框架起着重要的作用,用于控制用户在应用程序的访问权限。 权限框架的目的是确保用户只能访问他们被授权的功能和数据。在WPF Prism,权限框架通过将权限信息与模块和视图进行关联来实现这一目标。权限信息可以定义为角色、用户、权限等。 在WPF Prism,可以使用属性和方法来定义权限信息。通过为模块和视图添加属性,可以指定这些模块和视图所需的权限。然后,可以使用权限框架来检查当前用户是否具有执行这些模块和视图的权限。如果用户没有所需的权限,可以采取相应的行动,例如禁用按钮、隐藏视图等。 WPF Prism的权限框架还允许对用户进行身份验证,以确保只有经过身份验证的用户才能登录和使用应用程序。身份验证是通过使用用户名和密码等凭据进行的,以验证用户的身份。一旦用户通过身份验证,权限框架可以根据用户的角色和权限确定其可访问的功能和数据。 另外,WPF Prism的权限框架还支持动态权限管理。这意味着可以根据特定条件或用户角色的变化来动态地更新和调整用户的权限。例如,当用户升级到更高级别的用户角色时,可以动态地添加或删除其相关的权限。 总而言之,WPF Prism的权限框架为开发人员提供了一种灵活且可扩展的方式来管理用户的访问权限。通过使用权限框架,可以确保只有经过授权的用户才能访问所需的功能和数据,提高应用程序的安全性和可用性。 ### 回答2: WPF Prism 是一个用于开发模块化、可扩展的 WPF 应用程序的框架。它提供了一种在应用程序实现权限管理的方法。 在 WPF Prism ,权限框架是通过使用模块化的方式实现的。每个模块都可以定义自己的权限,然后在应用程序根据用户的角色或权限来动态加载和展示模块。 实现权限框架的方法可以有很多种,以下是一种常用的做法: 1. 在应用程序启动时,加载用户信息并确定用户的角色或权限。 2. 在每个模块的初始化过程,根据用户的角色或权限来判断是否需要加载该模块。 3. 在加载模块时,可以使用 Prism 提供的导航服务来控制模块的访问权限。通过配置导航菜单和页面权限,实现页面的动态加载和展示。 4. 可以使用身份验证服务来验证用户的身份和权限。可以自定义身份验证服务,根据具体的业务规则来判断用户是否有权限进行某个操作。 5. 在界面上可以通过使用可见性绑定和命令绑定来控制用户对不同控件或操作的权限。根据用户的角色或权限来控制控件的可见性和是否启用某个操作。 总之,使用 WPF Prism 框架可以很轻松地实现一个灵活的权限框架。开发人员可以定义各个模块的权限,根据用户的角色或权限来动态加载和展示模块,同时可以使用身份验证服务和可见性绑定来控制用户访问界面和操作的权限。 ### 回答3: WPF Prism是一种用于构建模块化、可扩展、可重用的WPF应用程序的框架。在WPF Prism,可以使用权限框架来实现对应用程序各个模块或页面的访问权限控制。 权限框架主要涉及两个方面的功能:访问控制和权限管理。 在访问控制方面,权限框架可以定义不同的用户类型或角色,并为每个角色分配不同的权限级别。通过在应用程序的模块或页面上设置特定的权限要求,可以限制只有具有相应权限的用户才能访问或执行特定的功能。这样可以保证敏感信息只对特定用户开放,并提供更好的安全性。 在权限管理方面,权限框架提供了一种机制来管理和维护用户的权限设置。管理员可以使用权限管理界面对不同用户的权限进行配置,并可以根据实际需要进行权限的增加、删除、修改等操作。这样可以方便地进行权限的维护和管理,同时也能够及时响应用户权限的变更需求。 此外,权限框架还可以提供权限验证和异常处理等功能,以确保在用户尝试访问没有权限的功能时能够给予相应的提示或进行适当的处理。 总的来说,WPF Prism的权限框架为应用程序提供了一种有效的权限控制机制,可以帮助开发人员实现精细化的权限管理和访问控制,提高应用程序的安全性和稳定性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值