Home > Net >  Is there any need to use Two connections with database if we use two business objects
Is there any need to use Two connections with database if we use two business objects

Time:02-10

code:

    public class ExpenseBO
{                                                  
    public void MakeExpense(ExpensePayload payload)
    {
        NpgsqlConnection connection = new NpgsqlConnection("Host=localhost;Username=postgres;Password=1234;Database=ExpenseManagerDB");
        var trA = new TransactionAccess(connection);
        var acA = new AccountAccess(connection);
        //acA.MakeAccount(payload.Account);
        
        var accountId = acA.GetAccountId(payload.Account);
        payload.Transaction.Account_Id = accountId;
        
    }                                                                                           
}   
public class TransactionAccess
{
    NpgsqlConnection connection;
    //constructor
    public TransactionAccess(NpgsqlConnection connection)
    {
        this.connection = connection;
    }
    //To make a Transaction
    public void MakeTransaction(Transaction t)
    {
        connection.Execute(@"INSERT INTO transaction(account_id, amount, date, note) 
        values (@account_ID, @amount, @date, @note);", new { t.Amount, t.Date, t.Note, t.Account_Id });
    }
}

public class AccountAccess
{
    NpgsqlConnection connection;
    public AccountAccess(NpgsqlConnection connection)
    {
        this.connection = connection;
    }
    public void MakeAccount(Account a)
    {
            connection.Execute(@"INSERT INTO account(account_name, type)
                                VALUES(@account_name, @type)", new { a.Account_Name, a.Type });
    }

My wuestion is thay, I am using ExpenseBO as business object here. if I wnated to make another business object then, is there any necessary to use another database connection? or jcan I use same database connection?

CodePudding user response:

  •  Tags:  
  • Related