Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Could you please suggest for the below code is proper or any better way is there for the below code
The main thing here is the object should get before and after data for user main action for multiple users until collection object ready to pass to Main method.

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<object> consolidatedAuditEvent = new List<object>();
            List<MyApplication> listMyApplication = new List<MyApplication>();
            int max = 30;
            Audit objAudit = new Audit();
            for (int i=0;i< max; i++)
            {
                MyApplication objMyApplication = new MyApplication();
                objMyApplication.id = i;
                objMyApplication.MyResponse = objAudit.PostAuditData(null,i);
                listMyApplication.Add(objMyApplication);
            }
            if (listMyApplication != null && listMyApplication.Count > 0)
            {
                for (int i = 0; i < max; i++)
                {
                    var jjj = listMyApplication.Find(x => x.id == i);
                    jjj.MyResponse = objAudit.PostAuditData(jjj.MyResponse,i);
                    consolidatedAuditEvent.Add(jjj.MyResponse);
                }
            }

            if (consolidatedAuditEvent != null)
            {
                foreach (MyResponse result in consolidatedAuditEvent)
                {
                    Console.WriteLine(result.AfterName + result.BeforeName);
                }
                Console.Read();
            }
        }
    }
    public class MyApplication { public int id { get; set; }
        public MyResponse MyResponse { get; set; }

    }

    public class MyResponse
    {
        public string BeforeName { get; set; }
        public string AfterName { get; set; }


    }

    public class Audit
    {
        public MyResponse PostAuditData( MyResponse objMyResponse,int i)
        {
            MyResponse obj = objMyResponse == null ? new MyResponse(): objMyResponse;
            if (objMyResponse == null)
                obj.BeforeName = "BeforeName"+i;
            else
                obj.AfterName = "AfterName"+i;
            return objMyResponse = obj;

        }

    }
}
Posted
Updated 15-Aug-19 20:52pm
v4
Comments
[no name] 13-Aug-19 19:57pm    
Are we to assume there IS a "performance problem"? Or are we just "looking" for one? Whether it exists or not ....

Well ... I have no idea what your "performance logic" problem might be but that code is um ... less than performant.
Look at what you are doing:
public MyResponse PostAuditData( MyResponse objMyResponse,int i)
        {
            MyResponse obj = objMyResponse == null ? new MyResponse(): objMyResponse;
            if (objMyResponse == null)
                obj.BeforeName = "BeforeName"+i;
            else
                obj.AfterName = "AfterName"+i;
            return objMyResponse = obj;

        }
So the first line checks it if is null, and if so, replaces it with a new instance.
And the second row checks again to see if it is null. Why? Why do two checks?
And at the end, you assign a value to a non-ref and nonoutput parameter and return it. Why?

The first bit could be better done using the null-coalescing operator
MyResponse obj = objMyResponse ?? new MyResponse();

but combining them is simpler and more obvious:
public MyResponse PostAuditData(MyResponse objMyResponse, int i)
    {
    if (objMyResponse == null)
        {
        objMyResponse = new MyResponse();
        objMyResponse.BeforeName = "BeforeName" + i;
        }
    else
        {
        objMyResponse.AfterName = "AfterName" + i;
        }
    return objMyResponse;
    }

And it's not exactly an obvious way to do any of that ... it looks like somebody jumped straight into coding without thinking at all about what he was trying to do. I have no idea why the Audit class exists at all, or why it's one and only method isn't static.
To be honest, I'd start again from scratch after reading my homework question again, really carefully and thinking about the design for at least ten minutes before starting to code anything.
 
Share this answer
 
you can do something like this 
No need for Audit class 
No need to 2 loops to do the same things



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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            var consolidatedAuditEvent = new List<object>();
            var listMyApplication = new List<MyApplication>();

            int max = 30;

            for (int i = 0; i < max; i++)
            {
                var objMyApplication = new MyApplication();
                objMyApplication.id = i;
                objMyApplication.MyResponse = PostAuditData(null, i);

                listMyApplication.Add(objMyApplication);
                consolidatedAuditEvent.Add(objMyApplication.MyResponse);
            }

            if (consolidatedAuditEvent != null)
            {
                foreach (MyResponse result in consolidatedAuditEvent)
                {
                    Console.WriteLine(result.AfterName + result.BeforeName);
                }
                Console.Read();
            }
        }

        public static MyResponse PostAuditData(MyResponse objMyResponse, int i)
        {
            if(objMyResponse == null)
            {
                objMyResponse = new MyResponse();
                objMyResponse.BeforeName = "BeforeName" + i;
            }
            else
            {
                objMyResponse.AfterName = "AfterName" + i;
            }
            return objMyResponse;
        }
    }
    public class MyApplication
    {
        public int id { get; set; }
        public MyResponse MyResponse { get; set; }

    }

    public class MyResponse
    {
        public string BeforeName { get; set; }
        public string AfterName { get; set; }
    }
    
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900