I am needing to get a sanity check to make sure I am setting up my model and viewmodel correctly. I believe what I have is correct even though I have seen some documentation that shows different ways this can be done. I posted two examples of my code below. Any hints or suggestions are always appreciated.
Model:
namespace StewLedger.Model
{
public class AccountModel : IDisposable
{
private Connection conn = new Connection (ConnectionString.connectionString);
private bool disposed = false;
private SafeHandle handle = new SafeFileHandle(IntPtr.Zero, true);
private AccountVM accountVM = new AccountVM();
private AccountTable accountTable = new AccountTable();
public bool AddNewAccount(AccountVM account)
{
if(conn.NonQueryCommand(accountTable.AddAccount(account.AccountType, account.AccountName, User._username, account.BankId, account.AccountNumber, account.StartingBalance, account.Balance)))
{
return true;
}
return false;
}
public Accounts LoadAccounts()
{
List<string> o = conn.QueryCommand(accountTable.GetAccounts(User._username));
Accounts accounts = new Accounts();
int numRecords = 0;
numRecords = o.Count / 7;
int item = 0, item1 = 1, item2 = 2, item3 = 3, item4 = 4, item5 = 5;
for (int i = 0; i < numRecords; i++)
{
AccountVM accountVM = new AccountVM
{
Id = Convert.ToInt16(o[item]),
AccountName = o[item1],
AccountType = o[item2],
Balance = Convert.ToDouble(o[item4]),
StartingBalance = Convert.ToDouble(o[item3]),
AccountNumber = o[item5]
};
item += 7;
item1 += 7;
item2 += 7;
item3 += 7;
item4 += 7;
item5 += 7;
accounts.addAccount(accountVM);
}
return accounts;
}
public bool UpdateBalance(double balance, int id)
{
AccountModel accountModel = new AccountModel();
if (conn.NonQueryCommand(accountTable.UpdateBalance(User._username, balance, id)))
{
return true;
}
return false;
}
public void SelectAccount(DashBoardView dashBoard)
{
AccountVM accountVM = (AccountVM)dashBoard.gridAccounts.SelectedItem;
Transaction transaction = new Transaction();
Accounts accounts = LoadAccounts();
LedgerModel ledgerModel = new LedgerModel();
if (accountVM != null)
{
transaction =ledgerModel.LoadLedger(accountVM.Id, accountVM.AccountName);
for (int i = 0; i < accounts.Count; i++)
{
if (accounts[i].Id == accountVM.Id)
{
accountVM.Balance = accounts[i].StartingBalance;
}
}
for (int i = 0; i < transaction.Count; i++)
{
transaction[i].Balance = ledgerModel.CalculateBalance(Convert.ToDouble(accountVM.Balance), transaction[i]);
accountVM.Balance = transaction[i].Balance;
}
if (transaction != null)
{
for (int i = 0; i < transaction.Count; i++)
{
if (transaction[i].TransType == "Withdrawal")
{
transaction[i].Color = new SolidColorBrush(Colors.Red);
}
else if (transaction[i].TransType == "Deposit")
{
transaction[i].Color = new SolidColorBrush(Colors.SteelBlue);
}
else
{
transaction[i].Color = new SolidColorBrush(Colors.Green);
}
}
dashBoard.gridLedger.ItemsSource = transaction;
}
}
}
public void DeleteAccount(DashBoardView dashBoard)
{
AccountTable accountTable = new AccountTable();
LedgerTable ledgerTable = new LedgerTable();
AccountVM accountVM = (AccountVM)dashBoard.gridAccounts.SelectedItem;
using (Connection conn = new Connection(ConnectionString.connectionString))
{
TableInfo tableInfo = new TableInfo
{
Id = accountVM.Id,
Owner = User._username,
TableName = "Accounts",
T = TableInfo.Table.ACCT
};
conn.NonQueryCommand(accountTable.RemoveRow(tableInfo));
if (conn.TableExist(User._username + accountVM.AccountName + "Ledger") == true)
{
conn.NonQueryCommand(ledgerTable.DropTable(User._username, accountVM.AccountName));
}
dashBoard.gridAccounts.ItemsSource = LoadAccounts();
};
}
public void Dispose()
{
Dispose(false);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposed)
{
return;
}
//Free managed resources
if (disposing)
{
handle.Dispose();
conn.Dispose();
}
disposed = true;
}
}
}
ViewModel:
namespace StewLedger.ViewModel
{
public class AccountVM : Notifier
{
private string _accountType;
private string _accountName;
private string _accountNumber;
private double _balance, _startingBalance;
private int _Id;
private int _bankId;
public int Id { get => _Id; set { _Id = value; OnPropertyChanged("Id"); } }
public string AccountType { get => _accountType; set { _accountType = value; OnPropertyChanged("AccountType"); } }
public string AccountName { get => _accountName; set { _accountName = value; OnPropertyChanged("AccountName"); } }
public double Balance { get => _balance; set { _balance = value; OnPropertyChanged("Balance"); } }
public int BankId { get => _bankId; set { _bankId = value; OnPropertyChanged("BankId"); } }
public string AccountNumber { get => _accountNumber; set { _accountNumber = value; OnPropertyChanged("AccountNumber"); } }
public double StartingBalance { get => _startingBalance; set { _startingBalance = value; OnPropertyChanged("StartingBalance"); } }
}
public class AccountTypes : ObservableCollection<string>
{
public AccountTypes()
{
this.Add("Checking");
this.Add("Savings");
//Add credit and loans later will require seperate setup
}
}
public class Accounts : ObservableCollection<AccountVM>
{
public Accounts()
{
}
public void addAccount(AccountVM vM)
{
this.Add(vM);
}
public ObservableCollection<AccountVM> GetAccounts()
{
return this;
}
public void deleteAllAccounts()
{
this.Clear();
}
}
}
LoadAccounts
,UpdateBalance
andSelectAccount
methods? \$\endgroup\$