Home > other >  ASP.NET Web API code uses which pattern - C#
ASP.NET Web API code uses which pattern - C#

Time:11-01

I have the following code in my so called repository layer.

public class EmployeeDetailsRepository : IEmployeeDetailsRepository
{
    private readonly IDataAccess _dataAccess;

    public ILogger Logger { get; set; }

    public EmployeeDetailsRepository(IDataAccess dataAccess)
    {
        Logger = LoggerUtil.GetLogger("Data access repository");
    }

    public EmployeeDetails GetEmployeeByFirstName(string firstName)
    {
        EmployeeDetails employeeDetails;

        using (ILinqContext context = _dataAccess.CreateContext(""))
        {
            employeeDetails =
                (from stg in context.Table<Employees>() 
                 where (stg.Name == firstName)
                 select stg).FirstOrDefault();
        }
        
        return employeeDetails;
    }
}

What exactly does the above code mean? Every example I google is for ASP.NET Core. I know for the fact that this one is ASP.NET Framework 4.5 using Web API 2. But I see so many patterns while searching, what should I read/study to understand this pattern better?

I can paste more code if required.

CodePudding user response:

Check this out Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application

it explains well about this pattern

CodePudding user response:

What your code does is; it is implementing repository patter using Entity framework

When you call the above method, EF will generate a SQL query like below:

SELECT TOP(1) [e].[Id], [e].[DoB], [e].[FirstName], [e].[LastName]
FROM [Employees] AS [e]
WHERE [e].[FirstName] = N'Bill'

You can refer below for more information.

https://channel9.msdn.com/Series/The-Full-Stack/The-Full-Stack-Part-5-Implementing-the-repository-with-EF-Code-First-and-Dependency-Injection

https://www.youtube.com/watch?v=rtXpYpZdOzM

  • Related