Home > Software design >  dotNet Core 5 API Service adds objects to list but result has multiple copies of the last item
dotNet Core 5 API Service adds objects to list but result has multiple copies of the last item

Time:05-07

dotNet Core 5 C# Web API project, Service class adds objects (based on a viewmodel) to a list. Debugging shows all data correct as the object is built and added to the List, but the returned result has multiple (matching the count it should have) copies of the last item added only. Service code and viewmodel code below. What am I missing?

using FS.Data.Repositories;
using FS.Models;
using FS.ViewModels;
using FS.ViewModels.APVendor;
using Microsoft.Extensions.Options;
using NLog;
using System.Collections;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace FS.Services
{
    public class GLAccountSetService : IGLAccountSetService
    {
        private static Logger _logger = LogManager.GetCurrentClassLogger();
        private readonly IOptions<AppSettingsModel> AppSettings;
        private readonly HttpClient Client;

        GlAccountSetResponseViewModel result = new GlAccountSetResponseViewModel();
        public GLAccountSetService(HttpClient client, IOptions<AppSettingsModel> app)
        {
            Client = client; //?? throw new ArgumentNullException(nameof(client));
            AppSettings = app;
        }
        public async Task<GlAccountSetResponseViewModel> GetGLAccountSets(IList<GlAccountSetRequestViewModel> req, IAPCorpRepository apcr)
        {
            result.GlAccountSetViewModels = new List<GlAccountSetViewModel>();
            result.SuccessErrorWarningResult = new SuccessErrorWarningResult();
            GlAccountSetViewModel accountSet = new GlAccountSetViewModel();
            
            string payorID = "";
            string payeeID = "";
            string payorCashAccount = "";
            string payeeCashAccount = "";
            string expenseAccount = "";
            string offsetAccount = "";
            string type = "";            
            long reqID = 0;

            foreach (GlAccountSetRequestViewModel glr in req)
            {
                type = glr.Type;
                expenseMain = glr.ExpenseAccount;
                payorID = glr.PayorCorpID;
                payeeID = glr.PayeeCorpID;
                reqID = glr.ReqID;

                //...skipping calculation code that works
                
                accountSet.ExpenseAccount = expenseAccount;
                accountSet.PayorCashAccount = payorCashAccount;
                accountSet.PayeeCashAccount = payeeCashAccount;
                accountSet.OffsetAccount = offsetAccount;
                accountSet.ReqID = reqID;
                result.GlAccountSetViewModels.Add(accountSet);                                
            }
            return result;
        }                
    }
}

using FS.Common;
using System.Collections.Generic;

namespace FS.ViewModels.APVendor
{
    public class GlAccountSetResponseViewModel : FSObject<GlAccountSetResponseViewModel>
    {
        public IList<GlAccountSetViewModel> GlAccountSetViewModels { get; set; }
        public SuccessErrorWarningResult SuccessErrorWarningResult { get; set; }
    }
}
namespace FS.ViewModels.APVendor
{
    public class GlAccountSetViewModel
    {
        public string ExpenseAccount { get; set; }
        public string PayorCashAccount { get; set; }
        public string PayeeCashAccount { get; set; }
        public string OffsetAccount { get; set; }
        public long ReqID { get; set; }
    }
}

CodePudding user response:

You're adding the same accountSet instance to the list multiple times, only modifying it in the loop. Thus all references to it in the list will "have" the most recently set values.

You need to create a new GlAccountSetViewModel instance in the loop and add that one, or make it a struct to copy it when being added.

  • Related