Search the web
Sign In
New User? Sign Up
altdotnet · Alt Dot.Net Discussions
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
AppLayer API   Message List  
Reply | Forward Message #22365 of 23254 |
Re: [altdotnet] Re: AppLayer API

> So IConstraintProvider<User> will return for example StringIsNotNull<User>, so I cannot directly apply those to the result of
> IConstraintProvider<UserDTO> because when I try to evaluate IsValid I would get an invalid cast.

Needs a bit of work but the code looks a bit like this:

var rulesToApplyToTarget = new DomainRuleSet<TTargetOfRules>();

foreach (IPropertyMappingDefinition propertyMappingDefinition in propertyMappings)
{
    var ruleDefinitionsForProperty = rulesToMap.RulesFor(propertyMappingDefinition.FromProperty);

    ruleDefinitionsForProperty.ForEach(ruleDefinition =>
                                {
                                    var newRuleDefinition =
                                            ruleDefinition.CloneForUseOn<TTargetOfRules>(propertyMappingDefinition.ToProperty);

                                    rulesToApplyToTarget.Add(newRuleDefinition);
                                });
}

Worth a bit of explanation, which probably indicates I should refactor, but essentially propertyMappings describes the mappings between the domain and DTO, each knows which property it is mapping to/from (ignore value objects for now). So I work out the rules for each property, and as you can see above for each of them we can create a new rule definition by calling CloneForUseOn:

public IReflectionBasedRuleDefinition<TTarget> CloneForUseOn<TTarget>(PropertyInfo property)
{
      return new ReflectionBasedRuleDefinition<TTarget, TProperty>(property, this.Rule);
}

public IDomainRule<TProperty> Rule { get; set; }

So you've got a new RelectionBasedRuleDefinition but setup for the property on the target, at run-time its executed like this against the target (the DTO):

public bool IsSatisfiedBy(TContainingProperty model)
{
       TProperty toValidate = (TProperty)this.PropertyRuleRelatesTo.GetValue(model, null);

       return this.Rule.IsSatisfiedBy(toValidate);
}

That cast can ofcourse fail. Anyway I'd fully accept its a bit convoluted, though actually using it is easy and all this is hidden away. Having said that its not been put through its paces on a big project yet and obviously there might be a better solution, and perhaps after trying it for a while I'll decide that just duplicating the rules on the DTO is fine.


2009/7/3 Peter Morris <mrpmorris@...>


> If you are interested I could blog about the approach, its not perfect but I far prefer it to most other approaches I've seen/used.

I'd certainly be interested, more information is always better and besides I can always discard it if I think it is rubbish :-)

What I wanted to do at one point was to have my IConstraintProvider<UserDTO> do something like this

return
from ucp in UserConstraintProvider.GetConstraints()
where
ucp.FieldName == "FullName"
|| ucp.FieldName == "EmailAddress"
select
ucp;

The problem is that IConstraintProvider<T> returns IConstraint, but these are implemented like so

public class StringIsNotNullConstraint<TInstance> :
BaseConstraint<TInstance, string>
{
public StringIsNotNullConstraint(.....Func<TInstance, string> getValue)
{
....
}

public bool IsValid(object instance)
{
string valueToValidate = GetValue((User)instance);
return valueToValidate != null;
}
}

So IConstraintProvider<User> will return for example StringIsNotNull<User>, so I cannot directly apply those to the result of IConstraintProvider<UserDTO> because when I try to evaluate IsValid I would get an invalid cast.

I like having my constraints as generic because then I can apply them to different classes...

new StringIsNotNullConstraint<User>(u => u.Title);
new StringIsNotNullConstraint<User>(u => u.FirstName);
new StringIsNotNullConstraint<User>(u => u.LastName);
or
new StringIsNotNullConstraint<Country>(u => u.IsoCode);
new StringIsNotNullConstraint<Country>(u => u.Name);

but its strength is also its weakness in this case, so I am torn!

Let me know when your blog is done :-)

Pete




Fri Jul 3, 2009 10:01 pm

colin.jack
Offline Offline
Send Email Send Email

Forward
Message #22365 of 23254 |
Expand Messages Author Sort by Date

Hi all I'm playing with a hobby application, the UI is ASP MVC and it uses NHibernate for the ORM. All I want my ASP layer to do is to interact with the app...
Peter Morris
mrpmorris
Offline Send Email
Jun 25, 2009
4:47 pm

Peter, While some parts of a view model will (at least initially) remain fairly similar in structure to the domain model itself other parts will vary ...
Jeff Brown
xeeyore42
Online Now Send Email
Jun 25, 2009
5:09 pm

Well put, Jeff. I struggle with this constantly myself - the lazy side always kicks in and screams "but they're the same!" when comparing the view and domain...
Matt Burton
matt.burton
Offline Send Email
Jun 25, 2009
5:19 pm

