-
Notifications
You must be signed in to change notification settings - Fork 3
/
BaseEntityHelper.cs
124 lines (102 loc) · 4.08 KB
/
BaseEntityHelper.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
using System;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Sasha.Exceptions;
using Sasha.ExtensionMethods;
namespace XrmLibrary.EntityHelpers.Common
{
/// <summary>
/// XrmLibrary.EntityHelpers Base class
/// </summary>
public class BaseEntityHelper
{
#region | Private Definitions |
IOrganizationService _organizationService;
#endregion
#region | Properties |
/// <summary>
/// <see cref="IOrganizationService"/>
/// </summary>
public IOrganizationService OrganizationService { get { return _organizationService; } }
/// <summary>
/// Related entity <c>MS CRM SDK</c> url
/// </summary>
public string SdkHelpUrl { get; internal set; }
/// <summary>
/// Related entity logical name
/// </summary>
public string EntityName { get; internal set; }
#endregion
#region | Constructors |
/// <summary>
/// Default constructor
/// </summary>
/// <param name="service"><see cref="OrganizationService"/></param>
public BaseEntityHelper(IOrganizationService service)
{
ExceptionThrow.IfNull(service, "service", "IOrganizationService can not be null");
_organizationService = service;
}
#endregion
#region | Public Methods |
/// <summary>
/// Retrieve record by Id
/// </summary>
/// <param name="id">Record (Entity) Id</param>
/// <param name="retrievedColumns">Set columns (attributes) that you need. If you don't set, default value is <see cref="ColumnSet(true)"/></param>
/// <returns>
/// <see cref="Entity"/> for record data
/// </returns>
public Entity Get(Guid id, params string[] retrievedColumns)
{
ExceptionThrow.IfNullOrEmpty(this.EntityName, "BaseEntityHelper.EntityName");
ExceptionThrow.IfGuidEmpty(id, "id");
var columnSet = !retrievedColumns.IsNullOrEmpty() ? new ColumnSet(retrievedColumns) : new ColumnSet(true);
return this.OrganizationService.Retrieve(this.EntityName.ToLower().Trim(), id, columnSet);
}
/// <summary>
/// Retrieve records by specified <see cref="QueryBase"/> .
/// <para>
/// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.messages.retrievemultiplerequest(v=crm.8).aspx
/// </para>
/// </summary>
/// <param name="query"></param>
/// <returns>
/// <see cref="EntityCollection"/> for data
/// </returns>
public EntityCollection Get(QueryBase query)
{
ExceptionThrow.IfNullOrEmpty(this.EntityName, "BaseEntityHelper.EntityName");
return this.OrganizationService.RetrieveMultiple(query);
}
/// <summary>
/// Retrieve records by specified <c>FetchXml</c>.
/// <para>
/// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.fetchxmltoqueryexpressionrequest(v=crm.8).aspx
/// </para>
/// </summary>
/// <param name="fetchxml"><c>FetchXml</c></param>
/// <returns>
/// <see cref="EntityCollection"/> for data
/// </returns>
public EntityCollection Get(string fetchxml)
{
ExceptionThrow.IfNullOrEmpty(fetchxml, "fetchxml");
FetchExpression request = new FetchExpression(fetchxml);
return this.OrganizationService.RetrieveMultiple(request);
}
/// <summary>
/// Delete an existing record
/// </summary>
/// <param name="id">Record Id</param>
public void Delete(Guid id)
{
ExceptionThrow.IfNullOrEmpty(this.EntityName, "BaseEntityHelper.EntityName");
ExceptionThrow.IfGuidEmpty(id, "id");
CommonHelper commonHelper = new CommonHelper(this.OrganizationService);
commonHelper.Delete(id, this.EntityName);
}
#endregion
}
}