Structuring Classes

The final question: how are classes usually structured in applications?

I am currently writing a test banking application in asp.net For example: I have these two classes. One is an account, and the other is a utility class (it deals with accounts, that is, get accounts, update accounts, etc.).

public Account {
  int ID;
  string Name;
  double Balance;
}

public Accounts {
  public List<Account> GetAllAccounts();
  public Account GetAccountByID(int AccountID);
}

in my view layer, when I want to get the account that I am using:

Account editAccount = new Accounts().GetAccountByID(234);

You can see that I am creating a new Accounts () class to get an account. What should I really do? Or is it right? Does the static class fit this need better?

I feel that it gets very clogged, and if it gets bigger, it might be uncontrollable with classes having similar names.

? ?

.

+3
6

, , API , . , , :

  • , , .
  • , , .

, , .

, , . (DI) DI , .

Accounts , ( ), . , , .

, OOD, . .

+7

, . :

public interface IAccountRepository
{
    IList<Account> GetAll();

    Account GetById(int accountId);

    // Insert and delete, if they apply

    // Other account-specific queries and operations
}

IAccountRepository:

public class EditAccountPresenter
{
    private readonly IEditAccountView _view;
    private readonly IAccountRepository _accounts;

    public EditAccountPresenter(IEditAccountView view, IAccountRepository accounts)
    {
        _view = view;
        _accounts = accounts;

        _view.DataBinding += OnDataBinding;
    }

    private void OnDataBinding(object sender, EventArgs e)
    {
        _view.Account = _accounts.GetById(234);
    }
}

, IAccountRepository, , :

var dataContext = new AccountDataContext("...connection string...");

this.Presenter = new EditAccountPresenter(this, new AccountRepository(dataContext));

. , , .

, . .

+2

GetAllAccounts GetAccountByID static Accounts. , Account. ( ).

+1

Account Accounts ... , Bank . , , .

+1

, Account. . "Get", GetAccountByID, static. .

 public static Account GetAccountByID(int AccountID); 

:

Account editAccount = Account.GetAccountByID(234); 
+1

, ?

.

, , , , , .

Is "Accounts" really something that the model has at all? Do you need a class for this? Could you just have a list of accounts and then use query operators on it? Instead of the method that gets the account by its identifier, you can just have a list and then use it accountList.First(x=>x.Id == 123). Perhaps this is a concept in which you do not even need to model.

0
source

Source: https://habr.com/ru/post/1733127/


All Articles