2

I'm trying to create better separation of concerns for code reuse in my program, that way I don't have a bloated controller that does all these different things.

for instance, in my application I have a user profile where users can upload a profile pic. If they don't customize their profile pic, I set the default profile pic to an avatar. I do this through a method to check if their profile pic string is null.

I created a folder called HelperMethods and created a class called UserHelperMethods which currently has one function:

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;
        }
    }
}

enter image description here

Now, in the controller, under the controller's folder, I added using HiRatik.Stories.HelperMethods;

and tried to call the public function GetUserProfilePic from the UserController. But I'm getting an error on the implementation. I'd like to be able to place a lot of these general functions related to users in another class like UserHelperMethods to clean up the bulk in the controller, but I'm missing something on the implementation. the using statement at the top is grayed out, so it's not picking up the function call. Any ideas?

enter image description here

2
  • 1
    Do you have an instance of that object in your class? UserHelperMethods helper = new UserHelperMethods(). Use: helper.GetUserProfilePic(foundUser) Commented Aug 30, 2018 at 16:20
  • ah. duh!!! Thanks. Commented Aug 30, 2018 at 16:23

5 Answers 5

3

You need to add an instance of the helper method class to every class you want to use it in.

UserHelpMethods helper = new UserHelperMethods();

then you can use it as:

helper.GetUserProfilePic(foundUser);
...
help.DoSomethingImportant(foundUser);
Sign up to request clarification or add additional context in comments.

Comments

1

You may want to make this into an Extension. Then you will be able to call it like this:

user.GetProfilePic();

The changes you have to do is, to make both your class and method static and have the this keyword before the parameter. Something like

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;
        }
    }

1 Comment

Depending on the version of C# you are using, you could write the entire method like return user.ProfilePic ?? "profile_pic_default.png";
1

I would consider making these methods static.

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;
        }
    }
}

If the helper methods don't rely on any state within the UserHelperMethods object, this will make it much easier to call your methods from anywhere, as there is no longer a need to create an instance of the UserHelperMethods type. You can call the method like this.

UserHelperMethods.GetUserProfilePic(foundUser);

Comments

0

just create instance of the class

var myInstance = new UserHelperMethods();

then just use myInstance object to access the functions in UserHelpMethods class

so you can call any function in UserHelpMethods like this

myInstance.FunctionName();

so in your case it will be like

myInstance.GetUserProfilePic(foundUser);

Comments

0

you could update your code to one of the following 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;
        }
    }
}

and inside your Controller just use call it like this

UserHelperMethods.Instance().GetUserProfilePic(founduser);

Or the easiest way

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

the diff you won't need to create instance all the time in diff controllers

I wish this help you !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.