Home > front end >  How to call business object class from main method?
How to call business object class from main method?

Time:02-11

I have separated my code into business object, access layer as shown below:

void Main()
{
    
}

//====================================================================
// Business object
public class ExpenseBO
{
    public void MakeExpense(ExpensePayload payload)
    {
        var trA = new TrAccess();
        var acA = new AcAccess();

        var account = acA.MakeAccount(payload.Account);

        payload.Transaction.Account = account;
        trA.MakeTransaction(payload.Transaction);
    }
}

//----------------------------------------------------------------------------
public class TrAccess
{
    private const string connString = "Host=localhost;Username=postgres;Password=1234;Database=ExpenseManagerDB";

    public void MakeTransaction(T t) 
    {
            using (var connection = new NpgsqlConnection(connString))
            {
                connection.Execute(@"INSERT INTO transaction (account_id, amount, date, note)
                                         SELECT a.account_id,@amount, @date, @note
                                         FROM account AS a
                                         WHERE a.account_name = @account_name", new { t.Amount, t.Date, t.Note, a.Account_Name });
            }
    }
}

public class AcAccess
{
    private const string connString = "Host=localhost;Username=postgres;Password=1234;Database=ExpenseManagerDB";

    public void MakeAccount(A a)
    {
        using (var connection = new NpgsqlConnection(connString))
        {
            connection.Execute(@"INSERT INTO account(account_name, type)
                             VALUES(@account_name, @type)", new { account.Account_Name, account.Type });
        }
    }
}

public class T
{
    public int Transaction_Id { get; set; }
    public int Account_Id { get; set; }
    public decimal Amount { get; set; }
    public string Note { get; set; }
    public DateTime Date { get; set; }
}

public class A
{
    public int Account_Id { get; set; }
    public string Account_Name { get; set; }
    public string Type { get; set; }
}

//--------------------------------------------
public class ExpensePayload
{
    public T Transaction { get; set; }
    public A Account { get; set; }
}

Now I wanted to call the ExpenseBO class method MakeExpense from the main methods which will create two objects and with them calls TrAccess, AcAccess.

But there is an ExpensePayload class. If I wanted to call ExpenseBO class MakeExpense method in main method, how should I do that? How should I create a ExpensePayload class object and call it in main method?

CodePudding user response:

  public void AccessToExpenseBO()
{
    var request = new ExpensePayload
    {
        Account = new A
        {
            Account_Id = 1,
            Account_Name = "Account 1",
            Type = "Type 1"
        },
        Transaction = new T
        {
            Account_Id = 1,
            Amount = 25,
            Date = DateTime.Now,
            Note = "Note 1",
            Transaction_Id = 25

        }
    };
    var expenseBO = new ExpenseBO();
    expenseBO.MakeExpense(request);
}

Basically, you can access how I show it but, I am gonna add some comments base on your code.

MakeAccount method does not return a value so, you cannot assign it to a variable. You should return a value in this case an Account to do what you want inside MakeExpense method.

public void MakeExpense(ExpensePayload payload)
{
    var trA = new TrAccess();
    var acA = new AcAccess();

    var account = acA.MakeAccount(payload.Account); //void method

    payload.Account = account; // so, there is no an account
    trA.MakeTransaction(payload.Transaction);
}

CodePudding user response:

  var payl = new ExpensePayload {
     Transaction = // some T,
     Account = // Some A
  }

  var bo = new ExpenseBo();
    
  bo.MakeExpense(payl);
   
  • Related