Welcome to triksbuddy blog. He we discuss on different technology, tips and tricks, programming, project management and leadership. Here we share technology tutorials, reviews, comparison, listing and many more. We also share interview questions along with answers on different topics and technologies. Stay tuned and connected with us and know new technologies and dig down known ones.
Friday, March 24, 2023
How to implement async CRUD methods for an Employee model using Dapper with Oracle
How to implement async CRUD methods for an Employee model using Dapper with MySQL
How to implement async CRUD methods for an Employee model using Dapper with PostgreSQL
You will need to install and add reference to the Npgsql package to communicate with PostgreSQL database.
Thursday, March 23, 2023
How to read data in .net from Oracle using Dapper
Note that you will need to add the reference to the Oracle.ManagedDataAccess.Client package.
Different ways to write a method that reads list of entities from SQL Server in C# language
There are different ways we can do same work. With evolution of .net technologies we have found different technologies to perform data operations. Here we will look at different ways of getting a list of employees from SQL Server database using different data access technologies.
Method-1: using ADO.NET
This method retrieves the employee list from the "Employee" table in the SQL Server database using a SELECT query, and maps the data to a list of Employee objects. Note that in this example, the Employee class has properties for the Id, Name, Department, and Salary fields in the database, and the SqlDataReader's Get methods are used to retrieve the values for each field from each record.
Method-2: using ADO.NET async
In this async version of the method, the SqlConnection is opened asynchronously using await connection.OpenAsync(), and the SqlDataReader is created and executed asynchronously using await command.ExecuteReaderAsync(). The while loop that reads the data is also made asynchronous using await reader.ReadAsync(). Note that the using statements are still used to ensure proper disposal of resources.
Method-3: using Dapper
In this async version of the method, the SqlConnection is opened asynchronously using await connection.OpenAsync(). The query is then executed asynchronously using Dapper's QueryAsync method, which returns a list of Employee objects already mapped to the data returned by the SQL query. The using statement is still used to ensure proper disposal of resources.
In this async version of the method, a DbContextOptionsBuilder is used to configure the connection string and then used to create a new DbContext instance. The Set method is used to retrieve the DbSet<Employee>, and then the ToListAsync method is called to execute the query asynchronously and retrieve the Employee objects already mapped to the data returned by the SQL query. The using statement is still used to ensure proper disposal of resources. Note that you will need to add the appropriate NuGet packages and configure EF Core for your specific database provider.
Method-5: LINQ to SQL:
In this async version of the method, a DataContext is used to connect to the database and a Table<Employee> is used to retrieve the table of Employee objects. The ToListAsync method is called on the Table<Employee> to execute the query asynchronously and retrieve the Employee objects already mapped to the data returned by the SQL query. Note that you will need to add the appropriate references to System.Data.Linq and configure the DataContext for your specific database provider. The using statement is still used to ensure proper disposal of resources.
Thursday, July 7, 2022
What is Interface in C#? What are the benefits of using Interface?
Interface: An interface contains definitions for a group of related functionalities that a non-abstract class or a struct must implement.
Interfaces can contain instance methods, properties, events, indexers, or any combination of those four member types. Interfaces may contain static constructors, fields, constants, or operators.Benefits: Interface helps us to implement all of Object Oriented Programming concepts. There are several benefits of using interfaces in .NET, including:
1. Loose Coupling: Interface allows us developing very loosely coupled system. An interface defines a contract that a class must follow. By using an interface, you can ensure that the class implementing it adheres to a specific set of rules and functionality.
2. Abstraction: Interface helps us developing secured system by implementing abstraction. It is a contract that contains properties and method signatures and implementation is done in inherited class. Thus it helps hiding implementation of internal business logic from external systems.
3. Inheritance: Interface in C# helps to achieve multiple inheritance. C# does not allow multiple class inheritance, but a class can implement multiple interfaces. This allows you to create objects with functionality from multiple sources, without the complications that can arise from multiple inheritance.
4. Polymorphism: Interfaces allow us to define polymorphism in a declarative way, unrelated to implementation. Two elements are polymorphic with respect to a set of behaviors if they realize the same interfaces.
5. Plug and Play: Interfaces are the key to the "plug-and-play" ability of an architecture.
6. Testing: Interfaces enable mocking objects which makes easier and better unit testing.
7. Dependency Injection: Interfaces are great for implementing Inversion of Control or Dependency Injection.
8. Extensibility: We can satisfy extensibility using the interfaces in C#.
9. Parallel Programming: Interfaces enable parallel application development.
10. Code Re usability: An interface can be implemented in multiple classes that enables greater code reuse. This can reduce the amount of code you need to write, and make it easier to modify or update existing code.
In conclusion we can say that using interfaces can help to create more modular, maintainable, and reusable code, while also enabling polymorphism and simplifying testing.
* Beginning with C# 8.0, an interface may define a default implementation for members.