Entity Framework 4 and CinchV2 Validation (WPF)

Mar 3, 2011 at 1:30 PM

I was hoping you could share a little insite on how you would go about using EF4 with CinchV2. I am having a hard time wrapping my head around how I should set this up. I have looked at your examples and read all your articals on Cinch V1 and V2.

What I believe to be the answer is to take my Viewmodel and put all the same properties into it as my Model wrapping it with a datawrapper to get the Validation I need... This seams like alot of redundent code to validate my model. It may be that I dont know enough about the framework which I am sure is a big part :-)

If you find any free time :-) could you explain

How YOU would use EF4 as the Modeling ORM, and use Cinch V2 to create your MVVM.

What I need to understand is how this combination, can validate inputs and provide feedback to the user like Textbox Highlighting and such.

Also On a side note: When reading your articals on the Mediator it lead me to believe that if we inherit from ViewModelBase that the Mediatior was initialized but I found this not to be the case.. Ie Mediator.Instance.Register(this) was not in the ViewModelBase. Was this a change to Version2?

I know that this might be a crazy request and you probably have more important thing to do but I really like the rest of your framework I just need to figure out this one last piece. :-)

Thanks in advance...

 

Coordinator
Mar 3, 2011 at 4:19 PM
Edited Mar 3, 2011 at 5:20 PM

Ok so you have 2 questions there, the easy one 1st

 

MEDIATOR QUERY

I did decide to not register with the Mediator for every ViewModel instance (by using the ViewModelBase class) in Cinch V2. But that is easy to do you just need to Mediator.Instance.Register(this); anywhere you want to use the Mediator, and Mediator.Instance.Unregister(this) when you want to unregister.

 

MODEL QUERY

Problem is with MVVM is there is no one way. Some people like to expose a current Model (I used to do that too), others expose all the properties needed from the model, and just have the ViewModel expose these properties. If you go down the current model within a Cinch ViewModel you will have to do the validation of the model yourself, and you will need to make sure you EF model objects implement IDataErrorInfo and you will have to do ALL the validation yourself, Cinch will not help you there.

Or what you could do is have a Cinch ViewModel expose the properties required off the model, and either write/get straight through to the backing EF model, or only propagate the changes back to the EF model at certain points, such as get data from EF model on ViewModel load, and only put data back on Valid and Saving or something like that. You could do this with some sort of mapper.

I think I would tend to go for the approach where you have only the properties exposes in the ViewModel that you need from the Model, and you get/set directly to the Model, and that way you can use the standard Cinch validation methods, you would of course also have to ensure you do not save your model if the ViewModel is invalid.

To be honest there are so many ways of doing this its not even funny, its all down to personal preference at the end of the day, Cinch is a MVVM framework, so the emphasis is on the ViewModel, and not so much the Model, but you can do anything you like really, it really is down to you.

There was a message here : http://cinch.codeplex.com/discussions/246989 where that chap wanted to use NHibernate validation and feed those broken rules back into a Cinch ViewModel, that would also work.

Also one thing, EF objects are really server side objects where you would want to persist them using the Entities DataContext, I would tend to transfer POCO objects to the Client (WPF) say using WCF, and then back to the server as POCO, and convert those back into Entity classes, and Attach them to context and save them to DB.

Like I say a million ways to skin a cat really.

 

One last thing, the Cinch.DataWrapper<T> is really only intended to wrap primitives such as string, int, double etc etc, not complex objects. It was really just so I could have a single object which held a value and a IsEnabled for the field where I showed the value to save me time creating loads of ViewModel properties for IsEnabled.

 

 

 

 

 

Mar 4, 2011 at 4:05 AM

Thanks for the fast reply... After looking at the link to sql demo some more I ended up going with a UI model on top of my EF 4 Model. This worked out nicely. What I ended up doing was creating a T4 Template that I could point at my edmx file and it would create ALL my UI Models using Cinch.DataWrappers, static converters, and overrides,Leaving me only needing to add the Rules I required. This Saved me alot of work creating the UI Models.

for the Mediator.Instance.Register(this);

that is what I thought was the issue, but reading the information on codeproject, it isnt updated for that being removed from the base class. It took me some digging to find out why My messages were not being picked up :-)

 

Again thanks This looks like my framework of choice for my next project. Great Work... 

Coordinator
Mar 4, 2011 at 7:03 AM

Cool sounds like you are sorted. As I say I do not use UI Models that hold rules any more, but that certainly is one viable approach that will work.

 

Sorry about the Mediator thing, have a look at the CInch V2 samples for more up to date code demos

Mar 4, 2011 at 3:00 PM

How are you doing it now? I didnt see any examples with V2 using MEF and data validation; Do you have anything available? The V2 documentation pointed to v1 for the mediator as it was listed as "No Change" :-)

Coordinator
Mar 5, 2011 at 4:31 PM

Validation is CInch V1 and V2 is via adding in Rule based classes, this is shown in both the Cinch v2 WPF and SL demos.

 

As for the Mediator the usage of it has not changed from V1 to V2 apart from the fact you need to register your class with the mediator now.