Questions tagged [c#]

C# is a multiparadigm, managed, garbage-collected object-oriented programming language created by Microsoft in parallel with the .NET platform

Filter by
Sorted by
Tagged with
3 votes
1 answer
214 views

Posts and Comments - how should I define them in domain driven design?

I am designing a blog in domain driven design. I don't know how I should define models: Post and Comment. My database: A user can see a post and related comments. So I think that Post model can be ...
user avatar
-1 votes
1 answer
167 views

Two layers of DTO's for API's with Entity Framework

I am building an API which, amongst other things, needs to handle users. I am building it with C# 10 and .NET 6. In order to receive/return data in the API end points, I am using records. So for ...
user avatar
0 votes
4 answers
168 views

Adapter or pure interfaces?

I got a bit strange 'future ready' scenario. And I'm not sure if I got it right in regards to C# adapter design pattern. The scenario is that to be future ready, 'to use the adapter pattern' to easily ...
user avatar
  • 137
1 vote
2 answers
299 views

Should domain layer have validation logic? [duplicate]

Let's say I have a User class described as follows: record User ( Guid Id, string Username, string Password ) { /* some methods */ }; Let's say Usernames and Passwords need at the very least ...
user avatar
2 votes
2 answers
199 views

How to split logging into streams?

I am looking for an advice (or an example) on how to organize logging streams in my cloud app (C#). In my app there are logging events related to the application infrastructure level and I want to ...
user avatar
4 votes
2 answers
2k views

What does "set" mean in programming languages like C#?

I've been reading a book about C#. What does the word 'set' mean in the following excerpt? Pattern matching with the switch statement: Like the if statement, the switch statement supports pattern ...
user avatar
  • 75
1 vote
0 answers
103 views

ASP .NET Core 6 - .razor components - Routing

I come from a background in ASP.NET MVC 5 on .NET 4.8. I've been trying to wrap my head around the new razor pages, and can't seem to get routing to work, or understand it, even. I don't want to use ...
user avatar
0 votes
2 answers
105 views

Make date range generator interface stateful or stateless?

An app has a feature that's much like any calendar application (like the Outlook calendar for example). Consequently, I need to do a lot of date logic. I wrote a class with date calculation methods ...
user avatar
1 vote
1 answer
177 views

How to test around a giant static class?

I am dealing with an old .net code base which has a PrivilegeChecker static class with hundreds of static methods, each of which takes in some user id and some other params, and then fetches some info ...
user avatar
  • 196
0 votes
1 answer
163 views

How did you work around covariant return types? Before the language allowed it?

I need some help with design logic, illustrated by an example. My question below. public class ChangeSet { } public class NodeChangeSet : ChangeSet { } public abstract class Base { protected ...
user avatar
  • 113
2 votes
3 answers
350 views

Should you use interfaces or classes in database entities?

Let's say I'm making a library database with books and authors, and I want to model the domain with interfaces, should I also specify the relations in the interfaces? For example, if I have the ...
user avatar
-2 votes
1 answer
119 views

What is the right approach for dynamically creation of SQL-queries?

The problem: I have a application where the data comes from SQL tables. The design of the tables differs a bit from client to client. Example: ClientA sells thermometers and the product has features ...
user avatar
1 vote
1 answer
205 views

Circular references with generic classes

I'm trying to create an interaction system for a game I'm developing with Unity and C# and I've been struggling with it for a while now. It consists of interactors and interactables. The idea is that ...
user avatar
0 votes
1 answer
78 views

Compile-time type-safe access to collection with polymorphic items, and extending it

I would like to have type-safe access to items in a collection with polymorphic items at compile-time. For this, I set up the code below as a proof of concept. Passing the DataItemProps instances to ...
user avatar
2 votes
2 answers
208 views

How could I extend these methods that use flexible arguments?

Context Let's say I have a Shape class (I'll use C# for the code snippets) that represents a 2D shape, like a triangle or a circle. These shapes have an area, so I'll include a method for calculating ...
user avatar
2 votes
2 answers
127 views

Implementing a method to achieve faster image acquisition

I hope I can explain my question clearly. I'm planning to do some real time 2D grayscale image acquisition, and after reading some articles, I noticed that many are using buffers or multithreading or ...
user avatar
  • 129
0 votes
0 answers
41 views

Handling Events Across Multi-Server Application

My situation is that I have an application suite that runs across several potential server configurations, which include web-site servers, windows-service servers, and web-service servers. An example ...
user avatar
3 votes
2 answers
507 views

Is inheritance constraint on Generics redundant? If not what is its recommended use?

I've recently found myself using a generics with constraint that a type should inherit from a specific base class, but now I've just realised that this is redundant and unnecessary because the ...
user avatar
1 vote
2 answers
71 views

Should I avoid putting Id's in a contract/response model?

Here is the scenario: Let's assume that we have a Telephone and a Driver. Here is the definition of the classes (Pseudocode): Telephone: { Id: number, [Unique] TelephoneNumber: string } Driver: { ...
user avatar
  • 109
0 votes
4 answers
148 views

Should I Decompose Client Classes?

I'm writing an SDK for the European Space Agency's DISCOs API. This has around 10 endpoints with different sets of query parameters, each of which returns a different payload type with different link ...
user avatar
-3 votes
3 answers
174 views

C# implement .bat file functionality, call bat file or implement from scratch in C#? [closed]

I have a bunch of scripts written in .bat files that I'm trying to implement into one big program using C#. These .bat files mostly do simple operations like copying files into network drives etc. ...
user avatar
  • 115
-2 votes
1 answer
107 views

How often should I request a token when communicating with a third-party API?

I have an ASP.net web API. I would like to call another API that has an authentication token inside my API. The code flow is as follows: My API is called → Get authentication token from 3rd party API →...
user avatar
-2 votes
2 answers
290 views

Best practices around writing testable extension methods

I’m currently working in C# and I’d like to write and extension method against a type. There is a small amount of repeated logic between classes surrounding JSON deserialization, using the ...
user avatar
  • 1
0 votes
3 answers
121 views

UpCasting DownCasting vs HighLevel modules/LowLevel modules

I am confused with wording used to describe UpCasting/DownCasting compared to wording used to describe level of modules. Imagine two classes: public class Human { } public class Doctor : Human { } ...
user avatar
  • 21
0 votes
0 answers
98 views

Improving performance of 'Weighted/prioritized left shuffle algorithm'

I wrote a prioritized/weighted left shuffle algorithm (the code is copied from my open source C# project Fluent Random Picker). What does that mean? You've got some values and each of them has a ...
user avatar
  • 101
0 votes
2 answers
238 views

Is there a reason to not use a guard check using params instead of singular checks?

I've been refactoring guard statements for my project to simply use a custom Ensure class with 2 methods. Some methods require multiple null checks and I implemented a guard method that simply accepts ...
user avatar
1 vote
1 answer
119 views

How do I share translations between a .NET application and a react application?

I currently have two applications where one is a landing page (built in react) and another is a web page that lets users explore data (Written in ASP.NET 4.7). I'm currently using resx files to handle ...
user avatar
  • 121
3 votes
3 answers
293 views

Dispose pattern without unmanaged objects

I've used the wellknown Dispose Pattern in .NET several times as described in the official docs. Now I have the following, rather simplified code: public class FirstForeignDependency : IDisposable { ...
user avatar
  • 139
1 vote
0 answers
59 views

Issue implementing the Command Processor pattern using MVVM for WPF?

I'm trying to implement the Command Processor pattern using MVVM for WPF. The Command Processor pattern that I'm roughly following is the one described in the book Pattern-Oriented Software ...
user avatar
  • 133
-1 votes
2 answers
128 views

Instantiating objects inline or not

I usually instantiate arrays inline, because I think it's cleaner. But I've seen a lot of people instantiating them when they're first needed. Is there any reason to do so, except for style? This ...
user avatar
1 vote
2 answers
205 views

Lockfree customer producer problem

We have following class: class ProcessStore { ConcurrentDictionary<int, MyDisposableClass> _processes = new(); bool _disposed; void Store(int Id) { if(_disposed) { throw; } ...
user avatar
  • 363
2 votes
1 answer
121 views

Data visualisation application for WPF and MVVM

I was wondering what is the best approach for a Data visualisation application for WPF and MVVM The key points are: The data is loaded, or needs just a little preparation such as pulling out ...
user avatar
0 votes
0 answers
174 views

Where Refit interfaces fit in a Clean Architecture structure

I want to start using Refit, it is a great library, but I'm having some hard time to keep the project structure clean because the library is at the same time the abstraction(interface) and the ...
user avatar
  • 101
0 votes
1 answer
90 views

Can resource which requires asynchronous cleanup be constructed synchronously?

More specifically this applies only to resources which have asynchronous dependencies themselves (but I think that's majority of them). Concrete example: class Foo : IAsyncDisposable { public ...
user avatar
  • 363
2 votes
1 answer
68 views

How to set up MVVM for highly dependent hierarchical objects with 3D representation

Imagine a program that designs plates with geometric cutouts. You start with a rectangular plate. A plate can have one or more rectangular depressions carved out of it. Each of these can have one or ...
user avatar
  • 121
1 vote
2 answers
229 views

C# WPF - Is it acceptable to have a dependency to PresentationCore.dll in domain layer?

We are working on a .NET desktop application written in C# and WPF, that communicates with a REST API server. The software has a layered design and consists of multiple projects. One project for the ...
user avatar
2 votes
3 answers
278 views

Handling exogenous exceptions in layered architecture

Exogenous exceptions are that kind of exception that are unavoidable because they are throwed outside of our system's pristine logic. A good practice is to throw wrapped exceptions in lower layers ...
user avatar
  • 21
0 votes
0 answers
80 views

Designing algorithm to use available space in signup form in most efficient manner

The case is that I have a signup form for students where they can choose between different lessons in multiple rounds. Each lesson in each round has a defined available space. The total amount of ...
user avatar
1 vote
0 answers
93 views

How to improve the coding quality in a problematic company [duplicate]

Due to circumstances I'm now working in a small software company. We make e-mail marketing management software and serve some big customers as our national railways and a big bank. The software has ...
user avatar
0 votes
1 answer
421 views

Mastering the usage of [PublicAPI] attributes?

Take the following class, without [PublicAPI] attribute, ReSharper says that all public members can be made private. using System; using JetBrains.Annotations; namespace ObjMtl { // [PublicAPI] ...
user avatar
  • 727
0 votes
1 answer
347 views

C# Kafka Consumer design

I have a C#-based system that works relatively well. It reads data from kafka, processes the data and then writes it out to MSSQL. The kafka topic has been partitioned into 10 partitions - and after 2 ...
user avatar
0 votes
0 answers
138 views

Microservice code reuse strategy for static classes

Hope this is the right place to post an opinion-based question like this. I'm in the process of converting an aging API monolith to a set of .NET 5/C#-based microservices and working on the code reuse ...
user avatar
1 vote
1 answer
188 views

Performing many SQL inserts quickly without using BulkCopy

I have a C# system that reads thousands of messages per minute from a Kafka topic. The messages are filtered and I need to insert the filtered messages into a MSSQL table. A normal insert statement is ...
user avatar
0 votes
2 answers
153 views

Is it safe to use the same IoC container for your own and third-party services?

I am developing an application with ASP.NET Core with the ability to dynamically add new libraries that implement the necessary functions. They can be included at the start. I have already written ...
user avatar
0 votes
3 answers
169 views

Critical section with two different "rights of way" [closed]

In C#, how do I handle critical section with two different "rights of way"? Theoretical use case: imagine a swimming pool (the resource). Many individual swimmers (worker threads A, B, C, ...
user avatar
  • 119
45 votes
9 answers
15k views

What's wrong with returning null?

I've recenlty been greeted by CS8603 - Possible null reference return, which indicates that my code could possibly return null. It's a simple function that looks up an entity in a database by id - if ...
user avatar
  • 649
3 votes
4 answers
504 views

C# How to avoid memory leak in this case

I've got a Window, which gets a viewmodel instance injected into its constructor. The window sets its DataContext to that viewmodel. The viewmodel contains a command to "submit" the window ...
user avatar
2 votes
3 answers
391 views

Best way to generically convert SQL-Based classes to DTO using Class Name in C#?

I have a collection of classes in a SQL database that all share the same structure, and cannot be changed. They are similar to the example below. I am using Entity Framework Core and would like to ...
user avatar
26 votes
5 answers
5k views

Why use the discard variable in C#?

When coding resharper recommends that if you're to discard or ignore the return of a method, that you use this syntax: _ = TheMethodICouldCareLessAboutTheReturnValue(); I know you could just call it ...
user avatar
1 vote
0 answers
93 views

How to improve maintainability and testability of a service by refactoring it into a functional style

A customer of ours has a rather large (500+ LOC) service as central part of the project. Written in C# and consisting of rather clunky large imperative style functions, I was asked to improve the test ...
user avatar

1
2
3 4 5
96