Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3797454
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:24:22+00:00 2026-05-19T13:24:22+00:00

Given a structure for say, a bank account.. class Account { virtual int Id

  • 0

Given a structure for say, a bank account..

class Account
{
 virtual int Id { get; set; }
 virtual int Balance { get; set; }
}

And I want to track transactions done, so say a simple class…

class Transaction
{
 virtual int Id { get; set; }
 virtual Account Account { get; set; }
 virtual DateTime Timestamp { get; set; }
 virtual int Amount { get; set; }
}

Let’s assume I want to keep track of transactions done, which is the more intelligent approach here?

interface IAccountRepository
{
 void Deposit(int account, int amount)
}

or …

class Account
{
 void Deposit(int amount)
 {
  // this one is easier, but then I have to repeat
  // myself because I need to store the Transaction
  // in the database too.
 }
}

The Repository pattern seems to be the most encompassing, since it will have a handle to the unit of work/orm/session (using nHibernate) – but using a class-level method seems more straightforward, since it’s more in line with standard object oriented principle of ‘Do this to this object’.

My question is that if I want to log the transactions, then I have to make sure they get saved as database objects too. Going the second route, with a class level method, I can’t do this inside of the Account class, so I would end up having to repeat myself.

My other option is another abstraction..

interface ITransactionRepository
{
 void CreateTransaction(int account, int amount);
}

Which works fine, It kind of wraps A and B together because I would find the account in the TransactionRepository and then perform its Deposit method, but it doesn’t really feel like this is a wise approach. I don’t know why, my gut just tells me it isn’t the best way to go.

This applies to more than just this one set of classes, of course – it’s a principle of design. I wanted to see what more veteran programmers would do in this situation, if you have any thoughts.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:24:22+00:00Added an answer on May 19, 2026 at 1:24 pm

    I would sugguest using the repository pattern for CRUD (Create, read, update, delete) operations on the Accounts.

    interface IAccountRepository
    {
      Add(Account acc);
      Remove(Account acc);
      Account GetAccountById(int account);
      Update(Account acc);
    }
    

    Then put the Deposit method in the Account class like you mentioned

    class Account
    {
      void Deposit(int amount)
      { 
      }
    }
    

    Then you access the account through the repository to update the account

    // Get the account by id
    Account acc = rep.GetAccountById(23143);
    // Deposit the money
    acc.Deposit(21.23);
    // Update if needed
    rep.UpdateAccount(acc);
    

    Transations could be done in a similar way, but I would probably store the account id, rather than the Account instance in the Transcation.

    class Transaction
    {
     virtual int Id { get; set; }
     virtual int AccountId { get; set; }
     virtual DateTime Timestamp { get; set; }
     virtual int Amount { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have this XML file: <weather> <temp>24.0</temp> <current-condition iconUrl=http://....>Sunny</current-condition> </weather> I'm trying
I have a linked list of structures. Lets say I insert x million nodes
How to, Sequentially distribute data from a list to arrays (or some other structure)
Hello I have this table structure: **ProductsTable** ProductID ProductName **CategoriesTable** CategoryID CategoryName **ProductCategories** ProductID
Perl 5.10 introduced a proper switch construct with given/when and it seems like a
How and where do I configure my application so that, when it launches, the
I'm developing a REST API service for a large social networking website I'm involved
Update : I just found this documentation page . Wish there was a link
I learned Django following django book and the document. In the django book exmaple,
I noticed already a couple of times that working with dates doesn't allow for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.