-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
167 lines (140 loc) · 5.01 KB
/
Program.cs
File metadata and controls
167 lines (140 loc) · 5.01 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using TestAutoMapCollection.DTOs;
using TestAutoMapCollection.Models;
namespace TestAutoMapCollection
{
class Program
{
private static IMapper _mapper;
private static void CreateOrder()
{
using (var context = new ContextFactory().CreateDbContext(null))
{
if (!context.Orders.Any())
{
context.Orders.Add(new Order()
{
Description = "This is an order",
OrderLines = new List<OrderLine>()
{
new OrderLine()
{
Description = "This is an order line"
},
new OrderLine()
{
Description = "This is another order line"
},
new OrderLine()
{
Description = "This is yet annother order line"
}
}
});
}
context.SaveChanges();
}
}
private static OrderDTO GetOrderDto()
{
OrderDTO orderDto = null;
using (var context = new ContextFactory().CreateDbContext(null))
{
var data = context.Orders
.Include(x => x.OrderLines)
//.ThenInclude(x => x.Products)
.FirstOrDefault();
orderDto = _mapper.Map<OrderDTO>(data);
}
return orderDto;
}
private static void SaveOrder(OrderDTO orderDto)
{
using (var context = new ContextFactory().CreateDbContext(null))
{
var fromDataBase = context.Orders
.Include(x => x.OrderLines)
.FirstOrDefault(x => x.OrderId == orderDto.OrderId);
_mapper.Map(orderDto, fromDataBase);
try
{
context.SaveChanges();
}
catch (Exception ex)
{
var baseMessage = ex.GetBaseException().Message;
Console.WriteLine(baseMessage);
}
}
}
static void Main(string[] args)
{
#region Setup That would normally be done by DI
ServiceCollection services = new ServiceCollection();
services.AddDbContext<DataContext>(ServiceLifetime.Scoped);
services.AddEntityFrameworkSqlServer();
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<Profiles>();
});
_mapper = new Mapper(config);
// Who needs to properly seed the database? Not me.
CreateOrder();
#endregion
// Get the order
var orderDto = GetOrderDto();
//Change the description on the order and its order lines
orderDto.Description = "This is different.";
orderDto.OrderLines.ForEach(ol => ol.Description = "This has been changed too.");
//Save the order back to the database
SaveOrder(orderDto);
}
}
public class Profiles : Profile
{
public Profiles()
{
CreateMap<Order, OrderDTO>().ReverseMap();
CreateMap<OrderLine, OrderLineDTO>().ReverseMap();
}
}
#region Profiles with the AfterMap Used
//internal class Profiles : Profile
//{
// public Profiles()
// {
// CreateMap<OrderDTO, Order>()
// .ForMember(d => d.OrderLines, opt => opt.Ignore())
// .AfterMap(AddOrUpdate)
// .ReverseMap();
// CreateMap<OrderLine, OrderLineDTO>()
// .ReverseMap();
// }
// private void AddOrUpdate(OrderDTO orderDto, Order order)
// {
// foreach (var dto in orderDto.OrderLines)
// {
// if (dto.OrderLineId == 0)
// {
// //order.OrderLines.Add(Mapper.Map<Order>(dto));
// order.OrderLines.Add(new OrderLine(){
// Description = dto.Description,
// OrderId = dto.Order.OrderId
// });
// }
// else
// {
// var ol = order.OrderLines.SingleOrDefault(x => x.OrderLineId == dto.OrderLineId);
// ol.Description = dto.Description;
// //Mapper.Map(dto, order.OrderLines.SingleOrDefault(c => c.OrderLineId == dto.OrderLineId));
// }
// }
// }
//}
#endregion region
}