Data conversion code or reflection code?

Getting data from a database table into an object in code always seemed like regular code. There are two ways that I have found for this:

  • have a code generator that reads the database table and creates a class and a controller for matching data fields with class fields or
  • use reflection to take the database field and find it in the class.

The problems noted by the above 2 methods are listed below.

  • Method 1 seems to me that I missed something, because I need to create a controller for each table.
  • Method 2 seems too time consuming when you get into access code heavy data.

Is there a third route that should try to get data from the database to my objects?

+3
source share
4 answers

I think the answer to this question depends on the technologies available for the language you are going to use.

I am very successful using ORM (NHibernate), so naturally I can recommend option 1.

There are other options you might want to accept:

  • If you use .NET, you can use attributes for your class properties to serve either as a mapping inside the class, or as data that might be mirrored
  • If you use .NET, Fluent NHibernate will make it easier to display types in your code.
  • , , , , , . CRUD-, , .
+1

OR (Object-Relational) mappers . , OR , Hibernate. ?

+3

, . " " , , , .

0

I agree with lewap, ORM (Object Relational Mapper) really helps in these situations. You can also consider the Active Record pattern (discussed in the Fowler Patterns Enterprise Architecture book). This can really speed up the creation of DAL in simple applications.

0
source

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


All Articles