

Please notice, the employee class has been passed to BaseService as an entity. Return new EmployeeService().Add(newEmployee) ĮmployeeService class looks very clean and simple. Call the add method in employee service. long InsertEmployee(Employee newEmployee) Now, to insert one employee in a database and to get the newly inserted employeeId (identity) in one single transaction, we need to write only a few lines of code, as given below. In the solution, you will get all other methods, which provides you controls over database through model classes only. Public IList GetAll() // to get all entities Public IList Find(IFilter filter) // find entities by filter criteria Public T GetById(object id) // get single entity by its Id Public void UpdateRange(IList entities) // to update multiple entities Public void Update(T entity) // to update one entity

public long Add(T entity) // to insert single entity.
Wpf sqlite tutorial how to#
Here, I will explain only the methods inside BaseService and how to use them.
Wpf sqlite tutorial code#
Since the code is bit lengthy, you can get the complete working code from the attachment. The above-mentioned claases, attributes, interfaces are used and the BaseService methods are prepared. It provies basic CRUD operations to all the Service classes. The class given below is called BaseService, which is inherited by all the Service classes (example: EmployeeService). I.SetValue(obj, Convert.ChangeType(reader, i.PropertyType)) If (((DbColumnAttribute)ca).Convert = true) Var ca = i.GetCustomAttribute(typeof(DbColumnAttribute)) Where(p => p.CustomAttributes.FirstOrDefault(x => x.AttributeType = typeof(DbColumnAttribute)) != null).ToList()) Public IList Map(SQLiteDataReader reader)įoreach (PropertyInfo i in obj.GetType().GetProperties() Now, we have a class called EntityMapper, which actually reads SQLite db reader and fills in the property values. UnaryExpression ubody = (UnaryExpression)exp.Body īody = ubody.Operand as MemberExpression MemberExpression body = exp.Body as MemberExpression I will explain this custom attribute little later.Įmployee model class looks like this. Another point I must mention at this moment is, each property will be decorated with a custom attribute called DbColumn, which denotes it to be a table column in the database. These model classes will be exactly similar to entity classes in case of an Entity framework with SQL Server. The properties will be same as the column's name. The model class name will be exactly the same as the table name (“Employee”). This class library will contains all the model classes.
Wpf sqlite tutorial windows#
It can be Windows form Application of WPF Application or any other.Īs the name says, it can be used by all the projects under this solution. In my case, I will create a solution, as given below. Now, we have to create our solution and tiers. SQLiteDemo.db and table name and schema is shown below. In our case, we have only one table inside the database – Employee. Also, how to optimize the code and its maintainability.įirst of all, let’s create a SQLite database, using any SQLite manager.How to use reflection and generic to create your custom and extensible ORM to interact with SQLite database.How to architect a SQLite +C# client Application without any ORM.

In the article given below, I will show you.

Thus, can we use quasi-lambda codes to interact with such file system DB? Can we avoid hardcoded table/column names from our C# code? If you ask me these questions, I would say "absolutely”. After working on Linq and Entity framework-like ORM, it feels quite irritating to write inline queries for CRUD operation in SQLite DB. Thus, we are compelled to use inline queries. There is no proper Entity Framework support available, as of now. Unlike SQL Server, this doesn’t require any Service to interact with the database but the real problem occurs, when we try to interact with SQLite DB in C#. SQLite is open source file system database.
