0

I have two class Libraries, one for my "backend" Code and one for the WebInterface side of the project.

I'm passing two variables (UserId and EnvId) using Ajax/Jquery to my Controller in the WebInterface side of my project. I then need to pass the two values from the Var's to the other Class Library to run several methods for unlocking user accounts.

Below is the code from my Controller (the code here currently post console.log messages back to the browser to I know I'm getting the right ID's)

[HttpPost]
    public ActionResult UnlockUserAction(string UserId, string EnvId)
    {
        var user = UserId;
        var environment = EnvId;

        if (user == "" || user == "0")
        {

            return Json("Error - The User doesn't exist or there was an error", JsonRequestBehavior.AllowGet);
        }
        else
        {
            //pass userid & envid to UnlockUser Class will go here
            var result = user + " | " + environment;
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }

This is my code from the "backend" Class Library;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace UserImportInterfaceLibrary
{
    public class UnlockUser
    {
    //Get Environment SQL
    public string sql = "SELECT Server,Catalog,UserName,UserPwd FROM tbl_Environments WHERE Description = @environment";
    //Unlock User String
    public string unlockSQL = "UPDATE User_Account_Data SET Account_Locked = 0 WHERE User_ID = @userid";

    public void getEnvironment(EnvId)
    {
        var envData = EnvId;
    }
}  

}

1
  • var bob = new UnlockUser(); bob.WhateverYouNeedToDoHere(parametersHere); Commented Jan 17, 2019 at 12:13

2 Answers 2

1

You can do this in multiple ways. Either in the constructor of you class library or use it as a service by creating a parameter less constructor with a method that does your logic.

In an ideal solution I would recommend splitting your code into three projects. One that access' your data and gets everything you need. One that handles all your logic with the data received from data layer. Finally your MVC project that only deals with the logic layer.

To answer your question for now:

public class UnlockUser
{
     public UnlockUser(string user, string env)
     {
         //Do logic with params
     }

}

In your controller add:

    else
    {
        //pass userid & envid to UnlockUser Class will go here
        var foo = new UnlockUser(userId, envId);
        var result = user + " | " + environment;
        return Json(result, JsonRequestBehavior.AllowGet);
    }

OR:

Add parameter less constructor in your class and a method that takes your params and does some logic. Use it as so:

public class UnlockUser
{
     public UnlockUser()
     { }
     public <YourResultObject> <YourNewMethod> (string userId, string envId)
     {
          //Do some logic
          return <YourResultObject>;
     }
}

In your controller method:

var foo = new UnlockUser();
foo.YourNewMethod(userId, envId);`
Sign up to request clarification or add additional context in comments.

Comments

0

You need to define a function in your class that will receive the parameter you need like:

public void MyFunction(MyParameter){ //do anything with your parameter here }

And then in your controller you need to create an object of your class like:

var user = new UnlockUser();

Finally you can call the function of your class in the controller

user.MyFunction(MyParameter);

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.