![]() Public void UpdateRange(IList entities) // to update multiple entities Public void AddRange(IList entities) // to bulk-insert multiple entities public long Add(T entity) // to insert single entity. Here, I will explain only the methods inside BaseService and how to use them. 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. 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. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |