Last active
August 29, 2015 14:02
-
-
Save alwalker/18de245d3b5ef90ed28e to your computer and use it in GitHub Desktop.
The Worlds Smallest ORM For People Who Hate ORM's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DAOBase | |
{ | |
private readonly string _connectionString; | |
public DAOBase(string database) | |
{ | |
try | |
{ | |
_connectionString = ConfigurationManager.ConnectionStrings[database].ConnectionString; | |
} | |
catch (Exception ex) | |
{ | |
throw new ApplicationException(String.Format("Error getting connection string {0} from config: {1}", database, ex.Message)); | |
} | |
} | |
protected void ExecuteNonQuerySproc(string sprocName, params SqlParameter[] args) | |
{ | |
try | |
{ | |
using (var conn = new SqlConnection(_connectionString)) | |
{ | |
using (var cmd = new SqlCommand(sprocName)) | |
{ | |
cmd.Connection = conn; | |
cmd.CommandType = System.Data.CommandType.StoredProcedure; | |
foreach (var arg in args) | |
{ | |
cmd.Parameters.Add(arg); | |
} | |
conn.Open(); | |
cmd.ExecuteNonQuery(); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw new ApplicationException("Error executing non query stored procedure: " + ex.Message); | |
} | |
} | |
protected IList<T> ExecuteEntitySproc<T>(string sprocName, params SqlParameter[] args) where T : Entity, new() | |
{ | |
try | |
{ | |
using (var conn = new SqlConnection(_connectionString)) | |
{ | |
using (var cmd = new SqlCommand(sprocName)) | |
{ | |
cmd.Connection = conn; | |
cmd.CommandType = System.Data.CommandType.StoredProcedure; | |
foreach (var arg in args) | |
{ | |
cmd.Parameters.Add(arg); | |
} | |
var ds = new DataSet(); | |
using (var da = new SqlDataAdapter() { SelectCommand = cmd }) | |
{ | |
conn.Open(); | |
da.Fill(ds, "results"); | |
var entities = new List<T>(); | |
foreach (DataRow row in ds.Tables["result"].Rows) | |
{ | |
var entity = new T(); | |
entity.PopulateFromDataRow(row); | |
} | |
return entities; | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw new ApplicationException("Error executing query stored procedure: " + ex.Message); | |
} | |
} | |
protected void ExecuteSaveSproc(Entity e, string sprocName, params SqlParameter[] args) | |
{ | |
try | |
{ | |
using (var conn = new SqlConnection(_connectionString)) | |
{ | |
using (var cmd = new SqlCommand(sprocName)) | |
{ | |
cmd.Connection = conn; | |
cmd.CommandType = System.Data.CommandType.StoredProcedure; | |
var id = new SqlParameter("id", SqlDbType.Int) { | |
Direction = ParameterDirection.InputOutput, | |
Value = e.Id | |
}; | |
cmd.Parameters.Add(id); | |
foreach (var arg in args) | |
{ | |
cmd.Parameters.Add(arg); | |
} | |
conn.Open(); | |
cmd.ExecuteNonQuery(); | |
if (id.Value != DBNull.Value) | |
{ | |
e.Id = Convert.ToInt32(id.Value); | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw new ApplicationException("Error executing non query stored procedure: " + ex.Message); | |
} | |
} | |
protected DataTable ExecuteDataTableSproc<T>(string sprocName, params SqlParameter[] args) where T : Entity, new() | |
{ | |
try | |
{ | |
using (var conn = new SqlConnection(_connectionString)) | |
{ | |
using (var cmd = new SqlCommand(sprocName)) | |
{ | |
cmd.Connection = conn; | |
cmd.CommandType = System.Data.CommandType.StoredProcedure; | |
foreach (var arg in args) | |
{ | |
cmd.Parameters.Add(arg); | |
} | |
var ds = new DataSet(); | |
using (var da = new SqlDataAdapter() { SelectCommand = cmd }) | |
{ | |
conn.Open(); | |
da.Fill(ds, "results"); | |
return ds.Tables["result"]; | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw new ApplicationException("Error getting data table: " + ex.Message); | |
} | |
} | |
} | |
public abstract class Entity | |
{ | |
abstract public void PopulateFromDataRow(DataRow row); | |
public int Id { get; set; } | |
protected int GetInt(DataRow row, string column) | |
{ | |
try | |
{ | |
return Convert.ToInt32(row[column].ToString()); | |
} | |
catch (Exception ex) | |
{ | |
throw new ApplicationException(String.Format("Couldn't convert {0} to an integer: {1}", column, ex.Message)); | |
} | |
} | |
protected string GetString(DataRow row, string column) | |
{ | |
try | |
{ | |
return row[column].ToString(); | |
} | |
catch (Exception ex) | |
{ | |
throw new ApplicationException(String.Format("Couldn't get {0} from data row: {1}", column, ex.Message)); | |
} | |
} | |
} | |
/****************************************************************/ | |
/* EXAMPLES */ | |
/****************************************************************/ | |
public class Customer : Entity | |
{ | |
private int _id; | |
private string _name; | |
public int Id { get { return _id; } } | |
public string Name { get { return _name; } } | |
public override void PopulateFromDataRow(DataRow row) | |
{ | |
_id = GetInt(row, "id"); | |
_name = GetString(row, "name"); | |
} | |
} | |
public class CustomerDAO : DAOBase | |
{ | |
public CustomerDAO(string database) | |
: base(database) | |
{ | |
} | |
public IList<Customer> GetCustomers() | |
{ | |
return ExecuteEntitySproc<Customer>("usp_GetAllCustomers"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment