Skip to content

Instantly share code, notes, and snippets.

View rodolfograve's full-sized avatar

Rodolfo Grave rodolfograve

View GitHub Profile
@rodolfograve
rodolfograve / storageaccount-private-networkAcls-azurepolicy.json
Created September 17, 2020 16:53
An Azure Policy to make sure Storage Accounts have the right networkAcls, without forcing the creators to specify the whole list of IPs
{
"mode": "All",
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Storage/storageAccounts"
},
{
@rodolfograve
rodolfograve / AspNetCoreTest.cs
Last active September 30, 2017 21:25
Testing ASP.NET Core services
public async Task WhenXThenY(
IRepository<SomeEntity> repository, // For this to work, a TestServer must have been started and
// this IRepository must be the same instance that the Controller is going to use when processing the request
WebApiClient client, // This must be wrapping an HttpClient connected to the TestServer
ISomeExternalServiceGateway gateway // Must also be the same instance to be used by the Controller)
{
var entityInRequiredState = CreateEntityInStateX();
repository.GetById(entityInRequiredState.Id).Return(entityInRequiredState);
await client.Post($"controller/action/{entityInRequiredState.Id}");
entityInRequiredState.ShouldBeInStateY();
@rodolfograve
rodolfograve / LINQ based Unit of Work implementation
Created January 4, 2014 21:46
My implementation of the Unit of Work pattern so that you can: - Realize the full power of LINQ by keeping your code data-store agnostic, leaving all SQL translation to the LINQ provider. - Run integration/functional/acceptance tests in InProcess mode (no database access, very very fast... think NCrunch)
public interface IUnitOfWork : IDisposable
{
void Add<T>(T newItem) where T : class;
IRepository<T> All<T>() where T : class;
void SaveChanges();
}
public interface IRepository<T> : IQueryable<T> where T : class
{
void Add(T newItem);
@rodolfograve
rodolfograve / Application service method
Created January 4, 2014 20:58
Ideal structure of an application service/message handler method
public void EntryPointForSomeUseCaseWhichPrincipalEntityIsBook(Guid id, string otherParam)
{
using (var unitOfWork = unitOfWorkFactory.Create())
{
var book = unitOfWork.All<Book>().WithId(id);
book.DoSomething(otherParam);
uow.SaveChanges();
}
}
// Verify that a controller returns a redirect to a Single Action Controller
result.AssertIsRedirectToRoute().WithSingleActionController(typeof(ShowList));
// Verify that a Single Action Controller returns a View
var sut = new Some.Controllers.ShowList(with, its, dependencies);
var request = new ShowListRequest();
var result = sut.Execute(request);
result.AssertIsView()
.WithName("_ListPartial")
.WithModel<ListViewModel>(p =>
{
p.Property1.Should().Be(1);
@rodolfograve
rodolfograve / gist:5138291
Created March 11, 2013 22:07
Examples of how to use strongly-typed methods to generate links and URLs with Single Action Controllers in TEAM.Commons.Web
// Create a link to SingleActionController
@Html.ActionLink("Link text", typeof(Type.Of.The.SingleActionController))
// Create a link to SingleActionController with a request and attributes
@Html.ActionLink("Another link text", typeof(Type.Of.The.SingleActionController),
routeValues: new ShowListRequest
{
Property1 = Model.Property1,
AnotherProperty = Model.AnotherProperty
},