Changing application using MVVM

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 10453 Views - Last Post: 06 November 2013 - 12:25 PM Rate Topic: -----

#16 MrShoes  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: Changing application using MVVM

Posted 04 November 2013 - 02:01 PM

Don't get too tied down to a single View being a UserControl. A Window is perfectly valid as a View. Yes, you should get your ViewModel to call it as and when needed.
Was This Post Helpful? 1
  • +
  • -

#17 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: Changing application using MVVM

Posted 05 November 2013 - 03:07 AM

I've always found Josh Smith's tutorial pretty good
I would advice you to download his demo app, and use his ViewModelBase class. It just makes working with WPF so much easier
The tutorial shows you how to validate input :)
His base class also warns you about bad bindings when debugging, which is pretty neat
My last advice would be to copy the code he has put into the finalize method. The code tells you when the object is being garbage collected, something I have had great use of
Was This Post Helpful? 1
  • +
  • -

#18 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Changing application using MVVM

Posted 05 November 2013 - 08:34 AM

CasiOo, that tutorial is AWESOME! It's exactly what I needed.

Edited because I answered my own question

This post has been edited by synlight: 06 November 2013 - 06:41 AM

Was This Post Helpful? 0
  • +
  • -

#19 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Changing application using MVVM

Posted 06 November 2013 - 06:51 AM

Okay, I have another question. I'm about 80% done with the business logic, so almost there!

I have a window (View) that uses a WebBroswer control. In my old application, I have a helper class (Device Viewer) that works like this:

1. get device selection from data grid on a Select Device Window
2. query database for device object data
3. use device data to piece together HTTP headers
4. pass headers back to new WebBroswer window for navigation


Should this logic now be in the ViewModel for the WebBroswer window, instead of in it's own model?
Was This Post Helpful? 0
  • +
  • -

#20 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: Changing application using MVVM

Posted 06 November 2013 - 10:53 AM

Your ViewModel should not have too many responsibilities, instead delegate them out
Let your event handlers be small & skinny, which will help you in several ways. Two of them being testing and reusing code
I would not say your ViewModel should be dealing with the http headers directly. It doesn't sound like this ViewModel has anything to do directly with the web, so let someone else handle it
Your model could have the responsibility of assembling the headers :)
//View model logic
DeviceRepository repository = new DeviceRepository();
DeviceData data = repository.getData(selectedDevice);
//Pass it on to somewhere else, maybe navigate to the new view supplying the data as argument


//Method inside DeviceData
public HttpHeaders AssembleHeaders() {
    //TO-DO....
}


Have I misunderstood your question, or does my answer cover some of it?

edit: You would usually initialize your viewmodel with the repositories it is gonna need instead of creating them locally in a method. Makes testing so much easier

This post has been edited by CasiOo: 06 November 2013 - 10:55 AM

Was This Post Helpful? 0
  • +
  • -

#21 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Changing application using MVVM

Posted 06 November 2013 - 11:22 AM

Yes, that does answer most of my question, thank you!

I'm a little unclear on what repositories are. Previously, I was just using my application database to pull device data. So.. I should place my database inside a repository when the software fires up?

Edit: Currently stuffing my brain with an MSDN article about the Repository Pattern. This is all so much information to ingest at once.

This post has been edited by synlight: 06 November 2013 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#22 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: Changing application using MVVM

Posted 06 November 2013 - 11:34 AM

Quote

I should place my database inside a repository when the software fires up?

I'm not sure I understand you correctly on that one

There are MANY tutorials on how to use the repository pattern
Are you using Entity Framework? It works nicely together with it
A small example of how it could be implemented
    public class MatchRepository : Repository<Match> {

        public MatchRepository()
            : base(new TableSoccerContext()) {

        }

        public MatchRepository(DbContext context)
            : base(context) {

        }

        public IList<Match> GetMatches(Team team) {
            var matches = from match in dbSet
                          where match.BlueID == team.ID || match.RedID == team.ID
                          select match;

            return matches.ToList<Match>();
        }

        public IList<Match> GetMatches(Team team1, Team team2) {
            var matches = from match in dbSet
                          where (match.BlueID == team1.ID && match.RedID == team2.ID) ||
                                (match.BlueID == team2.ID && match.RedID == team1.ID)
                          select match;

            return matches.ToList<Match>();
        }
    }


The repository is communicating with the database, or as in this case through entity framework
You will often find generic repositories that have common implementations for GetById(id), Save(entity), Update(entity), Delete(entity) etc.
Was This Post Helpful? 0
  • +
  • -

#23 synlight  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 582
  • Joined: 14-September 11

Re: Changing application using MVVM

Posted 06 November 2013 - 11:49 AM

Nope, not using the Entity Framework. I have MrShoesspectacular tutorial bookmarked, and on my "TODO" list. (ha! just found the member feature)

I have just been directly using my SQLite database since my app just uses basic CRUD commands.
Was This Post Helpful? 0
  • +
  • -

#24 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1571
  • View blog
  • Posts: 3,529
  • Joined: 05-April 11

Re: Changing application using MVVM

Posted 06 November 2013 - 12:25 PM

I STRONGLY recommend this course
The videos are amazing, Jon Galloway sure knows his stuff :)
Skip forward to where they begin fooling around with entity framework if you want to learn some basics. Hearing them talk about ASP.NET MVC is worth it even if you are not going to use it at the moment
They are doing it all code-first with entity framework which I like :)

This post has been edited by CasiOo: 06 November 2013 - 12:26 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2