Code Monkey home page Code Monkey logo

database-modelizer's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.