demndev
demndev
CC#
Created by demndev on 4/15/2023 in #help
✅ ef core can't translate a query
query:
var username = "some username";
var user = await _db.Users
.FirstOrDefaultAsync(u => u.Username.Value == username);
var username = "some username";
var user = await _db.Users
.FirstOrDefaultAsync(u => u.Username.Value == username);
UserConfiguration:
public void Configure(EntityTypeBuilder<UserEntity> builder)
{
builder.HasKey(u => u.Id);

builder.Property(u => u.Username)
.HasConversion(
u => u.Value,
value => new Username(value));

builder.Property(u => u.Password)
.HasConversion(
p => p.Value,
value => Password.FromValue(value));
}
public void Configure(EntityTypeBuilder<UserEntity> builder)
{
builder.HasKey(u => u.Id);

builder.Property(u => u.Username)
.HasConversion(
u => u.Value,
value => new Username(value));

builder.Property(u => u.Password)
.HasConversion(
p => p.Value,
value => Password.FromValue(value));
}
Password VO:
public class Password
{
public string Value { get; private set; }
private readonly IPasswordHasher _passwordHasher;

private Password(string value)
{
Value = value;
_passwordHasher = null!;
}

private Password(string value, IPasswordHasher passwordHasher)
{
_passwordHasher = passwordHasher;
Value = value;
}

public static Password Create(string value, IPasswordHasher passwordHasher)
{
Validate(value);

return new(passwordHasher.GetHash(value), passwordHasher);
}

public static Password FromValue(string value)
{
return new Password(value);
}

public void Update(string newValue)
{
Validate(newValue);

Value = _passwordHasher.GetHash(newValue);
}

private static void Validate(string value)
{
if (value.Length is not (<= 100 and >= 8))
throw new Exceptions.InvalidPasswordLengthException();
}
}
public class Password
{
public string Value { get; private set; }
private readonly IPasswordHasher _passwordHasher;

private Password(string value)
{
Value = value;
_passwordHasher = null!;
}

private Password(string value, IPasswordHasher passwordHasher)
{
_passwordHasher = passwordHasher;
Value = value;
}

public static Password Create(string value, IPasswordHasher passwordHasher)
{
Validate(value);

return new(passwordHasher.GetHash(value), passwordHasher);
}

public static Password FromValue(string value)
{
return new Password(value);
}

public void Update(string newValue)
{
Validate(newValue);

Value = _passwordHasher.GetHash(newValue);
}

private static void Validate(string value)
{
if (value.Length is not (<= 100 and >= 8))
throw new Exceptions.InvalidPasswordLengthException();
}
}
32 replies
CC#
Created by demndev on 4/9/2023 in #help
✅ can't map an entity to DB
i'm getting an error on adding a mapped domain entity
9 replies
CC#
Created by demndev on 4/7/2023 in #help
The best place to handle domain exceptions in an MVC app?
17 replies
CC#
Created by demndev on 3/15/2023 in #help
Error during SignalR integration test: The 'InvokeCoreAsync' method cannot be called if the connecti
I get the exception System.InvalidOperationException: The 'InvokeCoreAsync' method cannot be called if the connection is not active after running this test:
public class HelpCommandTests
{
[Fact]
public async Task HelpCommand_Success()
{
const string command = "/help";
var response = string.Empty;
var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddSignalR();
})
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chatHub");
});
});

var server = new TestServer(webHostBuilder);
var connection = new HubConnectionBuilder()
.WithUrl(
"http://localhost/chatHub",
o => o.HttpMessageHandlerFactory = _ => server.CreateHandler())
.Build();

await connection.StartAsync();

await connection.InvokeAsync("Command", command);

connection.On<string>("Receive", msg =>
{
response += msg;
});
}
}
public class HelpCommandTests
{
[Fact]
public async Task HelpCommand_Success()
{
const string command = "/help";
var response = string.Empty;
var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddSignalR();
})
.Configure(app =>
{
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chatHub");
});
});

var server = new TestServer(webHostBuilder);
var connection = new HubConnectionBuilder()
.WithUrl(
"http://localhost/chatHub",
o => o.HttpMessageHandlerFactory = _ => server.CreateHandler())
.Build();

await connection.StartAsync();

await connection.InvokeAsync("Command", command);

connection.On<string>("Receive", msg =>
{
response += msg;
});
}
}
BUT when I remove this peace of code:
typeof(ITerminalCommand).Assembly.GetTypesAssignableFrom<ITerminalCommand>().ForEach(s =>
{
services.AddScoped(typeof(ITerminalCommand), s);
});
typeof(ITerminalCommand).Assembly.GetTypesAssignableFrom<ITerminalCommand>().ForEach(s =>
{
services.AddScoped(typeof(ITerminalCommand), s);
});
all works fine.
3 replies
CC#
Created by demndev on 2/26/2023 in #help
❔ issue with central package management
40 replies
CC#
Created by demndev on 2/20/2023 in #help
✅ Build failed on `dotnet-ef database update`
12 replies
CC#
Created by demndev on 1/30/2023 in #help
✅ How can I improve this code?
It seems that I should change the way tags are stored. Or should I not?
public async Task<GetNotesByTagsQueryResult> Handle(GetNotesByTagsQuery query, CancellationToken ct)
{
var notes = new List<GetNotesByTagsQueryResultItem>();

foreach (var excludedTag in query.ExcludedTags)
{
foreach (var includedTag in query.IncludedTags)
{
notes = await _db.Notes
.Where(n => n.Tags.Contains(includedTag) && !n.Tags.Contains(excludedTag))
.ProjectToType<GetNotesByTagsQueryResultItem>()
.Distinct()
.ToListAsync(ct);
}
}

return new GetNotesByTagsQueryResult { Notes = notes };
}
public async Task<GetNotesByTagsQueryResult> Handle(GetNotesByTagsQuery query, CancellationToken ct)
{
var notes = new List<GetNotesByTagsQueryResultItem>();

foreach (var excludedTag in query.ExcludedTags)
{
foreach (var includedTag in query.IncludedTags)
{
notes = await _db.Notes
.Where(n => n.Tags.Contains(includedTag) && !n.Tags.Contains(excludedTag))
.ProjectToType<GetNotesByTagsQueryResultItem>()
.Distinct()
.ToListAsync(ct);
}
}

return new GetNotesByTagsQueryResult { Notes = notes };
}
35 replies
CC#
Created by demndev on 1/27/2023 in #help
✅ Cannot resolve scoped service...
5 replies