Questions tagged [c#]
C# is a multiparadigm, managed, garbage-collected object-oriented programming language created by Microsoft in parallel with the .NET platform
4,894
questions
1
vote
1
answer
238
views
How can I restore an entity from the database while keeping my entities database-independent?
Background
I'm currently working on a project using domain-driven design and Dapper as my ORM. The entities are naturally slightly different than the tables in which they are stored (e.g. _id is ...
2
votes
2
answers
307
views
What are the risks in eliminating sync over async?
I'm reviewing changes to a widely used library, which are supposed to be refactorings, and so we want to minimize the risk of introducing any accidental regression. Of course, there are changes from ...
-1
votes
1
answer
114
views
Fast methods to consolidate 20 csv files into one xml file [closed]
We have 2 systems Legacy & New and I have to migrate the data from the legacy system to the new system.
The legacy system exports the data in .csv format while the new system accepts the data in ...
1
vote
1
answer
441
views
How can I design a data access layer that connects to two different databases?
I have a C# application that needs to connect to either an Oracle database or a SQLite database. The databases can be considered "identical" - same schema - but users have the ability to &...
10
votes
8
answers
4k
views
What is the purpose of enclosing all return values and arguments of a method in separate classes?
I've seen such a convention. Whenever a public method is declared, two classes are also defined that enclose its return value and parameters like this:
public MethodNameReturnDTO MethodName(...
7
votes
5
answers
665
views
How to initiate unit testing when you're the grunt?
I am the newest programmer on a very small team of 6 developers, 3 of which are offsite. By "newest" I mean both in years of experience (drastically) and familiarity with the code. I've been ...
1
vote
3
answers
816
views
Compute hash that's consistent across program executions
I am creating a unique "key" object type for a tree, we will call this object type a TreeCoordinate.
A tree in this case is a fairly standard mapping of nodes from parent to children where ...
2
votes
1
answer
158
views
How to refactor same block of delegate code into a single private method?
I have these lines in multiple test methods. Now, trying to create a private method to clean up but not sure how to do it. Any suggestion or pointer would be really helpful. Thanks.
var ...
5
votes
2
answers
625
views
Why does C# List<T>.AsReadOnly() allocate?
I am looking at the List<T>.AsReadOnly() method.
Since List<T> itself is a IReadOnlyCollection<T>. (It implements IReadOnlyList<T> and IReadOnlyList<T> implements ...
0
votes
1
answer
172
views
WPF plugin system - Dependency injection in plugin class libraries
The main application loads plugins, list them and when user selects one it is initialized and displayed. Each plugin is a complex class library which contains several views/viewmodels/repositories/etc....
19
votes
8
answers
6k
views
Why assert for null on a object before asserting on some of its internals?
Let's consider the following test.
[Fact]
public void MyTest()
{
// Arrange Code
var sut = new SystemWeTest();
// Act Code
var response = sut.Request();
// Assert
...
0
votes
0
answers
114
views
How to design entities to keep up with the depth of JSON objects?
To better context see Rich Authorization Requests for OAuth2
As u can read here there are some common data field types like actions, locations etc.
So I have created C# entity for EF as below:
public ...
2
votes
5
answers
754
views
Where to control optimistic concurrency in a domain-driven design (DDD) application?
I am thinking how to develop an application in a DDD way, and now I am thinking about the concurrency part.
In some examples I have seen that in the domain classes are injected with dependency ...
0
votes
0
answers
207
views
Howto design a Rest-Client in c# the right way?
apologies, if my question is to trivial. But i after doing some research i couldn't find an appropriate answer to this seemingly simple question. As a developer with some experience, i know that i ...
1
vote
2
answers
371
views
Is it okay to extend more than one class if a new type/feature is needed?
I coudn't find a better phrasing for my question and hope it is not too confusing.
my question mainly targets the open/closed principle and extensibility of my program.
I tried to condense everything ...
0
votes
2
answers
286
views
How to Represent Functional Boolean
.NET Boolean type usually makes if else pair all over the code.
Functional Boolean should be more like Either type. Ideally represented as Either<Unit, Unit>.
However, my issues with Either type ...
0
votes
4
answers
152
views
Microservice arcitecture - seperation of services
The Microservice arcitecture is still a new thing for me to work with, so it gives me some unanswered questions.
In my case each MicroService has its own database.
I need a UserService to handle CRUD ...
1
vote
2
answers
124
views
Designing Products with multiple variations
I have a Product entity:
public class Product
{
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
[ForeignKey("Brand"...
2
votes
1
answer
272
views
How to restrict the construction of a domain object to an external service?
I have this object
RelativeFoo{int relativeCode, Origin relativeTo}
And I want to map it to this other object
AbsoluteFoo{int absoluteCode}
In order to do this, I need to use a service whose ...
2
votes
3
answers
369
views
What should we do in face of a failing sub in pub-sub?
I'm working on a microservices application that is implemented from the ground up with MediatR em CQRS. We have a list of domain events that will be published via MediatR [simple pub-sub library that ...
0
votes
1
answer
185
views
Domain Modeling / Architecture
I'm experimenting with some projects at the moment and I am stuck trying to decide on the correct approach to this question.
Given a simple architecture like this consisting of
Domain - (Person and ...
-1
votes
1
answer
90
views
Injecting Adapters Into Client
So I'm building a client for a third API and I want to be able to Get() and GetMany() for each type this controller can provide.
So I've built this adapter, that provides generically typed methods for ...
2
votes
1
answer
337
views
UML Diagram double arrow
In a Microsoft Learn article on MVVM there is an, what I assume, UML diagram that didn't make sense to me. (https://learn.microsoft.com/en-us/archive/msdn-magazine/2009/february/patterns-wpf-apps-with-...
0
votes
1
answer
521
views
Azure Function App and multiple Functions within it, performance
I am new to Azure Function App. I am using a consumption plan.
Does it make a difference how many functions are in a Function App?
What about if there are functions, such as the service bus triggered ...
0
votes
2
answers
306
views
Azure Function SQL polling Vs Service Bus. How reliable is Service bus? Does it ever fail?
I have scheduling requirements where I place all my schedules in SQL db and then use the azure function every minute to read that database. When the schedule is met, I then perform a durable function ...
15
votes
9
answers
8k
views
Can it be acceptable to construct SQL queries dynamically?
I know that as a general rule, you shouldn't construct SQL queries dynamically because of the possibility of SQL injection.
However, it could come in quite handy to break this rule and define for ...
1
vote
1
answer
341
views
C# Source Generator : which way to go for a simple GitHub Community Project
CONTEXT
My project is about implementing the "conventional method for house-energy-efficiency assessment" in France (120 pages). I want to provide a strict yet easy to use API to this method....
1
vote
3
answers
248
views
Is this really loosely coupled?
I was going through the source code of an existing project and totally confused about one implementation. The project is created in .Net 6 and it's a frontend MVC project. It's part of a microservice ...
0
votes
3
answers
393
views
Best way to provide default objects from a simple class in C#
I have a Material class :
public class Material
{
public string Name { get; private set; }
public double Density { get; private set; }
public double SpecificHeat { get; private set; }
...
1
vote
1
answer
1k
views
Best practices repository and service layer
I have read many articles about repository pattern and service layer but I have still some doubts in certain arguments:
Repository should return only aggregates and I should have repositories only ...
1
vote
1
answer
892
views
onion architecture - calling external APIs
Umbrella Question
In an onion architecture, where do all the components needed to consume an external API fit in?
The following is the architecture I have so far, based off an example found here: ...
2
votes
2
answers
103
views
How to provide test code for manual testing?
I have written a class that implements some BLE scanning functionality. I have also written some test code that helped me to manually verify my scanner works as intended.
Now I want to somehow ...
5
votes
5
answers
4k
views
Why is C# considered a statically-typed language, although it contains keywords such as `dynamic` and `var`?
C# is considered a statically-typed language. However, it contains keywords such as:
var, which infers the type at compile time, and
dynamic, which determines the type at runtime.
Is this a ...
2
votes
3
answers
280
views
C# Duplicated usage of an if/else condition and a ternary operator. A good practice?
I had a debate with a work mate regarding the following code as to which one of it would be the better practice:
My code (in pseudo):
var A = <precondition either TRUE or FALSE>;
var B;
if(A)
{
...
-1
votes
1
answer
1k
views
How are parameters values passed into a MVC controller constructor?
For this MS sample code, I have checked Program.cs, Startup.cs, and other config files, don't see how the two parameters sqlQueryService and sqlCommandService are populated. Appreciate if someone can ...
4
votes
3
answers
862
views
What is the relationship between variance, generic interfaces, and input/output?
There is a blog post by Eric Lippert where he describes how to define variance. In a general sense, covariance is achieved when the direction of assignment compatibility is preserved. Contravariance ...
0
votes
1
answer
706
views
Generic Repositories with different IDbConnections
I have two different connection strings for two different databases.
My first database has 2 relational tables:
-First table has some default fields,among which average on UI, and some additional ...
4
votes
1
answer
822
views
Should I favor default interface methods over extension methods?
Default interface methods of C# 8 and extension methods have in common that you can "add" a method with implementation to an interface.
"Add" being in quotes here because for ...
0
votes
2
answers
367
views
What if a Repository needs to apply Business Logic to load an Aggregate?
I have an aggregate User and the user has a Score. The Score of a user is calculated by queriying a bunch of different tables and running through (often very large) result sets applying some business ...
0
votes
0
answers
331
views
What approach for this kind of drawing application in C#/WPF
I've got experience with WPF, but I have always been creating standard UI's. I want to build a drawing application for electrical wiring diagrams (single-wire diagrams).
Example of the kind of ...
1
vote
1
answer
180
views
Where should I fetch a product price to create an order in CQRS / DDD?
I want to save an order with order items with prices in my write database. But firstly I need to fetch products with their prices because I don't trust a user - he could pass a lower price, that's why ...
0
votes
0
answers
42
views
How to map translated variable names for column names, so I can export them?
In short story, I want to make some universal generators for different formats (CSV, XLS, XLSX and JSON for now). Just to be clear, I will make different classes for all formats.
I have different ...
0
votes
1
answer
144
views
C# Best Practice DI Pattern?
Is there a best-practice pattern I can/should use for this scenario? I need access to some DI services in the OnFinished() method:
public class Product
{
public int Quantity {get;set;}
}
public ...
0
votes
1
answer
128
views
How handle many files effectively?
I'm looking for a way to work with a huge amount (~10^9) of files (all sizes) in .NET using unspecified file system (NTFS, BTRFS...).
What I have done up to now is to store them in evenly in a folder-...
0
votes
0
answers
103
views
How to avoid duplicate code when working with SOAP proxy classes in different environments?
It's been a long time since I worked with SOAP so my question might be trivial but I just wasn't able to find a satisfying answer so far!
Anyways here's what's going on:
I got access to a system via ...
0
votes
1
answer
671
views
What design pattern / solution helps me elegantly map classes that share the same base class
I'm looking for an elegant way (a design pattern if such exists, not a library) to map two classes that share the same base class, without duplicating the code that maps the properties of the base ...
0
votes
2
answers
397
views
Best practice ASP.NET MVC share code with Web API
I'm trying to develop an ASP.NET MVC-application with an additional Web API.
To reduce code-duplication, I'd like to share as much code as possible, but with the possibility to differentiate between ...
0
votes
1
answer
440
views
What should you unit test in (external) service driven architecture?
My system consists of 4 seperate layers.
API layer
Application layer
Infrastructure layer
Core layer
System is not Domain Driven. It contains no entities with domain logic. All models are anemic. ...
0
votes
1
answer
684
views
When unit-testing should common methods put in static class or in base class?
I have a few methods that are used from different class tests. Also, some other common methods are used for preparing the test data.
What is the common practice to handle this in tests?
Should I ...
0
votes
2
answers
687
views
Is it okay to open separate database connections in each method or is there a better way to do it?
I have a static class in my code that has two methods that store different data in a SQL database when they are called. In each of the methods I open a connection to the database. Is this the proper ...