Created
December 14, 2021 22:47
-
-
Save dresswithpockets/a3dc2db98370e872a10e0862927d1109 to your computer and use it in GitHub Desktop.
Working in C# like its Go
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 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