Skip to content

Instantly share code, notes, and snippets.

@dresswithpockets
Created December 14, 2021 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dresswithpockets/a3dc2db98370e872a10e0862927d1109 to your computer and use it in GitHub Desktop.
Save dresswithpockets/a3dc2db98370e872a10e0862927d1109 to your computer and use it in GitHub Desktop.
Working in C# like its Go
public record User(int Id, string Username);
// might have some state, doesnt necessarily need any though
public record UserController();
public static class UserControllerExt {
public static void DoComplexActionAgainstUser(
this UserController uc,
IUserConn conn,
int id) {
// do things against conn
}
}
public interface IUserConn {
User Get(int id);
User Put(User user);
User Patch(User user);
User Delete(User user);
}
public record UserConn(DbConnection DbConnection) : IUserConn {
// implementation details, maybe dapper crud ;)
}
IUserConn GetMockUserConn() {
// get in-memory or on-disk mock db or something
}
void TestAction() {
var conn = GetMockUserConn();
var user = conn.Put(new User("name"));
var controller = new UserController();
controller.DoComplexActionAgainstUser(conn, user.Id);
var updatedUser = conn.Get(user.Id);
// assert some state
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment