Class ModelResult<TModel>
Concrete class that implements the interface with the necessary methods to execute the validations of the models, including the model.
Inherited Members
Namespace: StoneCo.Framework.Model.ModelRules
Assembly: cs.temp.dll.dll
Syntax
public class ModelResult<TModel> : ModelResult, IModelResult<TModel>, IModelResult where TModel : IModel
Type Parameters
Name | Description |
---|---|
TModel | The type of the model to be returned in the result. |
Constructors
ModelResult()
Default constructor that set ModelStatusCode 200 and Message Success.
Declaration
public ModelResult()
ModelResult(Guid)
Default constructor that set ModelStatusCode 200, Message Success and TraceKey.
Declaration
public ModelResult(Guid traceKey)
Parameters
Type | Name | Description |
---|---|---|
Guid | traceKey | Key of trace. |
Properties
Model
Object model of the type TModel.
Declaration
public TModel Model { get; }
Property Value
Type | Description |
---|---|
TModel | Instance of the model or null. |
Validations
Validations of the properties.
Declaration
public IEnumerable<IValidation> Validations { get; }
Property Value
Type | Description |
---|---|
IEnumerable<IValidation> | Validation items if they exist, otherwise, null. |
Methods
AddValidation(String, String)
Add validated fields in the list of validations.
note
If adding a validation, the ModelStatusCode is 422 and Message is Validation.
Declaration
public void AddValidation(string attribute, string message)
Parameters
Type | Name | Description |
---|---|---|
System.String | attribute | The field name validated. |
System.String | message | The message that describes the field validation. |
Examples
Foo class used in this example.
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;
}
}
CreateOk()
Override method to verify if exists items on the Validations.
To simplify development and facilitate traceability, the codes follow the Http status code pattern.
Declaration
public override void CreateOk()
Overrides
Examples
Foo class used in this example.
public class Foo : IAggregateRoot
{
public IModelResult NewFoo() => new ModelResult();
}
SetModel(TModel)
Set object model of the type TModel.
Declaration
public void SetModel(TModel model)
Parameters
Type | Name | Description |
---|---|---|
TModel | model | Instance of the model. |
Examples
Sample using Foo class.
private static IModelResult<Foo> Validate(Foo foo)
{
var result = new ModelResult<Foo>();
result.SetModel(foo);
return result;
}