http://vistasquad.co.uk/blogs/announcements/archive/2009/05/03/asp-net-mvc-best-practices-video.aspx ... ...
Sebastien Lambla
serialseb
Offline Send Email
Jun 26, 2009
10:08 am

... always kicks in and screams "but they're the same!" WHat if they are the same and remain the same? Should we not wait for change and refactor? Automapper...
Paul Cowan
dagda1970
Offline Send Email
Jun 26, 2009
10:36 am

... In theory this sounds wonderful, in reality however...based on my experience, do it up front so you don't have to accumulate the "technical backlog" that...
Matt Burton
matt.burton
Offline Send Email
Jun 26, 2009
3:42 pm

I can cope with the similarity of structure. It's the constraints that are concerning me. If I change the maximum and minimum lengths of Salutation then I...
Peter Morris
mrpmorris
Offline Send Email
Jun 26, 2009
3:07 pm

... I don't believe it is so bad for the UI to "know about" domain classes, but in any case, can it really be that bad for the UI to know about domain...
Adelle Hartley
adellehartley
Offline Send Email
Jun 26, 2009
5:31 pm

... What I have in the app layer is an IConstraintProvider<T> interface. Each agg-root has its own defined. It has a method like so IEnumerable<Constraint>...
Peter Morris
mrpmorris
Offline Send Email
Jun 28, 2009
9:31 am

Out of curiousity, how do you generate HTML-validators from constraints? Any pointers about patterns/tools you're using? And, this might not answer your...
Hendry Luk
hendrypa
Offline Send Email
Jun 29, 2009
12:45 am

... I'm not yet, it's one of my goals. Here is an example of a constraint I have at the moment. public class StringIsNotTooLongConstraint<TInstance> :...
Peter Morris
mrpmorris
Offline Send Email
Jun 29, 2009
4:39 pm

... some of these constraint types (ones that will work) to javascript automatically and have them validated on the client. A worthy goal IMO. I've gone about...
Adelle Hartley
adellehartley
Offline Send Email
Jun 30, 2009
3:19 am

Thanks for the time to explain. Transforming constraints to javascript is a really interesting work. I am particularly interested to see similar thing with...
Hendry Luk
hendrypa
Offline Send Email
Jun 30, 2009
6:20 am

... I think it's just the naming that is questionable. It's not a view model, it's more of an API class. For input it is easy to use multiple parameters, but...
Peter Morris
mrpmorris
Offline Send Email
Jun 29, 2009
4:51 pm

Very interesting, Have you looked at Silverlight RIA services? It includes validation as metadata. Basically, you implement a sublclass of a validation...
Michael D. Brown
mbrebrown
Offline Send Email
Jun 29, 2009
5:32 pm

... The approach we're using, and it is early days, is to put all validation on the model and use PostSharp to copy it to any related classes (view models, WCF...
Colin Jack
colin.jack
Offline Send Email
Jul 1, 2009
7:22 pm

... I would be interested in the approach. Cheers Paul Cowan Cutting-Edge Solutions (Scotland) http://thesoftwaresimpleton.blogspot.com/ 2009/7/1 Colin Jack...
Paul Cowan
dagda1970
Offline Send Email
Jul 1, 2009
8:23 pm

I would be very interested in a blog post on this approach. I have always loved the idea of using PostSharp for validation, but I haven't had a chance to put...
Erick Thompson
renton_et
Offline Send Email
Jul 2, 2009
2:46 am

I'll definitely blog about it, like I say its not perfect and we've yet to put it through its paces on a big system but I think it has some merit even if you...
Colin Jack
colin.jack
Offline Send Email
Jul 2, 2009
9:49 am

How have you found the compilation time of postsharp? Last time I looked at it was a bit off putting but I must stress that that was a year and a half ago. ...
Paul Cowan
dagda1970
Offline Send Email
Jul 2, 2009
10:03 am

Hi, Newer versions have made it better but yeah it still really adds to the build time: http://www.postsharp.org/blog/announcing-postsharp-1.5-ctp-3 We're...
Colin Jack
colin.jack
Offline Send Email
Jul 2, 2009
12:40 pm

... I'd certainly be interested, more information is always better and besides I can always discard it if I think it is rubbish :-) What I wanted to do at one...
Peter Morris
mrpmorris
Offline Send Email
Jul 3, 2009
7:20 am

... StringIsNotNull<User>, so I cannot directly apply those to the result of ... would get an invalid cast. Needs a bit of work but the code looks a bit like...
Colin Jack
colin.jack
Offline Send Email
Jul 3, 2009
10:03 pm

... trick ... resemble each ... repetition (in ... Well said. You have convinced me that the view model and domain model should be logically distinct. On the...
Adelle Hartley
adellehartley
Offline Send Email
Jun 26, 2009
5:16 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help