-
Notifications
You must be signed in to change notification settings - Fork 1
/
CustomerOperations.cs
127 lines (109 loc) · 4.17 KB
/
CustomerOperations.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using NorthEntityLibrary.Contexts;
using NorthEntityLibrary.Models;
using NorthEntityLibrary.Repositories;
namespace NorthEntityLibrary.Classes
{
public class CustomerOperations
{
/// <summary>
/// Generic find, not specific to customers
/// </summary>
/// <param name="identifier"></param>
/// <param name="paths"></param>
/// <returns></returns>
public static async Task<Customers> GenericRepositoryFindAsync(int identifier, string[] paths)
{
var repo = new GenericRepository<Customers>();
return await repo.GetTask(identifier, paths);
}
/// <summary>
/// Get all customers without navigation properties
/// </summary>
/// <returns></returns>
public static List<Customers> GetAll()
{
var repo = new GenericRepository<Customers>();
return (List<Customers>) repo.GetAll();
}
/// <summary>
/// Get all Customers with desired navigation properties
/// </summary>
/// <param name="navigationProperties"></param>
/// <returns></returns>
public static List<Customers> GetAll(params Expression<Func<Customers, object>>[] navigationProperties)
{
var repo = new GenericRepository<Customers>();
return (List<Customers>)repo.GetAll(navigationProperties);
}
/// <summary>
/// Get customer by primary key with all navigation properties
/// </summary>
/// <param name="identifier"></param>
/// <returns></returns>
public static async Task<Customers> GenericRepositoryFindWithIncludesAsync(int identifier)
{
var repo = new GenericRepository<Customers>();
return await repo.GetWithIncludesTask(identifier);
}
/// <summary>
/// Get customer by primary key and optional one or more navigation properties
/// </summary>
/// <param name="identifier"></param>
/// <param name="navigationPaths"></param>
/// <returns></returns>
public static async Task<Customers> Get(int identifier, string[] navigationPaths = null)
{
using (var context = new NorthwindContext())
{
var customer = await context.Customers.FindAsync(identifier);
if (navigationPaths == null) return customer;
foreach (var navigation in context.Entry(customer).Navigations)
{
await navigation.LoadAsync();
}
foreach (var path in navigationPaths)
{
await context.Entry((object)customer).Reference(path).LoadAsync();
}
return customer;
}
}
/// <summary>
/// Get customer by primary key with all navigation properties.
/// </summary>
/// <param name="identifier"></param>
/// <returns></returns>
public static async Task<Customers> GetWithAllNavigationProperties(int identifier)
{
using (var context = new NorthwindContext())
{
var customer = await context.Customers.FindAsync(identifier);
foreach (NavigationEntry navigation in context.Entry(customer).Navigations)
{
await navigation.LoadAsync();
}
return customer;
}
}
/// <summary>
/// Get single Customers with no navigation properties
/// </summary>
/// <param name="keys">primary key to locate by</param>
/// <returns>Employees if found, null if not found</returns>
public static async Task<Customers> FindCustomersAsync(object[] keys)
{
using (var context = new NorthwindContext())
{
var customer = await context.Customers.FindAsync(keys);
return customer;
}
}
}
}