Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How to pass an object to a service?

$
0
0

I have a object, Person person = _sessionService.GetPersonFromSession(HttpContext); it has lots of data about that person. I have a service that is used to log information about that service.  Person is in my AccountController.cs. PersonActivityLogService.cs has methods to insert data into a table:

public int InsertPersonActivityLog(int logId, string edi, string titleOrRank, string firstName, string middleInitial, string lastName, DateTime activityDateTime, string activityTypeDefinition, string additionalDetails, string reasonMessage, string notes)
        {
            StringBuilder errorMessages = new StringBuilder();
            try
            {
                // Get the connection string from the appsettings.json.
                conn.ConnectionString = myConn.DefaultConnectionString(configuration);  //@"Server=BRENTHUMBERDEVE;Database=NextGeneration;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
                                                                                        // Open the connection.
                conn.Open();
                cmd.Connection = conn;

                // Set the command type of the command object.
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                // Assign values as "parameters to avoid 'SQL Injections.'"
                cmd.Parameters.AddWithValue("@edi", edi);
                cmd.Parameters.AddWithValue("@lName", lastName);
                cmd.Parameters.AddWithValue("@fname", firstName);
                cmd.Parameters.AddWithValue("@mi", middleInitial);
                cmd.Parameters.AddWithValue("@date", activityDateTime);
                cmd.Parameters.AddWithValue("@descriptor", activityTypeDefinition);
                cmd.Parameters.AddWithValue("@details", additionalDetails);
                cmd.Parameters.AddWithValue("@reason", reasonMessage);
                cmd.Parameters.AddWithValue("@notes", notes);

                return cmd.ExecuteNonQuery();
            }
            catch(SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    errorMessages.Append("Index #" + i + "\n" +"Message: " + ex.Errors[i].Message + "\n" +"LineNumber: " + ex.Errors[i].LineNumber + "\n" +"Source: " + ex.Errors[i].Source + "\n" +"Procedure: " + ex.Errors[i].Procedure + "\n");
                }
                Console.WriteLine(errorMessages.ToString());
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return 0;
        }

In short...how do you pass or parse person from AccountController.cs into my service?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>