Problem Setting Entity Fields

Posts   
 
    
Scotty
User
Posts: 41
Joined: 24-Jun-2005
# Posted on: 08-Jul-2005 02:59:07   

How are people handling validation using the Manager pattern with Adapter? I have a PL that uses LLBLGen entities directly, passes them off to the BL for validation, and then the BL passes them to the DAL for persistence. My problem is with the validation. When I take the contents of a textbox, for example, on the form and set the value of the corresponding property in the entity, if the user has entered too much data in the textbox and the length would exceed the length allowed in the entity, LLBLGen throws an exception, not allowing the value to be stored in the entity. I won't argue whether this is correct or not, as I see from other posts why Frans implemented it the way he did. The problem is that, because of the exception, the entity is never populated with the control's values. The entity never even makes it to the BL for validation, which is where I would like to catch these data entry errors so I can return a collection of errors to the PL for feedback to the user.

I realize I could manually set the max length of the textboxes in the PL, or do other sorts of validation of the data in the PL to avoid the error, but the idea of the BL is to consolidate the business rules of the system there (I consider length of the data a BR), so I'm a bit confused as to a good way to do this. I hate the idea of having the PL validate the lengths of the textbox fields prior to setting the entity fields, but maybe that is what I have to do. I also don't like the idea of manually setting the maximum lengths of the textboxes. If I make a change in the DB to the length of a field, I now have 2 tiers to deal with regarding the change.

Any ideas or other approaches to consider?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 08-Jul-2005 08:31:49   

I think the main issue is in whether or not the length of data is a business rule. Say you define the length as '100', as the business rule. If the field in the table can only hold 90 characters, your BR might succeed, but the save will fail.

That's why I have a bit of a problem with defining it as a BR, because it doesn't help much. I see the point of having rules independent of database limitations/definitions, though in this case I can't help but think that defining the length as a BR isn't that helpful.

Frans Bouma | Lead developer LLBLGen Pro
Scotty
User
Posts: 41
Joined: 24-Jun-2005
# Posted on: 08-Jul-2005 15:02:46   

Otis wrote:

I think the main issue is in whether or not the length of data is a business rule. Say you define the length as '100', as the business rule. If the field in the table can only hold 90 characters, your BR might succeed, but the save will fail.

You are correct that my BR might succeed, but the save would fail. The problem is that I may not even get to that point where my BR is called, because the PL setting the fields in the entity might throw an exception before the code that passes the entity to the BR for validation is even called. confused

That's why I have a bit of a problem with defining it as a BR, because it doesn't help much. I see the point of having rules independent of database limitations/definitions, though in this case I can't help but think that defining the length as a BR isn't that helpful.

And I certainly respect that opinion as I have seen other posts that say the same thing. Is there a suggested approach for having some "mini-validation" in the PL to test for lengths, before attempting to set the fields in the entity? Somehow, I need to avoid having an exception thrown when an entity's field length is exceeded in the PL, and ideally, not have to recode length limits manually in the PL.

I have been toying with the idea of calling some PL helper class that accepts a list of form controls and each associated entity field, and have it automatically set the maximum length of each form control. I'm just thinking there must be a better way.

Thanks!

stoneyowl avatar
stoneyowl
User
Posts: 62
Joined: 29-Jan-2004
# Posted on: 08-Jul-2005 18:16:44   

I have found that this (while still in the PL) works fairly well....

http://www.codeproject.com/cs/miscctrl/validationprovider.asp

Fishy avatar
Fishy
User
Posts: 392
Joined: 15-Apr-2004
# Posted on: 08-Jul-2005 18:42:39   

I use the following code in the pl:

        txtName.MaxLength = EntityFieldFactory.Create(AssignmentFieldIndex.Name).MaxLength

Scotty
User
Posts: 41
Joined: 24-Jun-2005
# Posted on: 08-Jul-2005 19:33:19   

Those are very good suggestions Stoneyowl and Fishy! Thanks.

I definitely like having a single line of code that simply sets the max length of a control to the maximum length of the field in the entity. I also like the feedback provided to the user with the Code Project sample. I may try to see if that can be adapted to take the error provider messages generated by the BR and use that as the basis for what is displayed on the client. As much as possible, I want the BL to handle the data validation and the PL just to provide feedback to the user.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39615
Joined: 17-Aug-2003
# Posted on: 08-Jul-2005 20:33:49   

Thanks for the suggestions, guys! simple_smile

Frans Bouma | Lead developer LLBLGen Pro
mattlant
User
Posts: 16
Joined: 24-Jun-2005
# Posted on: 09-Jul-2005 00:08:00   

I use variations of this....



        protected virtual void SetTextMaxLengths(Control control)
        {
            foreach(Control c in control.Controls)
            {
                if(c is TextBox && c.DataBindings.Count > 0 && c.DataBindings[0].DataSource is IEntity)
                {
                    ((TextBox)c).MaxLength = ((IEntity)c.DataBindings[0].DataSource).Fields[c.DataBindings[0].BindingMemberInfo.BindingField].SourceColumnMaxLength;
                }
                else if(c.Controls.Count > 0)
                    this.SetTextMaxLengths(c);
            }
        }



I just pass the form and It will find all textboxes and fill max length for me for any field. Of course this only works in a databound scenario and only 1 databinding on a textbox (or the first being the text property). I dont feel its a performanc eissue either as it is fairly fast, Unless you were to have many many many controls on a single form. Even then its still prolly not an issue.

This code just makes it much easier to manage PL rules. similar routines can be used for other scenarios as well.