Skip to content
codeschreiber edited this page Apr 13, 2012 · 19 revisions

Do you like the convenience of an ORM but hate the poor performance, messy SQL generation, and general complexity of Entity Framework or NHibernate? Have you considered or used micro-ORMs like Massive, Peta-Poco, or Dapper only to realize you're still writing too much repetitive SQL for simple CRUD functionality? If so, then YamORM may be the solution you're looking for.

YamORM gives you the best of both worlds. It is much smaller and performs better than a full ORM but has more features than a micro-ORM. You could probably call it a mini-ORM, but I call it "Yet Another Micro-ORM."

Features

  • Fluent configuration interface for mapping objects to tables and properties to columns.
  • Simple, clean SQL generation for basic CRUD functionality: SELECT all, SELECT by Id, INSERT, UPDATE, and DELETE.
  • Support for Stored Procedures text queries, using a Fluent interface to:
    • Set parameter names and values, and
    • Map result columns to object properties
  • Auto-mapping of Tables and Columns if they match Object and Property names.
  • Auto-mapping of Keys if the Property and corresponding Column names match "Id" or "Id" appended to the Object name (underscores are ignored).
  • Support for Transactions
  • High performance - close to that of raw ADO.NET

Code Samples

Database tables for code samples

CREATE TABLE [dbo].[Category](
    [CategoryId] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Category] PRIMARY KEY CLUSTERED 
(
    [CategoryId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[Product](
    [ProductId] [nvarchar](10) NOT NULL,
    [CategoryId] [int] NOT NULL,
    [Name] [nvarchar](200) NOT NULL,
    [Description] [nvarchar](1000) NOT NULL,
    [Price] [money] NOT NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED 
(
    [ProductId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Product]  WITH NOCHECK ADD  CONSTRAINT [FK_Product_Category] FOREIGN KEY([CategoryId])
REFERENCES [dbo].[Category] ([CategoryId])
GO

ALTER TABLE [dbo].[Product] NOCHECK CONSTRAINT [FK_Product_Category]
GO

The following code samples show the basic usage of YamORM

Configure DatabaseFactory Instance

DatabaseFactory.Instance.ConfigureDatabase("Connection String Name");

DatabaseFactory.Instance.ConfigureTable<Category>()
    .TableName("category")
    .Key(x => x.CategoryId, true) // Key is generated by database (Identity)
    .Property(x => x.CategoryId, "category_id")
    .Property(x => x.Nme, "name");

DatabaseFactory.Instance.ConfigureTable<Product>()
    .TableName("product")
    .Key(x => x.ProductCode) // Key is not generated by database
    .Property(x => x.ProductCode, "product_code")
    .Property(x => x.CategoryId, "category_id")
    .Property(x => x.Name, "name")
    .Property(x => x.Description, "description")
    .Property(x => x.Price, "price");

###Create Database object

IDatabase database = DatabaseFactory.CreateDatabase();

###Insert records using Transaction

database.BeginTransaction();
try
{
    Category cat = new Category { Name = "Widgets" };
    database.Insert(cat); // cat.CategoryId is set to database generated value (Identity)
    
    Product prod = new Product
    {
        ProductCode = "PREMWIDG01",
        CategoryId = cat.CategoryId,
        Name = "Premier Widget",
        Description = "Our top of the line Widget",
        Price = 19.99
    };
    database.Insert(prod);

    database.CommitTransaction();
}
catch
{
    database.RollbackTransaction();
}
Clone this wiki locally