Created
June 20, 2011 13:42
-
-
Save einarwh/1035616 to your computer and use it in GitHub Desktop.
Dry data: Naive data client
This file contains hidden or 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 Client1 | |
{ | |
private readonly string _connStr; | |
private readonly DbProviderFactory _dpf; | |
public Client1(string connStr) : this(connStr, | |
DbProviderFactories.GetFactory("System.Data.SqlClient")) | |
{} | |
public Client1(string connStr, DbProviderFactory dpf) | |
{ | |
_connStr = connStr; | |
_dpf = dpf; | |
} | |
private DbParameter CreateParameter(string name, object val) | |
{ | |
var p = _dpf.CreateParameter(); | |
p.ParameterName = name; | |
p.Value = val; | |
return p; | |
} | |
public IEnumerable<User> GetCompanyUsers(string company) | |
{ | |
var result = new List<User>(); | |
using (var conn = _dpf.CreateConnection()) | |
using (var cmd = _dpf.CreateCommand()) | |
{ | |
conn.ConnectionString = _connStr; | |
conn.Open(); | |
cmd.Connection = conn; | |
cmd.CommandType = CommandType.StoredProcedure; | |
cmd.CommandText = "spGetCompanyUsers"; | |
var p = CreateParameter("@companyName", company); | |
cmd.Parameters.Add(p); | |
var reader = cmd.ExecuteReader(); | |
while (reader.Read()) | |
{ | |
var u = new User | |
{ | |
Id = (string) reader["id"], | |
UserName = (string) reader["user"], | |
Name = (string) reader["name"], | |
Email = (string) reader["emailAddress"], | |
Phone = (string) reader["cellPhone"], | |
ZipCode = (string) reader["zip"] | |
}; | |
result.Add(u); | |
} | |
} | |
return result; | |
} | |
public string GetUserEmail(string userId) | |
{ | |
using (var conn = _dpf.CreateConnection()) | |
using (var cmd = _dpf.CreateCommand()) | |
{ | |
conn.ConnectionString = _connStr; | |
conn.Open(); | |
cmd.Connection = conn; | |
cmd.CommandType = CommandType.StoredProcedure; | |
cmd.CommandText = "spGetEmailForUser"; | |
var p = CreateParameter("@userId", userId); | |
cmd.Parameters.Add(p); | |
return (string) cmd.ExecuteScalar(); | |
} | |
} | |
public void StoreUser(User u) | |
{ | |
using (var conn = _dpf.CreateConnection()) | |
using (var cmd = _dpf.CreateCommand()) | |
{ | |
conn.ConnectionString = _connStr; | |
conn.Open(); | |
cmd.Connection = conn; | |
cmd.CommandType = CommandType.StoredProcedure; | |
cmd.CommandText = "spInsertOrUpdateUser"; | |
var ps = new [] { | |
CreateParameter("@userId", u.Id), | |
CreateParameter("@user", u.UserName), | |
CreateParameter("@name", u.Name), | |
CreateParameter("@emailAddress", u.Email), | |
CreateParameter("@cellPhone", u.Phone), | |
CreateParameter("@zip", u.ZipCode) | |
}; | |
cmd.Parameters.AddRange(ps); | |
cmd.CommandType = CommandType.StoredProcedure; | |
cmd.ExecuteNonQuery(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment