diff --git a/src/FakeXrmEasy.Core/Middleware/Crud/IOrganizationServiceMiddlewareExtensions.cs b/src/FakeXrmEasy.Core/Middleware/Crud/IOrganizationServiceMiddlewareExtensions.cs new file mode 100644 index 00000000..7f7c60b8 --- /dev/null +++ b/src/FakeXrmEasy.Core/Middleware/Crud/IOrganizationServiceMiddlewareExtensions.cs @@ -0,0 +1,128 @@ +using System; +using FakeItEasy; +using Microsoft.Xrm.Sdk; +using Microsoft.Xrm.Sdk.Messages; +using Microsoft.Xrm.Sdk.Query; + +namespace FakeXrmEasy.Middleware.Crud +{ + internal static class IOrganizationServiceMiddlewareExtensions + { + public static IOrganizationService AddFakeCreate(this IOrganizationService service) + { + A.CallTo(() => service.Create(A._)) + .ReturnsLazily((Entity e) => + { + var request = new CreateRequest(); + request.Target = e; + var response = service.Execute(request) as CreateResponse; + return response.id; + }); + + return service; + } + + public static IOrganizationService AddFakeUpdate(this IOrganizationService service) + { + A.CallTo(() => service.Update(A._)) + .Invokes((Entity e) => + { + var request = new UpdateRequest(); + request.Target = e; + service.Execute(request); + }); + + return service; + } + + public static IOrganizationService AddFakeRetrieveMultiple(this IOrganizationService service) + { + //refactored from RetrieveMultipleExecutor + A.CallTo(() => service.RetrieveMultiple(A._)) + .ReturnsLazily((QueryBase req) => { + var request = new RetrieveMultipleRequest { Query = req }; + + var response = service.Execute(request) as RetrieveMultipleResponse; + return response.EntityCollection; + }); + + return service; + } + + public static IOrganizationService AddFakeRetrieve(this IOrganizationService service) + { + A.CallTo(() => service.Retrieve(A._, A._, A._)) + .ReturnsLazily((string entityName, Guid id, ColumnSet columnSet) => + { + var request = new RetrieveRequest() + { + Target = new EntityReference() { LogicalName = entityName, Id = id }, + ColumnSet = columnSet + }; + + var response = service.Execute(request) as RetrieveResponse; + return response.Entity; + }); + + return service; + } + + public static IOrganizationService AddFakeDelete(this IOrganizationService service) + { + A.CallTo(() => service.Delete(A._, A._)) + .Invokes((string entityName, Guid id) => + { + if (string.IsNullOrWhiteSpace(entityName)) + { + throw new InvalidOperationException("The entity logical name must not be null or empty."); + } + + if (id == Guid.Empty) + { + throw new InvalidOperationException("The id must not be empty."); + } + + var entityReference = new EntityReference(entityName, id); + + var request = new DeleteRequest() { Target = entityReference }; + service.Execute(request); + }); + + return service; + } + + public static IOrganizationService AddFakeAssociate(this IOrganizationService service) + { + A.CallTo(() => service.Associate(A._, A._, A._, A._)) + .Invokes((string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection entityCollection) => + { + var request = new AssociateRequest() + { + Target = new EntityReference() { Id = entityId, LogicalName = entityName }, + Relationship = relationship, + RelatedEntities = entityCollection + }; + service.Execute(request); + }); + + return service; + } + + public static IOrganizationService AddFakeDisassociate(this IOrganizationService service) + { + A.CallTo(() => service.Disassociate(A._, A._, A._, A._)) + .Invokes((string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection entityCollection) => + { + var request = new DisassociateRequest() + { + Target = new EntityReference() { Id = entityId, LogicalName = entityName }, + Relationship = relationship, + RelatedEntities = entityCollection + }; + service.Execute(request); + }); + + return service; + } + } +} \ No newline at end of file diff --git a/src/FakeXrmEasy.Core/Middleware/Crud/MiddlewareBuilderExtensions.Crud.cs b/src/FakeXrmEasy.Core/Middleware/Crud/MiddlewareBuilderExtensions.Crud.cs index e44146ac..b6bd8b8b 100644 --- a/src/FakeXrmEasy.Core/Middleware/Crud/MiddlewareBuilderExtensions.Crud.cs +++ b/src/FakeXrmEasy.Core/Middleware/Crud/MiddlewareBuilderExtensions.Crud.cs @@ -57,13 +57,13 @@ public static IMiddlewareBuilder AddCrud(this IMiddlewareBuilder builder) #endif context.SetProperty(crudMessageExecutors); - AddFakeCreate(context, service); - AddFakeRetrieve(context, service); - AddFakeRetrieveMultiple(context, service); - AddFakeUpdate(context,service); - AddFakeDelete(context,service); - AddFakeAssociate(service); - AddFakeDisassociate(service); + service.AddFakeCreate() + .AddFakeRetrieve() + .AddFakeRetrieveMultiple() + .AddFakeUpdate() + .AddFakeDelete() + .AddFakeAssociate() + .AddFakeDisassociate(); }); return builder; @@ -94,7 +94,6 @@ public static IMiddlewareBuilder AddCrud(this IMiddlewareBuilder builder, IInteg /// public static IMiddlewareBuilder UseCrud(this IMiddlewareBuilder builder) { - Func middleware = next => { return (IXrmFakedContext context, OrganizationRequest request) => { @@ -133,108 +132,5 @@ private static OrganizationResponse ProcessRequest(IXrmFakedContext context, Org return fakeMessageExecutor.Execute(request, context); } - private static void AddFakeCreate(IXrmFakedContext context, IOrganizationService service) - { - A.CallTo(() => service.Create(A._)) - .ReturnsLazily((Entity e) => - { - var request = new CreateRequest(); - request.Target = e; - var response = service.Execute(request) as CreateResponse; - return response.id; - }); - } - - private static void AddFakeUpdate(IXrmFakedContext context, IOrganizationService service) - { - A.CallTo(() => service.Update(A._)) - .Invokes((Entity e) => - { - var request = new UpdateRequest(); - request.Target = e; - service.Execute(request); - }); - } - - private static void AddFakeRetrieveMultiple(IXrmFakedContext context, IOrganizationService service) - { - //refactored from RetrieveMultipleExecutor - A.CallTo(() => service.RetrieveMultiple(A._)) - .ReturnsLazily((QueryBase req) => { - var request = new RetrieveMultipleRequest { Query = req }; - - var response = service.Execute(request) as RetrieveMultipleResponse; - return response.EntityCollection; - }); - } - - private static void AddFakeRetrieve(IXrmFakedContext context, IOrganizationService service) - { - A.CallTo(() => service.Retrieve(A._, A._, A._)) - .ReturnsLazily((string entityName, Guid id, ColumnSet columnSet) => - { - var request = new RetrieveRequest() - { - Target = new EntityReference() { LogicalName = entityName, Id = id }, - ColumnSet = columnSet - }; - - var response = service.Execute(request) as RetrieveResponse; - return response.Entity; - }); - } - - private static void AddFakeDelete(IXrmFakedContext context, IOrganizationService service) - { - A.CallTo(() => service.Delete(A._, A._)) - .Invokes((string entityName, Guid id) => - { - if (string.IsNullOrWhiteSpace(entityName)) - { - throw new InvalidOperationException("The entity logical name must not be null or empty."); - } - - if (id == Guid.Empty) - { - throw new InvalidOperationException("The id must not be empty."); - } - - var entityReference = new EntityReference(entityName, id); - - var request = new DeleteRequest() { Target = entityReference }; - service.Execute(request); - }); - } - - private static void AddFakeAssociate(IOrganizationService service) - { - A.CallTo(() => service.Associate(A._, A._, A._, A._)) - .Invokes((string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection entityCollection) => - { - var request = new AssociateRequest() - { - Target = new EntityReference() { Id = entityId, LogicalName = entityName }, - Relationship = relationship, - RelatedEntities = entityCollection - }; - service.Execute(request); - }); - } - - private static void AddFakeDisassociate(IOrganizationService service) - { - A.CallTo(() => service.Disassociate(A._, A._, A._, A._)) - .Invokes((string entityName, Guid entityId, Relationship relationship, EntityReferenceCollection entityCollection) => - { - var request = new DisassociateRequest() - { - Target = new EntityReference() { Id = entityId, LogicalName = entityName }, - Relationship = relationship, - RelatedEntities = entityCollection - }; - service.Execute(request); - }); - } - } } \ No newline at end of file