Created
August 8, 2013 16:14
-
-
Save paaschpa/6186050 to your computer and use it in GitHub Desktop.
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 UserRepository | |
{ | |
public UserRepository(IUnitOfWork uow) : (uow) | |
{ } | |
public bool Authenticate(UserLogin userLogin) | |
{ | |
var user = DbConnExec<User>((con) => | |
con.Select<User>(user => | |
user.UserName == userLogin.UserName && user.Password == userLogin.Password | |
).FirstOrDefault | |
) | |
if (user != null) | |
{ | |
user.LoginTimes += 1; | |
var userLog = new UserLog { User = user, LogMessage = "Successful Login" } | |
var userSession = new UserSession { User = user, SessionStuff = "" } | |
DbConnExecTrans((con) => | |
{ | |
con.Update<User>(user); | |
con.Insert<UserLog>(userLog); | |
con.Insert<UserSession>(userSession); | |
}) | |
return true; | |
} | |
return false; | |
} | |
} | |
public abstract class RepositoryBase | |
{ | |
protected IUnitOfWork _uow; | |
protected RepositoryBase(IUnitOfWork uow) | |
{ | |
_uow = uow; | |
} | |
protected virtual T DbConnExec<T>(Func<IDbConnection, T> dbConnFn) | |
{ | |
using (var dbCon = _uow.NewConnection()) | |
{ | |
return dbConnFn(dbCon); | |
} | |
} | |
protected virtual void DbConnExecTransaction(Action<IDbConnection> dbConnFn) | |
{ | |
using (var dbCon = uow.NewConnection()) | |
{ | |
using (var trans = dbCon.OpenTransaction()) | |
{ | |
try | |
{ | |
dbConnFn(dbCon); | |
trans.Commit(); | |
} | |
catch (Exception ex) | |
{ | |
trans.Rollback(); | |
throw; | |
} | |
} | |
} | |
} | |
} | |
public class SqlServerUnitOfWork : IUnitOfWork | |
{ | |
protected UnitOfWorkBase(string connectionString) | |
{ | |
_connectionString = connectionString; | |
} | |
public virtual IDbConnection NewConnection() | |
{ | |
var conn = new SqlConnection(_connectionString); | |
conn.Open(); | |
return conn; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment