In the following scenario:
- A form based Windows application written in C#
- GUI contains only interface related code but instantiates a class for functional logic
- SQL class that stores and loads data from SQL
- Data classes that store data in a format compatible with the database
I want to load data via the sql class and store it in the data classes. What would be the best way to do it?
- The functional class instantiates a SQL class, calls the query function and passes the contained values to a new data class instance using a constructor;
- The SQL class contains a function that converts the query to the data class and thus returns the data in the correct format to the functional class;
- The Data class contains a method to parse the query result directly so the functional logic only has to call the parse.
Option 1 seems to be the most fitting in this structure. My experience at writing larger applications is rather limited so I’ld like to know how someone with more experience thinks about this.
Someone with more experience uses an ORM (object relational mapper) to do this for them.
Or potentially writes a data access layer that encapsulates the database access and passes data transfer objects to the application code.
Option 1 feels like the database is bleeding into the application. Separation of concerns is very important in large applications.
Option 2 sounds like the start of writing your own Data Access Layer.
Option 3 will pollute the data transfer objects with knowledge of the relational database structure.
Think very hard about picking up an ORM.
Entity framework. Nhibernate. Subsonic. Massive. Dapper. There are lots to chose from. The main thing is that this is a problem that has already been solved many times by many developers with a lot of experience. You can leverage their learning and speed up your development.