Anton
Anton
CC#
Created by Anton on 10/16/2023 in #help
❔ System.IO.Pipelines cannot complete the reader
No description
21 replies
CC#
Created by Anton on 6/23/2023 in #help
❔ Rider accepting completion deletes the next word
When I hit tab in Rider to accept a suggestion, it overwrites everything that comes after. Just like it does in this vid at the 15th second https://youtu.be/wCllU4YkxBk How do I disable that?
16 replies
CC#
Created by Anton on 3/31/2023 in #help
❔ Replacing joins to a table with a CTE in EF Core
Say I generate my sql with ef core. Is there a way to make it do joins on the CTE rather than the original table? So for example if I do a
context.Owners
.Select(o => new
{
Id = o.Id,
Projects = o.Projects,
});
context.Owners
.Select(o => new
{
Id = o.Id,
Projects = o.Projects,
});
Can I make the join with projects use a CTE, so it looks kinda like this:
context.Owners
.SubstituteJoins(context.Projects, p => p.Where(p => p.Name.Contains(" ")))
.Select(o => new
{
Id = o.Id,
Projects = o.Projects,
})
context.Owners
.SubstituteJoins(context.Projects, p => p.Where(p => p.Name.Contains(" ")))
.Select(o => new
{
Id = o.Id,
Projects = o.Projects,
})
Generating a query that's similar to this:
WITH view AS (
SELECT *
FROM projects
WHERE name LIKE '% %'
)
SELECT *
FROM owner
LEFT JOIN view ON view.ownerId = owner.id;
WITH view AS (
SELECT *
FROM projects
WHERE name LIKE '% %'
)
SELECT *
FROM owner
LEFT JOIN view ON view.ownerId = owner.id;
To be clear, what I want is for all joins with the projects table to be substituted for a join to a CTE. Since I think EF Core cannot generate CTE's, is there a way to just force it to replace joins with the Projects table to view, which I would then append to the SQL query as text? I guess an idea is to replace the strings which reference the Projects table after it generates the SQL, but is there a way to do this at one higher level of abstraction? Is my idea flawed, or is there a simpler way, you think? The same can kinda be achieved with nested queries on all nested fields, but my problem with this is that it requires redoing the projections. Thinking of the Projects table as a view feels simpler in this case.
3 replies
CC#
Created by Anton on 3/28/2023 in #help
❔ HotChocolate with IQueryable, apply a required filter on the ef entity
Basically, I'm trying to find a way to configure an IObjectFieldDescriptor by adding a required argument to it, which should in turn apply a filter on the database entity. In this case, I'm adding a companyId argument, and then applying the respective filter manually, before mapping. What should I do to configure this in a simpler way? Let me show you what I have and then what I mean by a better way. This is what I have so far. Don't mind the FieldCollectionQuery and UseBuiltinMiddleware extension methods, they are there to set up the ef core stuff. The argument is the important bit right now.
public sealed class QueryType : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
{
descriptor
.FieldCollectionQuery<SupplierGraphDtoType>("suppliers")
.Argument("companyId", a => a.Type<NonNullType<IntType>>())
.UseBuiltinMiddleware<SupplierGraphDtoType>(MiddlewareFlags.All)
.Resolve(ctx =>
{
var q = ctx.DbContext<DataContext>()
.Set<Supplier>()
.AsQueryable();

int companyId = ctx.ArgumentValue<int>("companyId");
q = q.Where(s => s.CompanyId == companyId);

return q.ProjectTo<Supplier, SupplierGraphDto>(ctx);
});
}
}
public sealed class QueryType : ObjectType
{
protected override void Configure(IObjectTypeDescriptor descriptor)
{
descriptor
.FieldCollectionQuery<SupplierGraphDtoType>("suppliers")
.Argument("companyId", a => a.Type<NonNullType<IntType>>())
.UseBuiltinMiddleware<SupplierGraphDtoType>(MiddlewareFlags.All)
.Resolve(ctx =>
{
var q = ctx.DbContext<DataContext>()
.Set<Supplier>()
.AsQueryable();

int companyId = ctx.ArgumentValue<int>("companyId");
q = q.Where(s => s.CompanyId == companyId);

return q.ProjectTo<Supplier, SupplierGraphDto>(ctx);
});
}
}
4 replies
CC#
Created by Anton on 3/24/2023 in #help
❔ Blazor WebAssembly CSS
2 replies
CC#
Created by Anton on 3/21/2023 in #help
❔ Using automapper to update EF Core entities recursively
I'm trying the neat idea to try and make a generic create/update method which supports includes. The first branch takes care of inserts. It takes requests of this form (currently doesn't check the nested ids, which it should):
{
"name": "John",
"projects": [
{
"projectName": "A"
},
{
"projectName": "B"
}
]
}
{
"name": "John",
"projects": [
{
"projectName": "A"
},
{
"projectName": "B"
}
]
}
It then maps this dto to a recursively tracked entity. The second branch works with requests of this form, taking care of updates:
{
"id": 1,
"projects": [
{
"id": 7,
"projectName": "Changed"
},
{
// Should not update anything, but try and find the project with this id.
"id": 8
},
{
"projectName": "Q"
}
]
}
{
"id": 1,
"projects": [
{
"id": 7,
"projectName": "Changed"
},
{
// Should not update anything, but try and find the project with this id.
"id": 8
},
{
"projectName": "Q"
}
]
}
Currently I'm doing a bunch of things that are related to nested entities manually. I'm manually sorting the projects in reverse order, so that the projects with null key (projects to be inserted), end up at the end of the list. I'm then querying the entity from the db, checking if all of the projects meant for updating were found, and then I do a recursive copy of the dto into that entity. This maps all properties of the person and of each nested project. At least it should, what actually happens is it copies the null values over as well (see the mapper profile below). Then the tracker picks up on all of the newly mapped projects. My question is, why does automapper map null properties even though it's set to ignore them, and is there a way to generalize this thing? The way I came up with doesn't seem overly complex, it's just not fully generic. I want the both the include filtering and the mappings to work independent of the level of depth of the objects.
21 replies
CC#
Created by Anton on 3/14/2023 in #help
❔ Preventing a panel from overflowing + show scrollbar WPF
I have a situation like this
<StackPanel>
<Grid>
some buttons
</Grid>
<StackPanel> possibly wrapped in ScrollViewer?
variable height content that may not fit
</StackPanel>
</StackPanel>
<StackPanel>
<Grid>
some buttons
</Grid>
<StackPanel> possibly wrapped in ScrollViewer?
variable height content that may not fit
</StackPanel>
</StackPanel>
The problem is that the bottom panel overflows on the y axis. Solutions online suggest setting it to the width of the container like here https://stackoverflow.com/questions/47333220/wpf-scrollviewer-content-overflowing which I obviously can't do. How do I hide the overflow? I want it to take all available space on the Y axis, but no more than the screen height.
11 replies
CC#
Created by Anton on 3/14/2023 in #help
❔ WPF a control to display an arbitrary object
It should support complex objects with multiple properties, int, string, and IEnumerable's of these. It's fine if it doesn't support nested collections. If the question is not clear, tell me to clarify it.
9 replies
CC#
Created by Anton on 2/23/2023 in #help
❔ WPF draw over other arbitrary controls
I want to draw over items contained in an ItemsControl, without wrapping the ItemsControl into any sort of other object. Can anyone suggest a starting point / best practice? With my knowledge of WPF I don't know where to start solving this one.
53 replies
CC#
Created by Anton on 2/18/2023 in #help
❔ Is something like this possible with the default DI package?
So what I want to do is like this How scopes work normally: - global scope - scope 1 * scoped service A * scoped service B - scope 2 * scoped service A (different instance) * scoped service B (different instance) What I want is to be able to resolve scoped services of the parent from a subscope, but create new services for some types in subscopes (are there even subscopes?) - global scope - scope 1 * service A - scope 1.1 * service A resolved to the one of the parent * service B - scope 1.2 * different instance of service B I've seen a lib allow this, but it's for Unity. They did it through custom scope classes. Autofac might allow this too? I'm not sure.
4 replies
CC#
Created by Anton on 2/17/2023 in #help
❔ Dotnet NuGet fetch packages with documentation
I can set <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> in the project file to enable copying the nuget packages on build. This makes the build in essence equivalent to publish. What I want, however, is for it to copy the xmls with the documentation instead, be it in publish or on build, I don't care. These xmls with documentation are located next to the dlls in the nuget packages. What property do I have to set to enable this?
5 replies
CC#
Created by Anton on 1/26/2023 in #help
❔ Multiple configurations with automapper
I have two automapper configurations, the first one is used to project entities to dtos when querying ef core normally or when just mapping objects, while the second one is for use with OData. A while back I have asked about conventions of service configuration, and the answer was quite different from my intuitive solution. So what I'm asking here is what's the conventional way of registering multiple automapper configurations? A factory by name or index does not seem to be officially supported by the public API, should I make a factory myself? Maybe some other library or option exists?
33 replies
CC#
Created by Anton on 1/20/2023 in #help
❔ Resolve a service before the container is built
Is it possible? My use case is that I want to share an object when I'm configuring the services. This question is ungooglable in the way I describe it
51 replies
CC#
Created by Anton on 1/20/2023 in #help
❔ API DI advice
I want to implement a configurable injected class via an extension method, using the options system for the configuration. By looking through some code, I gathered that by doing .Configure<Options> you can chain configuration actions, which will execute when the container is being built. When the service that requires these options is instantiated, it would get the options snapshot from the service provider as a service. However, what do I do if I still want to use this system, chaining the repeated actions, but I also want to register more services from these actions? How do I reference the service collection? Do I keep a reference to it in the options class? That's not it, right? Basically, I need to register a few dependencies while the service is being configured from those actions. Also, I've seen a library use ServiceDescriptor added explicitly, but the code there is really funky, I don't understand how it works (it being encumbered with abstractions does not help).
26 replies
CC#
Created by Anton on 1/18/2023 in #help
❔ Is there some generic type for factories in asp net core?
Making such a factory kinda requires a bunch of code, and I haven't even made the standard fluent configuration helpers. Are there classes that do this generically in asp net core?
public interface ISomethingFactory
{
ISomething Create(string schemeName);
}

