- Cost Breakdown for EC2
- Example Setup
- Step 1: Prepare AWS Environment
- Step 1.1: Generate the key pem in local
- Step 2: SSH into the EC2 Instance
- Step 3: Install NGINX and .NET on CentOS 7 EC2 Instance
- Step 5: Set Up Your .NET Web API
- Step 6: Test the Web API
- Step 7: Automate Web API Startup (Optional)
- Step 8: Clean Up Resources (Optional)
- Upcoming content
7 posts tagged with "c#"
View All Tagsnet 8 and c# 12 big change
Mongo with csharp
Exploring Dynamic Distance Calculation with Delegates: A Proof of Concept
Welcome to our latest project - a Proof of Concept (PoC) repository that explores dynamic distance calculation using delegates. In this blog post, we'll take you through the features, the motivation behind the project, and how you can experiment with dynamic resolution of distance calculation services in a web API.
Why Delegates?
Delegates in C# are powerful constructs that allow us to treat methods as first-class citizens. Leveraging this capability, our PoC demonstrates how to dynamically resolve distance calculation services at runtime. This flexibility opens the door to easily switch between different providers based on specific conditions or user preferences.
Key Features
Dynamic Resolution with Delegates
The heart of this project lies in the DistanceCalculator
class, which uses a delegate (DistanceProviderResolver
) to dynamically resolve the distance calculation service. This allows us to switch between Google and PCMiler providers with ease, providing a seamless and extensible solution.
var distanceCalculator = new DistanceCalculator();
var distanceService = distanceCalculator.ResolveDistanceProvider(DistanceProvider.Google);
double distance = distanceService.CalculateDistance("Origin", "Destination");
Configurability
Experiment with different distance calculation providers by customizing the Program.cs
file. This configurability makes it easy to adapt the PoC to various scenarios and requirements.
Limitations and Roadmap
As a Proof of Concept, it's important to note that this project currently uses simulated distance calculation logic with dummy values. While it showcases the concept effectively, it may lack certain features expected in a production-ready application.
We encourage you to explore, experiment, and contribute. If you encounter issues or have ideas for improvements, please open an issue on our GitHub repository.
Demo
Clone the Repository :
git clone https://github.com/nhonvo/delegate-poc.git
cd delegate-poc
Conclusion
Dynamic distance calculation with delegates offers a glimpse into the world of flexible and configurable service resolution. We hope this PoC sparks ideas and discussions about how such a concept could be integrated into real-world applications.
Feel free to dive into the code, contribute your insights, and join us on this exploration of dynamic resolution in C#.
Happy coding!
Entity framework core
Introduction
- Entity Framework Core is an ORM framework, open-source, lightweight and cross-platform developed by Microsoft.
- It enables developers to work with databases using .NET object and EF Core is built on top of ADO.NET
Relationship
One-to-One Relationship
modelBuilder.Entity<Author>()
.HasOne(a => a.Book) // Author has one Book
.WithOne(b => b.Author) // Book has one Author
.HasForeignKey<Book>(b => b.AuthorId); // Foreign key in Book referencing AuthorId
One-to-Many Relationship
modelBuilder.Entity<Author>()
.HasMany(a => a.Books) // Author has many Books
.WithOne(b => b.Author) // Book has one Author
.HasForeignKey(b => b.AuthorId); // Foreign key in Book referencing AuthorId
Many-to-Many Relationship
modelBuilder.Entity<StudentCourse>()
.HasKey(sc => new { sc.StudentId, sc.CourseId }); // Define composite primary key for the join table
modelBuilder.Entity<StudentCourse>()
.HasOne(sc => sc.Student)
.WithMany(s => s.Courses)
.HasForeignKey(sc => sc.StudentId); // Foreign key in join table referencing StudentId
modelBuilder.Entity<StudentCourse>()
.HasOne(sc => sc.Course)
.WithMany(c => c.Students)
.HasForeignKey(sc => sc.CourseId); // Foreign key in join table referencing CourseId
DeleteBehavior
The DeleteBehavior
enum in EF Core includes the following options:
Cascade: Deleting the principal/parent entity will cause the dependent/child entities to be deleted as well.
OnDelete(DeleteBehavior.Cascade)
SetNull: Deleting the principal/parent entity will set the foreign key properties in the dependent/child entities to null.
OnDelete(DeleteBehavior.SetNull)
SetDefault: Deleting the principal/parent entity will set the foreign key properties in the dependent/child entities to their default values.
OnDelete(DeleteBehavior.SetDefault)
Restrict: Prevents the deletion of the principal/parent entity if there are dependent/child entities.
OnDelete(DeleteBehavior.Restrict)
. An exception will be thrown.NoAction: Similar to
Restrict
, it is used to specify no action on delete.OnDelete(DeleteBehavior.NoAction)
and you'll need to handle constraints in your application logic.
For example:
modelBuilder.Entity<ParentEntity>()
.HasMany(p => p.ChildEntities)
.WithOne(c => c.ParentEntity)
.OnDelete(DeleteBehavior.Restrict);
This configuration would set the delete behavior for the relationship between ParentEntity
and ChildEntity
to Restrict
.
Best pratices
Indexing: Creating indexes on frequently accessed columns can improve query performance.
Proper data modeling: Designing tables and relationships properly can improve query performance and prevent performance issues.
Caching: Storing frequently accessed data in a cache can reduce database calls and improve application performance.
Query optimization: Writing efficient queries can improve performance. Techniques such as avoiding unnecessary joins and reducing the number of returned columns can help.
Connection pooling: Reusing database connections instead of creating new ones can improve performance.
Batch processing: Performing multiple operations in a single database call can improve performance and reduce overhead.
Asynchronous programming: Using asynchronous programming techniques can improve performance by allowing the application to continue executing while waiting for database calls to complete.