This library is a school project for managing a fictional library database connection using Entity Framework Core. It includes the AppDbContext
class, which is used to interact with the database.
Please note that some of the comments and chapters in this project and .md file are intended for educational purposes and may not provide practical value to the code or the reader. Most notes will be most likely incomplete or unfinished since in most cases they will only contain information that i either want aware of or wanted to remind my self of.
-
- Data Grid Binder
- List vs Bindinglist
- Data Grid Binder
The AppDbContext
class is defined in AppDbContext.cs. It extends the DbContext
class from Entity Framework Core and includes DbSet
properties for each of the tables in the database.
The XML markup in the configuration file sets up the connection string for the database. The AppDbContext
class in AppDbContext.cs uses this connection string to connect to the database. Said configuration file would be located in Library/Data/App.config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add
name="DefaultConnection"
connectionString="Host=; Database=; Username=; Password="
providerName="System.Data.SqlClient"
/>
</connectionStrings>
<appSettings>
<add key="GoogleCustomSearchApiKey" value="ApiKey" />
<add key="GoogleCustomSearchCx" value="SearchCx" />
</appSettings>
</configuration>
Its also possible to instead replace the connection string configuration manager with the connection string it self.
//App.config solution
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
//Hardcoded solution
string connectionString = "Host=; Database=; Username=; Password=";
Generic Types (T
and TViewModel
)
T
represents the type of entities in database. TViewModel
represents the type of view models that used for displaying data in DataGridView
.
Function Parameters
-
queryFunc
is a function that takes anAppDbContext
and returns anIQueryable<T>
.Note:
IQueryable<T>
is an interface that represents a collection of objects that can be queried in a LINQ fashion. -
whereFunc
is a function that takes an entity of typeT
and returns a boolean, it defaults to a function that always returnstrue
, meaning no filtering is applied. -
selectFunc
is a function that takes an entity of typeT
and converts it to a view model of typeTViewModel
.
Using AppDbContext
creates a new instance of the AppDbContext
class, and the using
statement ensures that the context is disposed of correctly when the block is exited.
Note: It's a good practice to wrap database operations in a using
statement to ensure proper resource management.
Query Execution
-
var query = queryFunc(context);
executes the providedqueryFunc
to get anIQueryable<T>
representing the initial set of data from the database. -
var filteredQuery = query.Where(whereFunc ?? (_ => true));
filters the data based on thewhereFunc
(or a default filter ifwhereFunc
is null).Note: The
??
operator is the null-coalescing operator, which means ifwhereFunc
is null, it uses a default filter that always returnstrue
. -
var viewModelList = filteredQuery.Select(selectFunc).ToList();
: Projects the filtered entities into view models using theselectFunc
and converts the result to a list.
A List<>
is simply an automatically resizing array, of items of a given type, with a couple of helper functions (eg: sort). It's just the data, and you're likely to use it to run operations on a set of objects in your model.
A BindingList<>
is a wrapper around a typed list or a collection, which implements the IBindingList interface. This is one of the standard interfaces that support two-way databinding. It works by implementing the ListChanged event, which is raised when you add, remove, or set items. Bound controls listen to this event in order to know when to refresh their display.
When you set a BindingSource's DataSource to a List<>, it internally creates a BindingList<> to wrap your list. You may want to pre-wrap your list with a BindingList<> yourself if you want to access it outside of the BindingSource, but otherwise it's just the same. You can also inherit from BindingList<> to implement special behavior when changing items.
IEditableObject is handled by the BindingSource. It'll call BeginEdit on any implementing object when you change the data in any bound control. You can then call EndEdit/CancelEdit on the BindingSource and it will pass it along to your object. Moving to a different row will call EndEdit as well.