Thursday, 22 March 2012

From notepad to blogspot

This is a journal that I made in notepad when developing a prediction league application using ASP.NET MVC and WPF. This project was never finished (because I started a contract and never got around to finishing it off). Maybe I'll come back to it one day.

>>>

27/10/2010

Written a command to Add a period:

I've changed the AdminModule so that that I can supply a delegate function to provide a way for the Region Manager to create a view that already has the ViewModel attached. This was just to get the view working with bindings to the view model. Later I will use the unity container to do this.

I exchanged the DateTimePicker for a Calendar component. I used the calendar component because I was having trouble getting the date picker component to update the view model through the binding when typing in a date. I wanted it to update the view model on each key press. I realised that I didn't need this and a calender component would suffice.

Bound the Calender.SelectedDate (Nullable) to the Deadline property on the ViewModel, which in turn wraps the Deadline property on the Period model object.

Bound the Add Button.Command to the AddDeadlineCommand property on the view Model. The AddDeadlineCommand property returns RelayCommand (ICommand) to provide delegates for the view to correctly query whether the command can execute and then Execute the command itself.

For some reason, if the Execute raises an exception, it retries shortly after. Some investigation is required here. Might try a similar scenario on one of the tutorial applications.

Another issue I encountered was that the calendar wouldn't release focus after clicking on a date. I found a solution to this online:

http://stackoverflow.com/questions/2425951/wpf-toolkit-calendar-takes-two-clicks-to-get-focus

Had to put some code in the code-behind, but as this was a view specific issue (i.e. workaround for control mal-behaviour) I think this should be ok. I will probably move this into a base class for views later on.

Tomorrow I'm going to finish this story by having the newly added period appear in a new view that displays the periods.

28/10/2010

I'm now adding the period to a list on the service and the service is now raising an event to let anything know that we have a new period. I'm hooking up to the event so that I can add to an Observable collection of Period Model views. I want to be able to see these as a list on the user interface, but haven't got the bindings right yet.

29/10/2010

ObservableCollection:
http://www.e-pedro.com/2009/04/an-introduction-to-observablecollection-in-wpf/

Have now bound the observable collection to a simple ListView. I've used the ItemsSource to bind the collection and the DisplayMemberPath to bind the Deadline property. This gives me the ability to see the dates in full (including time) and in US format. I'm probably going to have to do something to sort out the formatting of the actual date. I'm also unsure of whether I'm going to be able to hook up to the OnSelected event that could be used by player to select the period to view/predict (or even admin to delete a period), but that's for another day.

I've now refactored so that the List of periods are in a separate view. The module creates both views and composes one into the other. Initially, I had the fancy idea of having a PRISM region on the first view that the other view could register with. This didn't work, I assume because regions cannot be on views, only shells. I realised that I didn't need to do this anyway and used the much simpler method of composition. I kept the ItemControl and just added the view as a control in code.

I've just had my first foray into entity framework. I created a Period entity which I can add to a collection (synonymous with at table) and Save. One limitation with SQL Server Compact is that I could not use Identity fields (or any server generated IDs), so I had to use a Guid (uniqueidentifier) instead. The major problem I am foreseeing is that these entity classes are automatically generated, so I'm not going to be able to add validation rules via IDataErrorInfo. It seems to me that I'm going to have to map between domain model classes and data model classes. Will need to do some further reading up on what is the best way forward.

My last step is to load the Periods back and load the domain model/view models to that the periods are recalled. This I'll do tomorrow.

01/11/2010

This morning I've been learning more about Entity Framework. The most important thing I've learned is that you can use T4 templates to change the code generated by EF. In-fact you can get it to generate plain-old-C#-objects (POCOs) and then have EF inject EntityObject behavior into it a runtime using dynamic proxies... nice! You can even use legacy business POCOs as EntityObjects in this way.

Also, as the generated entity classes are partial classes, IDataErrorInfo methods can be added to a non-generated part of the class. So I can have validation rules on my EF classes after all.

I've watched tutorials on using LINQ to Entities and LINQ methods to perform CRUD operations on hierarchies of data within a context. There were also tutorials on binding to winforms and WPF UI.

In the afternoon, I wanted to stop using SQL Server compact and use SQL Server 2008 R2 Express. The main reason for this is so that I can use a profiler to see what is going on when I use EF. This took me a while to sort out. In the end, I had to create a connection string pointing to an MDF file and then change the connections string to point to the server instance. Now I have it so that I can generate a script from the model and run it in SQL server Management Studio.

02/11/2010

AM: Got sidetracked by Enterprise Integration patterns! An agent left a voice-mail about a Technical Architect role which I initially dismissed out-of-hand. I mean, who wants to draw diagrams for a day-job? Later on I thought I'd have a look at the job spec that he sent through. Much of what they were asking for I had experience of. It mentioned Integration Patterns, so I had to do some googling to find out what they were. It turns out they are a bunch of patterns that I have used over the years, but didn't know what they were called. So I spent the morning making mind-maps of all of these patterns so that I'd remember them and what I used them for. I was thinking of applying for the role after all, but thought better of it in the end. Again, who wants to draw diagrams for a day-job?!

