首页 > 解决方案 > 如何将变量注入c#中的每个类或方法

问题描述

我有以下代码。

    [HttpGet]
    public async Task<List<TenantManagementWebApi.Entities.SiteCollection>> Get()
    {
        var tenant = await TenantHelper.GetActiveTenant();
        var siteCollectionStore = CosmosStoreFactory.CreateForEntity<TenantManagementWebApi.Entities.SiteCollection>();
        await siteCollectionStore.RemoveAsync(x => x.Title != string.Empty); // Removes all the entities that match the criteria
        string domainUrl = tenant.TestSiteCollectionUrl;
        string tenantName = domainUrl.Split('.')[0];
        string tenantAdminUrl =  tenantName + "-admin.sharepoint.com";

        KeyVaultHelper keyVaultHelper = new KeyVaultHelper();
        await keyVaultHelper.OnGetAsync(tenant.SecretIdentifier);

        using (var context = new OfficeDevPnP.Core.AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(tenantAdminUrl, tenant.Email, keyVaultHelper.SecretValue))
        {
            Tenant tenantOnline = new Tenant(context);
            SPOSitePropertiesEnumerable siteProps = tenantOnline.GetSitePropertiesFromSharePoint("0", true);
            context.Load(siteProps);
            context.ExecuteQuery();
            List<TenantManagementWebApi.Entities.SiteCollection> sites = new List<TenantManagementWebApi.Entities.SiteCollection>();
            foreach (var site in siteProps)
            {
                if(site.Template.Contains("SITEPAGEPUBLISHING#0") || site.Template.Contains("GROUP#0"))
                {
                    string strTemplate= default(string);
                    if(site.Template.Contains("SITEPAGEPUBLISHING#0"))
                    {
                        strTemplate = "CommunicationSite";
                    };
                    if (site.Template.Contains("GROUP#0"))
                    {
                        strTemplate = "Modern Team Site";
                    };

                    try
                    {
                        Guid id = Guid.NewGuid();
                        Entities.SiteCollection sc = new Entities.SiteCollection()
                        {
                            Id = id.ToString(),
                            Owner = site.Owner,
                            Template = strTemplate,
                            Title = site.Title,
                            Active = false,
                            Url = site.Url
                        };

                        var added = await siteCollectionStore.AddAsync(sc);
                        sites.Add(sc);
                    }
                    catch (System.Exception ex)
                    {
                        throw ex;
                    }


                }
            }

            return sites;
        };
    }

但是以下几行,我在每种方法上都重复它们:

  var tenant = await TenantHelper.GetActiveTenant();
            var siteCollectionStore = CosmosStoreFactory.CreateForEntity<TenantManagementWebApi.Entities.SiteCollection>();
            await siteCollectionStore.RemoveAsync(x => x.Title != string.Empty); // Removes all the entities that match the criteria
            string domainUrl = tenant.TestSiteCollectionUrl;
            string tenantName = domainUrl.Split('.')[0];
            string tenantAdminUrl =  tenantName + "-admin.sharepoint.com";

            KeyVaultHelper keyVaultHelper = new KeyVaultHelper();
            await keyVaultHelper.OnGetAsync(tenant.SecretIdentifier);

我的项目中会有很多 API 控制器

有没有一种简单的方法(不是重构为一种方法),让我的代码更干净并注入我需要的变量,而无需每次都复制和粘贴?

标签: c#.netasp.net-web-api

解决方案


推荐阅读