ASP.NET MVC lookup tables in Linq to SQL

This is really an architectural matter. I feel like I'm doing it wrong and wanted to contribute to best practices.

Say I have a table Transactionsand a table TransactionTypes. Views will present the relevant transaction data that is processed in my controller. The problem is that the logic in the controller can be a little complicated, and TransactionType is not provided by the inputs of the form, but is calculated in the controller. (What could be part of my problem).

For example, let's say that View represents the ViewModel that will be displayed in the TransactionType "Withdrawal". However, the controller discovers that it needs to change it to Overdraft, "because there are not enough funds. What I don't like is not :

transaction.TypeId =
    DataContext.TransactionTypes.Single(x => x.type == "Overdraft").id;

... since I will embed string literals in my code. Correctly?

OK, so I can match the values ​​with strong types that would allow me to do this:

class TranTypes
{
   public const long Deposit = 1;
   public const long Withdrawal = 2;
   public const long Overdraft = 3;
}

...

transaction.TypeId =
    DataContext.TransactionTypes.Single(x => x.id == TranTypes.Overdraft);

Now, if my search queries change in the database, I have one place where I can update the mappings, and my controllers still have an idea about the model.

But this is also embarrassing.

, , Linq To SQL , (Deposit, Withdrawal Draft) , . , , , , .

, , ?

.: -)

+3
2

, - - - .

, , - - , , .

+3

, , - Enum.

public enum TransactionType {
 Overdraft
}

transaction.TypeId =
    DataContext.TransactionTypes.Single(x => x.type == TransactionType.Overdraft.ToString()).id;

, .

( , Linq to SQL, ORM (, EF, DO.NET, LLBLGen ..) - .

TransactionType, OverdraftTransactionType, (), TransactionTypes .

: http://weblogs.asp.net/zeeshanhirani/archive/2008/08/16/single-table-inheritance-in-entity-framework.aspx

+1

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


All Articles