-
Notifications
You must be signed in to change notification settings - Fork 40
/
IEntityBuilder.cs
86 lines (74 loc) · 2.66 KB
/
IEntityBuilder.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
// Copyright (c) Mondol. All rights reserved.
//
// Author: frank
// Email: [email protected]
// Created: 2017-01-22
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Internal;
using Mondol.DapperPoco.Metadata;
namespace Mondol.DapperPoco
{
public interface IEntityBuilder
{
FluentEntityTableInfo Build();
}
public class EntityBuilder<TEntity> : IEntityBuilder where TEntity : class
{
private string _tableName;
private readonly Dictionary<string, FluentEntityColumnInfo> _properties = new Dictionary<string, FluentEntityColumnInfo>();
public EntityBuilder<TEntity> TableName(string name)
{
_tableName = name;
return this;
}
public EntityBuilder<TEntity> ColumnName(Expression<Func<TEntity, object>> propertyExpression, string name)
{
var prop = GetProperty(propertyExpression);
GetFluentEntityColumnInfo(prop).ColumnName = name;
return this;
}
public EntityBuilder<TEntity> Ignore(Expression<Func<TEntity, object>> propertyExpression)
{
var prop = GetProperty(propertyExpression);
GetFluentEntityColumnInfo(prop).IsIgnore = true;
return this;
}
public EntityBuilder<TEntity> PrimaryKey(Expression<Func<TEntity, object>> propertyExpression, bool? autoIncrement = null)
{
var prop = GetProperty(propertyExpression);
var eci = GetFluentEntityColumnInfo(prop);
eci.IsPrimaryKey = true;
eci.IsAutoIncrement = autoIncrement;
return this;
}
public FluentEntityTableInfo Build()
{
return new FluentEntityTableInfo()
{
TableName = _tableName,
Columns = _properties.Values.ToArray()
};
}
private FluentEntityColumnInfo GetFluentEntityColumnInfo(PropertyInfo prop)
{
FluentEntityColumnInfo eci;
if (!_properties.TryGetValue(prop.Name, out eci))
{
eci = new FluentEntityColumnInfo() { Property = prop };
_properties.Add(prop.Name, eci);
}
return eci;
}
private PropertyInfo GetProperty(Expression<Func<TEntity, object>> propertyExpression)
{
//TODO: 此处对EF Core有依赖,日后去掉依赖
var pi = propertyExpression.GetPropertyAccess();
return pi;
}
}
}