Validator different value based on context

22 views
Skip to first unread message

revin

unread,
Jan 13, 2010, 8:07:41 PM1/13/10
to nhusers
Hi,

I have a structure:

[Serializable]
public struct Address
{
private string _street1;
private string _street2;
private string _suburb;
private string _state;
private string _postcode;
private string _country;
:
}

Struct Address can be used by Employee or Customer.
However, Address in Employee and Customer may have different
constraints.

For example:
Employee.Address.County can only be Australia and NewZealand
However, Customer.Address.Country can by anything (NotNull).

I am using Attribute and Xml file to configure the validation.
Is it possible to achieve?

Regards,
Revin.

Fabio Maulo

unread,
Jan 13, 2010, 9:38:13 PM1/13/10
to nhu...@googlegroups.com
It seem a business-rule instead a entity-constraint

2010/1/13 revin <rs....@gmail.com>
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.






--
Fabio Maulo

revin

unread,
Jan 14, 2010, 3:44:09 PM1/14/10
to nhusers
Thanks.
I will manage it as part of business rule.

Fabio Maulo

unread,
Jan 14, 2010, 3:57:22 PM1/14/10
to nhu...@googlegroups.com
btw you can try if "Validation tagging" match with your needs
Thanks.
I will manage it as part of business rule.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.






--
Fabio Maulo

Fabio Maulo

unread,
Jan 14, 2010, 4:19:51 PM1/14/10
to nhu...@googlegroups.com, nhc...@googlegroups.com
revin... I have re-read your question... perhaps, and I mean perhaps, we can implements a new feature...
So far, for a relationship, we are giving only the ability to define a "cascade-validation" using [Valid].
We can extend it with:
Valid(Type entityValidator) where entityValidator shoud implements IValidator
In this way you can define which is the "cascade-validation" to use from the aggregate.

The other possible improvement is
[Valid(Force = true)]
In this way the user can fore the initialization of the relation/association.

If you think that it is viable please add an issue, for each improvement, in our issue tracker.
Thanks.   

P.S. a copy of this mail was sent to dev-list

2010/1/13 revin <rs....@gmail.com>
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.






--
Fabio Maulo

Fabio Maulo

unread,
Jan 15, 2010, 1:16:23 AM1/15/10
to nhu...@googlegroups.com, nhc...@googlegroups.com
As self-reminder I'll write a post.
This is a possible solution (and there are some others):
public class CustomerValidation : ValidationDef<Customer>
{
public CustomerValidation()
{
Define(e => e.CompanyName).NotNullableAndNotEmpty();
Define(e => e.VatIdentificationNumber).IsPartitaIva();
}
}

public class InvoiceValidation : ValidationDef<Invoice>
{
public InvoiceValidation()
{
ValidateInstance.By((invoice, validationContext) =>
{
bool isValid = true;
if(string.IsNullOrEmpty(invoice.Customer.VatIdentificationNumber))
{
isValid = false;
validationContext.AddInvalid<Customer, string>("To be used in an invoice the Customer should have the VatNumber",
                                              p => p.VatIdentificationNumber);
}
return isValid;
});
Define(e => e.Amount).Satisfy(amount => amount > 2.0m).WithMessage("The amount should be greater than 2€");
}
}

2010/1/14 Fabio Maulo <fabio...@gmail.com>



--
Fabio Maulo

Fabio Maulo

unread,
Jan 15, 2010, 5:35:05 PM1/15/10
to nhu...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages