Code Monkey home page Code Monkey logo

norm's Introduction

Norm

(Yet Another) .Net object-relational mapper

Overview

Norm is an opinionated object-relational mapper (ORM). It relies on a specific set of naming conventions for your objects. Yes, strict adherence to an arbitrary set of naming conventions limits the range of use cases that Norm can handle, but I was scratching my own itch, and wanted a simple ORM for smaller projects that does only what I need it to, and doesn't require a bunch of setup to get started.

Norm defines a single class for interacting with your database, aptly named Database. You have 2 options for initializing a Database object:

// Pass in the connection string. Norm will open a connection to the database, and close it when it's done.
public Database(string connectionString)
// Pass in the connection. Norm will assume the connection is already open, and leave it open when it's done.
public Database(IDbConnection connection)

Methods

The Database class defines 4 methods for performing your typical CRUD database operations: Select, Insert, Update, Delete.

The Select method builds a query using a fluent-interface. You can then return a single object using SingleOrDefault(), or a list of matching objects using ToList(). The query can be fine-tuned using a number of fluent methods: OrderBy(), OrderByDesc(), and Limit().

Example usage:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
	public char MiddleInitial { get; set; }
	public string LastName { get; set; }
	public char Gender { get; set; }
	public DateTime CreateDate { get; set; }
	public DateTime? UpdateDate { get; set; }
}

var db = new Norm.Database(connString);

// select all
IEnumerable<Person> people = db.Select<Person>().ToList();

// select one
Person person = db.Select<Person>(p => p.Id == 1).SingleOrDefault();

// use of OrderBy and Limit
IEnumerable<Person> people = db.Select<Person>(p => p.LastName == "Smith")
                               .OrderBy(p => p.FirstName)
                               .Limit(5)
                               .ToList();

The Insert method inserts the object obj, and returns the Id of the newly inserted object.

Example usage:

Person person = new Person();
person.FirstName = "John";
person.MiddleInitial = 'Q';
person.LastName = "Smith";
person.Gender = 'M';

person.Id = db.Insert(p);

The Update method updates an existing object obj, and returns true if the object was updated successfully, otherwise false.

Example usage:

Person person = db.Select<Person>(p => p.Id == 1).SingleOrDefault();
person.FirstName = "Jane";
person.MiddleInitial = null;
person.LastName = "Doe";
person.Gender = 'F';

db.Update(person);

The Delete method deletes an existing object obj, and returns true if the object was deleted successfully, otherwise false.

Example usage:

Person person = db.Select<Person>(p => p.Id == 1).SingleOrDefault();
db.Delete(person);

Assumptions & Naming Conventions

  • Norm assumes that your object and property names map directly to your table and column names.
  • Norm uses the following naming conventions (note that name-matching is not case sensitive):
    • The primary key field (required) is either Id or <TypeName>Id.
    • The created timestamp (optional) is either Created, CreateDate, or CreatedOn. This field will automatically be set when a record is inserted.
    • The updated timestamp (optional) is either Updated, UpdateDate, or UpdatedOn. This field will automatically be set when a record is updated.

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.