Best Repository Pattern for ASP.NET MVC

后端 未结 4 389
野的像风
野的像风 2020-12-07 06:54

I recently learned ASP.NET MVC (I love it). I\'m working with a company that uses dependency injection to load a Repository instance in each request, and I\'m familiar with

4条回答
  •  旧时难觅i
    2020-12-07 07:27

    There is a ready to use solution at URF - Unit of Work & (extensible/generic) Repositories Framework. It will save you a lot of time. They implemented a generic repository (also there is an async repository). For extending any repository they have used extensions like this:

         public static decimal GetCustomerOrderTotalByYear(this IRepository repository, string customerId, int year)
        {
            return repository
                .Queryable()
                .Where(c => c.CustomerID == customerId)
                .SelectMany(c => c.Orders.Where(o => o.OrderDate != null && o.OrderDate.Value.Year == year))
                .SelectMany(c => c.OrderDetails)
                .Select(c => c.Quantity*c.UnitPrice)
                .Sum();
        }
    

    Some classes like QueryObject may be an overwork depending on your project but overally it is good solution to help you get up and running.

提交回复
热议问题