PM: Finally finished the first story. The data-access layer now returns the loaded Periods by calling ToList() on the ObjectSet before it is disposed of.
Before moving onto the next story, I did some refactoring to separate the concerns of Service and data layers. Started looking into the data template for the Period list and selection of periods. I'm thinking that the selected Period can be passed to the new FixtureMaintenanceViewModel, which will then bind to the FixtureMaintenanceViewModel getting the data through a new Fixture service.

03/11/2010

Back to the wonderful world of the prediction league application and adding fixtures to my periods.

Added a new prototype view for the fixture maintenance. The way I'm thinking this should work is: the user selects a period and at that point any fixtures for that period are displayed in the bottom section of the view. Then the admin can add more fixtures by typing in some of a team name, this will list up matching teams in the section just below the text entry box. The admin will then be able to drag the team into either the home or away slot. Once both home and way slots are populated, the Add fixture button will enable allowing the user to add the fixture to the list.

I've made some progress to get the views docking into the regions correctly. Before I was using an ItemsControl as the region, but it appears this is for when you want add multiple views to a region so that they appear vertically stacked. I don't want this, so I've replaced the ItemsControls for UserControls. This stretches the view out so that it takes up the entire region. This looks much better and is ok for what I need as I don't want to display more than one view in a region.

I've also discovered that if I want the root of the user control to autosize to the region, I need to click on the little box at the side of the control at design time. This makes the design size 'ignorable' at runtime.


05/11/2010

Learning point - To use SQL Server 2008 Express with the Database Explorer in VisualStudio 2010 Express, the database has to be called SQLEXPRESS! So, what I should have done was to disable SQL Express 2008 and install SQL Express 2008 R2 with the name SQLEXPRESS... then it would have worked out of the box!

I've refactored out the local model in favour of the global model provided by the POCOs generated by the POCO code generator. This was very easy to do and simplified the service object and the VMs. So now, the VMs wrap instances of the POCOs rather than the local model and this is really neat as it means that no changes are required to the views as they are still bound to the VMs as they were.

It's firework night... it's 7pm and I can hear a load of them going off. I'm going to take a look.

08/11/2010

Had a great weekend. Saw Ghost Stories up the west end, very entertaining! On Sunday, saw one of the planetarium shows at the Greenwich Observatory.

Anyway, back to Prediction League. Today I'm going to try and get the Team search up and running. The idea is that I'm going to bind a command to the edit box fired on a change event to update the Team selection List.

09/11/2010


Ok, it appears that I do not need a command. I just need to fire of the search when the bound search string setter gets called. This should run a LINQ query and update the Observable list. For some reason, even though the observable list is being updated, it's not getting refreshed on the the view control. I'm going to investigate why.

I've now got the found team list updating. The problem was the creating of a new instance of the ObservableCollection each time. I've now got it to clear and repopulate the collection which now reflects on the ListView. What I probably really should be doing is using the ListCollectionView to do the filtering. This way, I will bind the the full list once and then use the ListCollectionView to recalculate the rendered list.

11/11/2010

Today I worked on getting the period selection hooked up to the fixture list. For this, I needed to extend the PeriodSelectonViewModel so that it has a Selected period property that when set, raises a PeriodSelectedEvent. This was done so that the PeriodSelectonViewModel (IPeriodSelection) can be used by the fixture maintenance and the prediction and score views. Then I extended the FixtureMaintenanceViewModel so that it handles the PeriodSelectedEvent and refreshes the Fixture list. However, I've an issue keeping the fixture list populated after disconnecting. Will need to do some investigation on EF eager loading.

30/11/2010

Back to some ASP.NET MVC work. I'm working out how ASP.NET MVC applications are plumbed together. These are the rules:
1) The controller class should be called [ControllerName]Controller
2) The views relating to this controller need to be in a folder called Views/[ControllerName] or Shared
3) The View file (aspx or ascx) should have the same name as an action method(s) in the controller
4) To redirect to a page, you will need the controller name and the action.

03/12/2010

Today I'm going to have a look at some WF material to re-familiarise myself with the technology.
Watched the WF screen-casts up to no 7. This was good material for solidifying my knowledge.

06/12/2010

Watched the remaining WF screencasts in the series. There are a whole load more from PluralSight. These explore the integration of WCF and WF as a composite technology.

07/12/2010

Watched some of the Pluralsight WF

09/12/2010

Done quite a bit of work on the WPF application today: Firstly I investigated into and written the drag & drop code for getting the teams into a fixture. Some of this code had to go in the code behind as it was tracking a couple of mouse events. I would like to know how this could be done in a neater way. I've also written the Add Fixture command that persists the fixture. To finish the story, I just need the fixture list for the current period to refresh. Will look into this tomorrow.

10/12/2010

Finished the story so that the fixture list is now updated when a fixture is added. I've noticed that there is a bug with the drag and drop. The drag needs to be limited to the list items, because if you drag the vertical scroll bar it thinks your dragging a list item and raises an exception on the cast.