public class SomethingFactory : ISomethingFactory
{
public class Configuration
{
public Configuration(IReadOnlyDictionary<string, Type> providers)
{
Providers = providers;
}

public IReadOnlyDictionary<string, System.Type> Providers { get; }
}

private readonly Configuration _configuration;
private readonly ServiceProvider _serviceProvider;

public SomethingFactory(Configuration configuration, ServiceProvider serviceProvider)
{
_configuration = configuration;
_serviceProvider = serviceProvider;
}

public ISomething Create(string schemeName)
{
if (!_configuration.Providers.TryGetValue(schemeName, out var providerType))
throw new ArgumentException($"No provider found for scheme {schemeName}");

return (ISomething) _serviceProvider.GetService(providerType);
}
}
public interface ISomethingFactory
{
ISomething Create(string schemeName);
}

public class SomethingFactory : ISomethingFactory
{
public class Configuration
{
public Configuration(IReadOnlyDictionary<string, Type> providers)
{
Providers = providers;
}

public IReadOnlyDictionary<string, System.Type> Providers { get; }
}

private readonly Configuration _configuration;
private readonly ServiceProvider _serviceProvider;

public SomethingFactory(Configuration configuration, ServiceProvider serviceProvider)
{
_configuration = configuration;
_serviceProvider = serviceProvider;
}

public ISomething Create(string schemeName)
{
if (!_configuration.Providers.TryGetValue(schemeName, out var providerType))
throw new ArgumentException($"No provider found for scheme {schemeName}");

return (ISomething) _serviceProvider.GetService(providerType);
}
}
8 replies
CC#
Created by Anton on 1/15/2023 in #help
❔ Authentication scheme name
Is there a way to determine which authentication scheme was used to authenticate the request within an endpoint?
2 replies
CC#
Created by Anton on 1/14/2023 in #help
❔ EF Core migrations
Is it normal that when I do ef database update with an unapplied migration, it tries applying all migrations from the start, even though the database already has all of the migrations but the last one? Like it even has a table with migration history so it should automatically detect which migrations haven't been applied yet. Or am I wrong?
32 replies
CC#
Created by Anton on 12/4/2022 in #help
❔ Azure Key Vault for local development
I need an oauth secret for development, but since storing secrets in source code is a bad practice, and it's clear to me why, I thought I'd use a service for this. The only free* service that I found is Azure Key Vault. Now I'm wondering how to connect it to the dev aspnet core application. - I need it to be easily available on other machines, granted I'm logged in; - It should prompt me to log in if it doesn't have the appropriate permissions to access the keys; - I should be able to manage whom I entrust the keys, so I should be able to authorize specific users manually, given I have access to the account that created the vault. The info I've found so far either throws around various authentication terms, like the RBAC, the active directory, the managed identity, which I haven't worked with and hence I don't know which one I need if any. Most tutorials talk about deploying to Azure, which I don't care about, I only need development access to the vault. Now how I see the ideal experience, is that on restore of the project a command is run, which prompts authentication if required and copies the secrets into the secret manager storage for the project. https://learn.microsoft.com/en-us/aspnet/core/security/app-secrets?view=aspnetcore-7.0&tabs=windows#how-the-secret-manager-tool-works Then this command could be rerun as needed to sync the keys manually. The app then would just use this config and not care about azure. * with the student pack
2 replies
CC#
Created by Anton on 12/3/2022 in #help
❔ How do I implement tabbed interface in WPF?
Like they do in browsers, with each tab splittable into a new window with the ability to turn back into a tab. I'm looking for a free library for this.
4 replies
CC#
Created by Anton on 11/26/2022 in #help
❔ Authorization convention for requests other than get
I want to require authorization for all endpoints that accept request types other than get. By looking through the source code, I've only found AuthorizeFilter. Should I be conditionally adding filters based on the existent filters? but IAuthorizeData is not a metadata filter, so it won't even be in that list. Should I make a custom X (I can't find the type that has to do with policy requirement extraction)?
2 replies