Class ModelResultExtension
Extension for IModelResult.
note
To simplify the examples and present actual use cases, a base code was created below.
public class Foo : IAggregateRoot
{
private Foo() { }
public string Name { get; private set; }
public static IModelResult<Foo> NewFoo(string name)
{
var foo = new Foo { Name = name };
return Validate(foo);
}
private static IModelResult<Foo> Validate(Foo foo)
{
var result = new ModelResult<Foo>();
if (string.IsNullOrWhiteSpace(foo.Name))
result.AddValidation("Name", "The field is required");
if (result.IsModelResultValid())
result.SetModel(foo);
return result;
}
}
Sample of the request FooMessageRequest.
[DataContract(Namespace = "http://stone.com.br/type/")]
public class FooMessageRequest : RequestMessage
{
[DataMember(Name = "name")]
public string Name { get; set; }
}
Sample of the response FooMessageResponse.
[DataContract(Namespace = "http://stone.com.br/type/")]
public class FooMessageResponse : ResponseMessage
{
[DataMember(Name = "name")]
public string Name { get; set; }
}
Sample of the service layer with contract IFooService.
public interface IFooService
{
IModelResult<Foo> CreateNew(string name);
IModelResult ExecuteRuleOfFoo();
IModelResult<Foo> GetFoo(string name);
}
Inheritance
Inherited Members
Namespace: StoneCo.Framework.Application.Extension
Assembly: cs.temp.dll.dll
Syntax
public static class ModelResultExtension
Methods
ToResultResponseMessage<TRequestMessage>(IModelResult, TRequestMessage)
Get ResultResponseMessage from an IModelResult.
Declaration
public static ResultResponseMessage ToResultResponseMessage<TRequestMessage>(this IModelResult modelResult, TRequestMessage requestMessage)
where TRequestMessage : RequestMessage
Parameters
Type | Name | Description |
---|---|---|
IModelResult | modelResult | Instance of the ModelResult that implement IModelResult. |
TRequestMessage | requestMessage | Request that will be referenced in the response. |
Returns
Type | Description |
---|---|
ResultResponseMessage | ResultResponseMessage with values of the IModelResult. |
Type Parameters
Name | Description |
---|---|
TRequestMessage | The type of the request that will be referenced in the response. |
Examples
Example of the application layer with concret class FooApplicationService and implementation of the method ExecuteRuleOfFoo.
This sample represents a validation in the business rules flow that is implemented in the service layer and mapping the validation to ResultResponseMessage.
In this case, the response was not required as a result of ResultResponseMessage.
public class FooApplicationService
{
private IFooService FooService { get; }
public FooApplicationService(IFooService fooService) => FooService = fooService;
public ResultResponseMessage ExecuteRuleOfFoo(RequestMessage request)
{
var fooResult = FooService.ExecuteRuleOfFoo();
return fooResult.ToResultResponseMessage(request);
}
}
ToResultResponseMessage<TRequestMessage, TModel>(IModelResult<TModel>, TRequestMessage)
Get ResultResponseMessage from an IModelResult.
Declaration
public static ResultResponseMessage ToResultResponseMessage<TRequestMessage, TModel>(this IModelResult<TModel> modelResult, TRequestMessage requestMessage)
where TRequestMessage : RequestMessage where TModel : IModel
Parameters
Type | Name | Description |
---|---|---|
IModelResult<TModel> | modelResult | Instance of the ModelResult that implement IModelResult. |
TRequestMessage | requestMessage | Request that will be referenced in the response. |
Returns
Type | Description |
---|---|
ResultResponseMessage | ResultResponseMessage with values of the IModelResult. |
Type Parameters
Name | Description |
---|---|
TRequestMessage | The type of the request that will be referenced in the response. |
TModel | The type of the model referenced in IModelResult. |
Examples
Example of the application layer with concret class FooApplicationService and implementation of the method CreateNew.
This sample represent the fields validation on model and mapping this validations to ResultResponseMessage.
In this case, the response was not required as a result of ResultResponseMessage.
public class FooApplicationService
{
private IFooService FooService { get; }
public FooApplicationService(IFooService fooService) => FooService = fooService;
public ResultResponseMessage CreateNew(FooMessageRequest request)
{
var fooResult = FooService.CreateNew(request.Name);
return fooResult.ToResultResponseMessage(request);
}
}
ToResultResponseMessage<TRequestMessage, TResponseMessage, TModel>(IModelListResult<TModel>, TRequestMessage)
Get ResultResponseMessage from an IModelListResult.
Declaration
public static ResultResponseMessage<TResponseMessage> ToResultResponseMessage<TRequestMessage, TResponseMessage, TModel>(this IModelListResult<TModel> modelListResult, TRequestMessage requestMessage)
where TRequestMessage : RequestMessage where TResponseMessage : ResponseMessage where TModel : IModel
Parameters
Type | Name | Description |
---|---|---|
IModelListResult<TModel> | modelListResult | Instance of the ModelListResult that implement IModelResult. |
TRequestMessage | requestMessage |
Returns
Type | Description |
---|---|
ResultResponseMessage<TResponseMessage> | ResultResponseMessage with values of the IModelResult. |
Type Parameters
Name | Description |
---|---|
TRequestMessage | The type of the request that will be referenced in the response. |
TResponseMessage | The type of the response to be returned in the result. |
TModel | The type of the model referenced in IModelResult. |
Examples
Sample of the application layer with class FooMessageMapper.
public class FooMessageMapper
{
public static FooMessageResponse MapFrom(Foo model)
{
if (model == null)
return new FooMessageResponse();
return new FooMessageResponse { Name = model.Name };
}
}
Example of the application layer with concret class FooApplicationService and implementation of the method GetFoo.
This sample represent the validation on model or in business rules flow, mapping to ResultResponseMessage.
In this case, the response is setted on the result of ResultResponseMessage.
public class FooApplicationService
{
private IFooService FooService { get; }
public FooApplicationService(IFooService fooService) => FooService = fooService;
public ResultResponseMessage<FooMessageResponse> GetFoo(FooMessageRequest request)
{
var fooResult = FooService.GetFoo(request.Name);
var result = fooResult.ToResultResponseMessage<FooMessageRequest, FooMessageResponse, Foo>(modelListResult, request);
var response = FooMessageMapper.MapFrom(fooResult.Model);
result.SetReturn(response);
return result;
}
}
ToResultResponseMessage<TRequestMessage, TResponseMessage, TModel>(IModelResult<TModel>, TRequestMessage)
Get ResultResponseMessage from an IModelResult.
Declaration
public static ResultResponseMessage<TResponseMessage> ToResultResponseMessage<TRequestMessage, TResponseMessage, TModel>(this IModelResult<TModel> modelResult, TRequestMessage requestMessage)
where TRequestMessage : RequestMessage where TResponseMessage : ResponseMessage where TModel : IModel
Parameters
Type | Name | Description |
---|---|---|
IModelResult<TModel> | modelResult | Instance of the ModelResult that implement IModelResult. |
TRequestMessage | requestMessage | Request that will be referenced in the response. |
Returns
Type | Description |
---|---|
ResultResponseMessage<TResponseMessage> | ResultResponseMessage with values of the IModelResult. |
Type Parameters
Name | Description |
---|---|
TRequestMessage | The type of the request that will be referenced in the response. |
TResponseMessage | The type of the response to be returned in the result. |
TModel | The type of the model referenced in IModelResult. |
Examples
Sample of the application layer with class FooMessageMapper.
public class FooMessageMapper
{
public static FooMessageResponse MapFrom(Foo model)
{
if (model == null)
return new FooMessageResponse();
return new FooMessageResponse { Name = model.Name };
}
}
Example of the application layer with concret class FooApplicationService and implementation of the method GetFoo.
This sample represent the validation on model or in business rules flow, mapping to ResultResponseMessage.
In this case, the response is setted on the result of ResultResponseMessage.
public class FooApplicationService
{
private IFooService FooService { get; }
public FooApplicationService(IFooService fooService) => FooService = fooService;
public ResultResponseMessage<FooMessageResponse> GetFoo(FooMessageRequest request)
{
var fooResult = FooService.GetFoo(request.Name);
var result = fooResult.ToResultResponseMessage<FooMessageRequest, FooMessageResponse, Foo>(request);
var response = FooMessageMapper.MapFrom(fooResult.Model);
result.SetReturn(response);
return result;
}
}