Skip to content

simple-works/database-modelizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Icon Database Modelizer (C#)

C# 4.0 .NET Framework 4.0

Simple object–relational mapping (ORM) library for C# .NET.

It helps creating model classes to represent the tables of a relational database and access them through objects and generic collections to perform CRUD operations without having to write queries or dealing with table rows and columns names.

đź“ť How to use ?

  1. Add DatabaseModelizer.dll to project references.

  2. Add the namespace DatabaseModelizer:

using DatabaseModelizer;
  1. Create a new DataAccessor object using a connection string and choose a provider (Ex: Sql, OleDb, Odbc, ...etc):
DataAccessor dataAccessor = new DataAccessor("connection-string-here", DataProvider.Sql);
  1. Now, let's say you have a sql database with the following table:

👤 persons (🔑 id, full_name, gender, birth_date)

Create a new Person model class for this table as follow:

class Person : Model
{
    [Column("id")]
    public object Id { get; set; }

    [Column("full_name")]
    [System.ComponentModel.DisplayName("Full Name")]
    public object Name { get; set; }

    [Column("gender")]
    public object Gender { get; set; }

    [Column("birth_date")]
    [System.ComponentModel.DisplayName("Birth Date")]
    public object BirthDate { get; set; }
}

As you see, you must derive from Model class and use the ColumnAttribute to specify the database table column that the class property must map to. You can also take this chance to set the .NET DisplayNameAttribute to give the field a name that will be showed when it is used in a control like DataGridView.

  1. Create a generic ModelTable object for the created Person Type:
ModelTable<Person> persons = new ModelTable<Person>("persons", dataAccessor);

The created persons object represents the 👤 persons table in database and can be used to query the table.

  1. User persons object to perform the CRUD operations:

a. Create:

Person newPerson = new Person()
{
    Id = Guid.NewGuid(),
    Name = "Nina",
    Gender = "F",
    BirthDate = DateTime.Now
};
Database.Persons.Create(newPerson);

b. Read:

List<Person> allPersons = Database.Persons.Read();
List<Person> personsNamedNina = Database.Persons.Read(p => p.Name == "Nina");
Person nina = personsNamedNina[0];

b. Update:

nina.Name = "Maria";
Database.Persons.Update(nina, p => p.Id == nina.Id);

b. Delete:

Database.Persons.Delete(p => p.Id == nina.Id);

PS: Check the included Sample Client example project.

🚀 Development

đź“„ License

Licensed under MIT.