Random programming things I'd want to remember

Saturday, March 22, 2014

Code snippet for MVVM command in a ViewModel

Since code snippets are not that hard to do, here is another one. This one gets all the plumbing for a command in a ViewModel, I used DelegateCommand class to abstract all the INotifyPropertyChanged functionality. See my other posts on the subject. Call this one in VisualStudio by its shortcut, dcmvvm.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Plumbing for an MVVM DelegateCommand</Title>
      <Author>yevgeller</Author>
      <Description>Plumbing for an MVVM DelegateCommand for use in a ViewModel</Description>
      <Shortcut>dcmvvm</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>cmd1</ID>
          <ToolTip>Prefix for a private variable that holds command name.</ToolTip>
          <Default>your</Default>
        </Literal>
        <Literal>
          <ID>cmd2</ID>
          <ToolTip>Prefix for a property Name that exposes the command to the View.</ToolTip>
          <Default>Your</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[
        DelegateCommand _$cmd1$Command;
        public ICommand $cmd2$Command { get { return _$cmd1$Command; } }
        private void $cmd2$CommandAction(object obj)
        {
            throw new NotImplementedException();
        }
        //add the following line to ViewModel constructor or wherever else you initialize your commands:
        _$cmd1$Command = new DelegateCommand(this.$cmd2$CommandAction, this.Can$cmd2$);
        private bool Can$cmd2$(object obj)
        {
            return true;
        }
      ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Tuesday, March 18, 2014

Code snippet file for a property that calls for OnPropertyChanged event

With minimal changes, this snippet will also work for PropertyChanged event. How to use:
  1. Save the following contents into a .snippet file
  2. Visual Studio -> Tools -> Code Snippets Manager (or Ctrl+K, Ctrl+B)
  3. Import, find your file
  4. While working on another ViewModel, type in propopc and enjoy

Note to self: while working on the next snippet, if variables don't show up in the result, make sure that they are included in the <Snippet> section

Helpful resource 1, helpful resource 2.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Property with a call to OnPropertyChanged</Title>
      <Author>yevgeller</Author>
      <Description>Property calling OnPropertyChanged method</Description>
      <Shortcut>propopc</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>var</ID>
          <ToolTip>Replace with a private variable.</ToolTip>
          <Default>_privateVar</Default>
        </Literal>
        <Literal>
          <ID>type</ID>
          <ToolTip>Replace with a property type.</ToolTip>
          <Default>string</Default>
        </Literal>
        <Literal>
          <ID>propName</ID>
          <ToolTip>Replace with a property name.</ToolTip>
          <Default>MyProperty</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[
  private $type$ $var$;
         public $type$ $propName$
         {
              get { return $var$; }
              set
              {
                  if ($var$ == value) return;
                  $var$ = value;
                  OnPropertyChanged("$propName$");
              }
         }
      ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Monday, March 17, 2014

One of the uses for Visual Studio's Output window

I am generating some dummy data for testing every time I build and run a program, and I need to look something up every time. Being a lazy programmer, I was wondering if there is a way in Visual Studio to display something during compile time. I found that if execute this code:
System.Diagnostics.Debug.WriteLine("Hello, World!");
Then it will be display along with a bunch of other compilation info in the Output Window. Oh, yeah, Output window in Visual Studio is also extremely useful for displaying XAML binding information.

Wednesday, March 12, 2014

ASP.Net Web API with Angular.js front-end example

I wanted to learn about Microsoft ASP.Net Web API and how I could use Angular.js framework as a front-end. I built a little application that demonstrates the basics.

This app was heavily influenced by this tutorial (especially parts 3-5), and this book's chapter on Web API.

The code is available on Github here.

The project has two controllers: HomeController and RecordController. HomeController is the ASP.Net MVC controller while RecordController deals with Web API. The Web API data infrastructure is in /Models folder. The script file is in /Scripts/Home/record.js. One last thing I need is a view, so there is a view RecordsApi.cshtml under /Views/Home

The view has all the regular angular directives. It uses /Scripts/Home/records.js file which has all the Angular.js stuff. I use $http service to communicate with the server.

In short, ajax calls are replaced with calls to the corresponding methods of Angular's $http service. The view is created as usual, API controller is the same as it would be for a regular Web API service, and models are done in the same way. The only difference you need to make if you want Angular.js front-end is to introduce angular script in the view and put all the directives in place.

Comments/questions are welcome.