簡體   English   中英

從C#/。NET MVC App中的另一個類調用函數

[英]Calling a function from another class in C#/.NET MVC App

我試圖為程序中的代碼重用創建更好的關注點分離,這樣我就沒有一個have腫的控制器來完成所有這些不同的事情。

例如,在我的應用程序中,我有一個用戶個人資料,用戶可以在其中上傳個人資料圖片。 如果他們沒有自定義個人資料圖片,則將默認的個人資料圖片設置為頭像。 我通過一種方法來檢查他們的個人資料圖片字符串是否為空。

我創建了一個名為HelperMethods的文件夾,並創建了一個名為UserHelperMethods的類,該類當前具有一個功能:

namespace HiRatik.Stories.HelperMethods
{
    public class UserHelperMethods
    {
        //checks if the user's profile pic is null and sets it to default pic if it is
        public string GetUserProfilePic(ApplicationUser user)
        {
            if (user.ProfilePic == null)
            {
                user.ProfilePic = "profile_pic_default.png";
            }

            return user.ProfilePic;
        }
    }
}

在此處輸入圖片說明

現在,在控制器的控制器文件夾下,我using HiRatik.Stories.HelperMethods;

並嘗試從UserController調用公共函數GetUserProfilePic 但是我在實現上遇到錯誤。 我希望能夠將與用戶相關的許多這些常規功能放在另一個類(如UserHelperMethods以清理控制器中的大量內容,但是我在實現上缺少了一些東西。 頂部的using語句顯示為灰色,因此不會進行函數調用。 有任何想法嗎?

在此處輸入圖片說明

您需要將輔助方法類的實例添加到要在其中使用的每個類。

UserHelpMethods helper = new UserHelperMethods();

那么您可以將其用作:

helper.GetUserProfilePic(foundUser);
...
help.DoSomethingImportant(foundUser);

您可能需要將此擴展名。 然后您將可以這樣稱呼它:

user.GetProfilePic();

您要做的更改是,使您的類和方法都保持靜態,並使this關鍵字位於參數之前。 就像是

public static class ApplicationUserExtensions
    {
        //checks if the user's profile pic is null and sets it to default pic if it is
        public static string GetProfilePic(this ApplicationUser user)
        {
            if (user.ProfilePic == null)
            {
                user.ProfilePic = "profile_pic_default.png";
            }

            return user.ProfilePic;
        }
    }

我會考慮將這些方法設為靜態。

namespace HiRatik.Stories.HelperMethods
{
    public class UserHelperMethods
    {
        //checks if the user's profile pic is null and sets it to default pic if it is
        public static string GetUserProfilePic(ApplicationUser user)
        {
            if (user.ProfilePic == null)
            {
                user.ProfilePic = "profile_pic_default.png";
            }

            return user.ProfilePic;
        }
    }
}

如果輔助方法不依賴UserHelperMethods對象內的任何狀態,這將使從任何地方調用方法變得更加容易,因為不再需要創建UserHelperMethods類型的實例。 您可以像這樣調用方法。

UserHelperMethods.GetUserProfilePic(foundUser);

您可以將代碼更新為以下A之一:

namespace HiRatik.Stories.HelperMethods
{
    public class UserHelperMethods
    {
         private static UserHelperMethods _instance = null;
         public static UserHelperMethods Instance()
         {
           if(_instance == null)
           {
             _instance = new UserHelperMethods();
           }
           return _instance;
         }
        //checks if the user's profile pic is null and sets it to default pic if it is
        public string GetUserProfilePic(ApplicationUser user)
        {
            if (user.ProfilePic == null)
            {
                user.ProfilePic = "profile_pic_default.png";
            }

            return user.ProfilePic;
        }
    }
}

並在Controller內使用,像這樣調用它

UserHelperMethods.Instance().GetUserProfilePic(founduser);

還是最簡單的方法

var helperObj = new UserHelperMethods();
helperObj.GetUserProfilePic(founduser);

您無需始終在diff控制器中創建實例的diff

希望對您有幫助!!

只需創建該類的實例

var myInstance = new UserHelperMethods();

然后只需使用myInstance對象訪問UserHelpMethods類中的函數

因此您可以像這樣在UserHelpMethods調用任何函數

myInstance.FunctionName();

所以你的情況就像

myInstance.GetUserProfilePic(foundUser);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM