Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Saturday, March 4, 2023

Repository Design Pattern in Asp .Net

 Introduction:

The Repository Design Pattern is a widely used pattern in the world of software development. It separates the logic that retrieves data from a data source from the logic that manipulates data. This pattern is important for achieving separation of concerns, flexibility, and maintainability. In this blog post, we will demonstrate how to implement the Repository Design Pattern in ASP.NET using C# code.

Step 1: Define an Interface for the Repository

The first step is to define an interface for the repository. This interface should define the methods that will be used to access the data source. In this example, we will create an interface named IProductRepository.


public interface IProductRepository
{
    IEnumerable GetAllProducts();
    Product GetProductById(int id);
    void AddProduct(Product product);
    void UpdateProduct(Product product);
    void DeleteProduct(int id);
}

Step 2: Implement the Repository Interface

The next step is to implement the repository interface. This implementation should contain the logic for accessing the data source. In this example, we will create a class named ProductRepository that implements the IProductRepository interface. We will use Entity Framework to access the data source.


public class ProductRepository : IProductRepository
{
    private readonly ApplicationDbContext _dbContext;

    public ProductRepository(ApplicationDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public IEnumerable GetAllProducts()
    {
        return _dbContext.Products.ToList();
    }

    public Product GetProductById(int id)
    {
        return _dbContext.Products.FirstOrDefault(p => p.Id == id);
    }

    public void AddProduct(Product product)
    {
        _dbContext.Products.Add(product);
        _dbContext.SaveChanges();
    }

    public void UpdateProduct(Product product)
    {
        _dbContext.Products.Update(product);
        _dbContext.SaveChanges();
    }

    public void DeleteProduct(int id)
    {
        var product = GetProductById(id);
        if (product != null)
        {
            _dbContext.Products.Remove(product);
            _dbContext.SaveChanges();
        }
    }
}

Step 3: Inject the Repository into the Application

The next step is to inject the repository into the application. We will use dependency injection to inject the repository into the application. In this example, we will add the following code to the Startup.cs file to configure dependency injection.
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddScoped();

    services.AddControllersWithViews();
}
Step 4: Use the Repository in the Application

The final step is to use the repository in the application. We will use the repository in a controller to retrieve and manipulate data. In this example, we will create a controller named ProductController that uses the IProductRepository interface.


public class ProductController : Controller
{
    private readonly IProductRepository _productRepository;

    public ProductController(IProductRepository productRepository)
    {
        _productRepository = productRepository;
    }

    public IActionResult Index()
    {
        var products = _productRepository.GetAllProducts();
        return View(products);
    }

    public IActionResult Details(int id)
    {
        var product = _productRepository.GetProductById(id);
        if (product == null)
        {
            return NotFound();
        }
        return View(product);
    }

    [HttpGet]
    public IActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Create(Product product)
    {
        if (ModelState.IsValid)
            {
        _productRepository.AddProduct(product);
        return RedirectToAction(nameof(Index));
    }
    return View(product);
}

[HttpGet]
public IActionResult Edit(int id)
{
    var product = _productRepository.GetProductById(id);
    if (product == null)
    {
        return NotFound();
    }
    return View(product);
}

[HttpPost]
public IActionResult Edit(int id, Product product)
{
    if (id != product.Id)
    {
        return NotFound();
    }

    if (ModelState.IsValid)
    {
        _productRepository.UpdateProduct(product);
        return RedirectToAction(nameof(Index));
    }
    return View(product);
}

[HttpGet]
public IActionResult Delete(int id)
{
    var product = _productRepository.GetProductById(id);
    if (product == null)
    {
        return NotFound();
    }
    return View(product);
}

[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
    _productRepository.DeleteProduct(id);
    return RedirectToAction(nameof(Index));
}

Using the Repository Design Pattern in ASP.NET provides several benefits, including:

Improved Separation of Concerns: The Repository Design Pattern separates the data access logic from the application logic, improving the separation of concerns.

Flexibility: The Repository Design Pattern provides a flexible architecture for data access, making it easy to change the data source without affecting the application logic.

Maintainability: The Repository Design Pattern makes it easier to maintain the application by isolating the data access logic in a separate layer.

Performance: The Repository Design Pattern can improve the performance of the application by reducing the number of trips to the database.

Conclusion:

The Repository Design Pattern is an important pattern in the world of software development. It provides a scalable and maintainable architecture for data access in ASP.NET. By separating the data access logic from the application logic, the Repository Design Pattern improves the separation of concerns, flexibility, maintainability, and performance of the application. If you are developing an ASP.NET

Monday, June 6, 2022

Most Important Topics and Interview questions for Dot net Interview

Introduction:

Asp .NET is one of the best frameworks introduced by Microsoft to build web applications. It provides a platform to develop the most complex web services and applications.

Then the solution can be deployed to the azure (App service) cloud to host the application online. The complete front and back end can be developed in the Asp .NET MVC framework and deployed to the cloud. Razor view engine handles the front, and with the help of c#, dot net and EF (entity framework) we can build the backend. 

Aspirates preparing for interviews or fresher trying to get into the IT industry, interested in web development, stay tuned with my blogs.

In this blog, I primarily focus on Interview topics and questions. I have prepared a significant number of official interview questions and also faced these interview questions. I hope these topics and questions will help the aspirants.

I will ensure you that if you prepare well for all the below-mentioned topics and questions, you will get selected. 

Topics:

Dot Net:

1. OOPs concepts (Polymorphism, Inheritance - highly asked)

2. Basics topics of Asp .Net (Just In Time Compiler, CLR, CTS, Assembly)

3. Asp .Net MVC architecture

4. Cloud services like Azure

5. Design Principles

6. SOLID Principles

7. Filters in MVC

6. CRUD operations in Web API and MVC

7. Page Life Cycle (WPF)


Cloud Services (Azure): If you are interviewing for Azure cloud services position or azure as a skillset check the below topics:

1. Virtual Machine

2. Azure Functions, Logic Apps

3. Service bus Queues, Topics and Queue Storage

4. Storage Services like Blob storage

5. Azure Active Directory

6. Azure Fabrics and Data bricks


SQL Server:

1. Basics concepts of SQL server (DDL, DML and DCL)

2. Views, Store Procedures and Table variables

3. Temporal tables, Indexes and Functions

4. Loops and try-catch implementation in my SQL

5. SQL built-in functions like STUFF, REPLACE, CONVERT, CAST, PATINDEX

6. DML Triggers