Klarth
Klarth
CC#
Created by Tom on 12/21/2024 in #help
✅ Disposing dbContext with dependency injection
It's still best practice to .ConfigureAwait(false) each call in a library.
28 replies
CC#
Created by Tom on 12/21/2024 in #help
✅ Disposing dbContext with dependency injection
Granted, ASP.NET Core doesn't have a SynchronizationContext, so that happens no matter what. Well, unless you create your own SynchronizationContext there for some reason.
28 replies
CC#
Created by Tom on 12/21/2024 in #help
✅ Disposing dbContext with dependency injection
So say AddCompletions is a library method (it effectively is), then you want .ConfigureAwait(false) on each call so it can resume on any available thread.
28 replies
CC#
Created by Tom on 12/21/2024 in #help
✅ Disposing dbContext with dependency injection
When you await the call on the GUI side (without .ConfigureAwait(false)), the continuation will run on the UI thread.
28 replies
CC#
Created by Tom on 12/21/2024 in #help
✅ Disposing dbContext with dependency injection
Nevermind on that.
28 replies
CC#
Created by Tom on 12/21/2024 in #help
✅ Disposing dbContext with dependency injection
Correct, though I probably should have omitted it for the SaveChangesAsync at the end.
28 replies
CC#
Created by Tom on 12/21/2024 in #help
✅ Disposing dbContext with dependency injection
Oh, maybe not. await using is a lot older than I thought. C# 8.
28 replies
CC#
Created by Tom on 12/21/2024 in #help
✅ Disposing dbContext with dependency injection
I'm pretty sure await using wasn't a feature when this was made.
28 replies
CC#
Created by Tom on 12/21/2024 in #help
✅ Disposing dbContext with dependency injection
And for the DI configuration side (though you don't need NodaTime):
public void ConfigureDbContext(IServiceCollection services)
{
services.AddDbContextFactory<SomeContext>(
options => options.UseSqlite(_connectionString, x => x.UseNodaTime())
);
}
public void ConfigureDbContext(IServiceCollection services)
{
services.AddDbContextFactory<SomeContext>(
options => options.UseSqlite(_connectionString, x => x.UseNodaTime())
);
}
28 replies
CC#
Created by Tom on 12/21/2024 in #help
✅ Disposing dbContext with dependency injection
It's not. I use that approach in an Avalonia app. eg.
private readonly IDbContextFactory<SomeContext> _dbFactory;

public CompletionService(IDbContextFactory<SomeContext> dbFactory)
{
_dbFactory = dbFactory;
}

public async Task AddCompletion(string familyName, string objectiveName, Instant completionTime)
{
using var context = _dbFactory.CreateDbContext();

var family = await GetOrCreateFamily(context, familyName).ConfigureAwait(false);
var objective = await GetOrCreateObjective(context, objectiveName, false, family).ConfigureAwait(false);
objective.LastCompletion = completionTime;

var completion = new Completion()
{
Family = family,
Objective = objective,
CompletionTime = completionTime
};

context.Completions.Add(completion);
await context.SaveChangesAsync().ConfigureAwait(false);
}
private readonly IDbContextFactory<SomeContext> _dbFactory;

public CompletionService(IDbContextFactory<SomeContext> dbFactory)
{
_dbFactory = dbFactory;
}

public async Task AddCompletion(string familyName, string objectiveName, Instant completionTime)
{
using var context = _dbFactory.CreateDbContext();

var family = await GetOrCreateFamily(context, familyName).ConfigureAwait(false);
var objective = await GetOrCreateObjective(context, objectiveName, false, family).ConfigureAwait(false);
objective.LastCompletion = completionTime;

var completion = new Completion()
{
Family = family,
Objective = objective,
CompletionTime = completionTime
};

context.Completions.Add(completion);
await context.SaveChangesAsync().ConfigureAwait(false);
}
28 replies
CC#
Created by OrangeHokage on 12/17/2024 in #help
XAML UI Preview on Visual Studio
Running the app + using XAML Hot Reload is a much better dev experience than previewers...which are only as good as the data you feed it.
13 replies
CC#
Created by OrangeHokage on 12/17/2024 in #help
XAML UI Preview on Visual Studio
XAML can pull from many sources, including from objects created by C# at runtime. So it is hard to keep a designer/previewer happy and usable even if one exists for your GUI framework.
13 replies
CC#
Created by WantToBeeMe on 12/13/2024 in #help
From WPF to Avalonia
If the plan is to keep the WPF client for some time, then you really want to isolate the business logic first, IMO.
26 replies
CC#
Created by WantToBeeMe on 12/13/2024 in #help
From WPF to Avalonia
4. Once ported, then remove WPF stuff from the WPF project. And move business logic into its own project as its essentially isolated at this point.
26 replies
CC#
Created by WantToBeeMe on 12/13/2024 in #help
From WPF to Avalonia
Isolating it into its own reusable project (within the same solution) is best. If you can't do that, then the best you can do is: 1. create separate Avalonia project. 2. reference business + WPF project from it. 3. hope that the isolation works.
26 replies
CC#
Created by WantToBeeMe on 12/13/2024 in #help
From WPF to Avalonia
That's a pretty big architectural flaw to have backend/domain side-by-side with your GUI app.
26 replies
CC#
Created by WantToBeeMe on 12/13/2024 in #help
From WPF to Avalonia
Is the backend code in its own project?
26 replies
CC#
Created by WantToBeeMe on 12/13/2024 in #help
From WPF to Avalonia
That's how I did my last port, but that was approx 2 years ago and in VS.
26 replies
CC#
Created by WantToBeeMe on 12/13/2024 in #help
From WPF to Avalonia
I don't mean different solution, I mean different project. ie. YourApp.Wpf and YourApp.AvaloniaUI in the same solution.
26 replies
CC#
Created by WantToBeeMe on 12/13/2024 in #help
From WPF to Avalonia
It seems like too niche of a scenario for JetBrains to add specific support for, but you could always open a ticket for them to see if there are any mitigations.
26 replies