<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ayush Pandey</title>
    <description>The latest articles on DEV Community by Ayush Pandey (@ayusclg_).</description>
    <link>https://dev.to/ayusclg_</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3400850%2F8bfbbafe-9779-4bcb-bd3a-e5cb0606cca1.jpg</url>
      <title>DEV Community: Ayush Pandey</title>
      <link>https://dev.to/ayusclg_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ayusclg_"/>
    <language>en</language>
    <item>
      <title>What really Repository is in Asp.Net Core ?</title>
      <dc:creator>Ayush Pandey</dc:creator>
      <pubDate>Tue, 14 Oct 2025 17:06:36 +0000</pubDate>
      <link>https://dev.to/ayusclg_/what-really-repository-is-in-aspnet-core--4n8</link>
      <guid>https://dev.to/ayusclg_/what-really-repository-is-in-aspnet-core--4n8</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is a Repository?&lt;/strong&gt; &lt;br&gt;
A Repository acts as a bridge between your application’s services and the database.&lt;/p&gt;

&lt;p&gt;Think of it like this: &lt;br&gt;
Your Service contains the business logic – the rules, validations, and operations your app performs.Your DbContext represents the database connection and tables.The Repository sits in the middle, handling all database operations like Create, Read, Update, and Delete (&lt;strong&gt;CRUD&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;By doing this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Your database logic is cleanly separated. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;2.Your service doesn’t need to know how the database works. &lt;/p&gt;

&lt;p&gt;3.Your code becomes easier to maintain, test, and extend.&lt;/p&gt;

&lt;p&gt;Example: UserRepository &lt;br&gt;
Here’s an example of a simple repository for managing users:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using backend_01.Core.User.Model;
using backend_01.Infrastructure.Data;
using Microsoft.EntityFrameworkCore;

namespace backend_01.Infrastructure.Repository
{
    public class UserRepository
    {
        private readonly ApplicationDbContext _context;

        public UserRepository(ApplicationDbContext context)
        {
            _context = context; // DbContext injected via DI
        }

        public async Task&amp;lt;UserModel&amp;gt; CreateUser(UserModel user)
        {
           try
           {
               await _context.Users.AddAsync(user);
               await _context.SaveChangesAsync();
               return user;
           }
           catch (Exception ex)
           {
               throw new Exception($"Internal Server Error, Message: {ex.Message}");
           }
        }
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our UserRepository, we first inject the &lt;strong&gt;ApplicationDbContext&lt;/strong&gt; through the constructor using private readonly ApplicationDbContext _context; so that the repository can directly &lt;strong&gt;interact&lt;/strong&gt; with the database. To create a new user, we define the CreateUser(UserModel user) method, where we await _context.Users.AddAsync(user); to add the entity and then await _context.SaveChangesAsync(); to persist the changes. For fetching users, we have GetUsers(), which uses await _context.Users.Take(10).ToListAsync(); to retrieve the first 10 users. Both methods are wrapped in try-catch blocks to handle errors gracefully and throw descriptive exceptions. This repository is then registered in Program.cs with &lt;strong&gt;builder.Services.AddScoped();&lt;/strong&gt;, ensuring a new instance is created per HTTP request. Essentially, the repository acts as a bridge between the service layer and the database, centralizing all CRUD operations and keeping business logic in the service layer clean and focused.&lt;/p&gt;

</description>
      <category>dotnetcore</category>
      <category>csharp</category>
      <category>database</category>
      <category>backend</category>
    </item>
    <item>
      <title>DB Connection on Asp.net core</title>
      <dc:creator>Ayush Pandey</dc:creator>
      <pubDate>Mon, 13 Oct 2025 15:53:22 +0000</pubDate>
      <link>https://dev.to/ayusclg_/db-connection-on-aspnet-core-364a</link>
      <guid>https://dev.to/ayusclg_/db-connection-on-aspnet-core-364a</guid>
      <description>&lt;p&gt;&lt;strong&gt;ASP.NET Core&lt;/strong&gt; is a framework that is &lt;strong&gt;structured&lt;/strong&gt; and &lt;strong&gt;type-safe&lt;/strong&gt;, which means it helps us write code in an organized way and catches mistakes before they cause problems. Today we are going to look at how to connect our application to a database using PostgreSQL. To do this, we first need to create an entity or model, which defines the shape of the data we want to work with. Then we need a database context, which acts as a bridge between our application and the database, allowing us to read and write data easily.&lt;/p&gt;

&lt;p&gt;Now let’s create a model. In ASP.NET Core, a model defines the structure of the data we want to work with. Here’s an example of a simple User model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;namespace backend_01.Core.Model
{
    public class User
    {
        public int Id { get; set; }
        public string? UserName { get; set; } = string.Empty;
        public string? Email { get; set; } = string.Empty;
        public string? Password { get; set; } = string.Empty;
        public DateTime createdAt { get; set; } = DateTime.Now;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s break this down:&lt;/p&gt;

&lt;p&gt;Id is an integer that uniquely identifies each user.&lt;/p&gt;

&lt;p&gt;UserName, Email, and Password are all strings. The ? means they can be null, and = string.Empty ensures they start as empty strings so our app doesn’t crash if nothing is provided.&lt;/p&gt;

&lt;p&gt;createdAt is a DateTime property that stores when the user was created. By default, it is set to the current time using DateTime.Now.&lt;/p&gt;

&lt;p&gt;Now that we have our model ready, we need something that connects our application to the database — this is where the DbContext comes in. You can think of the DbContext as the main bridge between your C# code and the actual database. It helps us send and receive data in an organized way. Here’s the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;using backend_01.Core.Model;
using Microsoft.EntityFrameworkCore;

namespace backend_01.Infrastructure.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions&amp;lt;ApplicationDbContext&amp;gt; options) : base(options)
        {

        }
        public DbSet&amp;lt;User&amp;gt; Users { get; set; }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s understand it in simple terms:&lt;/p&gt;

&lt;p&gt;ApplicationDbContext is a class that inherits from DbContext. This makes it capable of communicating with the database.&lt;/p&gt;

&lt;p&gt;The line public ApplicationDbContext(DbContextOptions options) : base(options) passes configuration options (like the database connection string) to the base DbContext class.&lt;/p&gt;

&lt;p&gt;public DbSet Users { get; set; } tells Entity Framework that we want a table in the database for our User model. Here, DbSet represents that table, and each User object will become one record (or row) inside it.&lt;/p&gt;

&lt;p&gt;So, whenever we perform operations like adding, updating, deleting, or fetching users, we’ll do it through this ApplicationDbContext. Think of it as your app’s direct hotline to the database.&lt;/p&gt;

&lt;p&gt;You should always use the database context after creating the builder and before building the app instance. This is because we need to register all our services (like controllers, DbContext, authentication, etc.) before the application starts running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddDbContext&amp;lt;ApplicationDbContext&amp;gt;(options =&amp;gt;
    options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this guide, we learned how ASP.NET Core helps us build structured and type-safe applications that stay clean and organized. We created a simple User model to define our data structure, then built an ApplicationDbContext class to connect our application with the database. Finally, we registered our DbContext in the Program.cs file, ensuring that the app can communicate with PostgreSQL smoothly. With these steps, our application is now ready to perform database operations like saving, updating, or retrieving data — all in a clean and professional way.&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>dotnet</category>
      <category>csharp</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
