Friday, May 10, 2013

XML Schema and SSDT Sql compare

I am really enjoying the Sql Compare tool in Visual Studio 2012.  One rough edge is when working with XML Schemas in sql.  Sometimes they update sometimes not.  In development I just drop my table and re-run the schema compare.  In deployment, that will have to be scripted.  The advantage is I don't have to write scripts during development.  This is a nice time saver.

Wednesday, May 8, 2013

Why AngularJs MVC is Different

In the Asp.net MVC framework and in Sencha each layer is clearly defined.  In AngularJs the model is 'compiled' for lack of a better term.

In AngularJs the model definition can occur in many different places, this is why it is different.  Adding a new model item to a form is simple.  In an html form with an html input node, simply add an html attribute of ng-model="person.name".  When the page is compiled, AngularJs crawls the DOM, and automatically binds all of the ng-model directives to the $scope object.  $scope is where we find the M in MVC.  Scope also contains other things.

Reading from the model is also easy, simply wrap it in curly braces in the html.
<div>{{person.name}}</div>

When I was first learning AngularJS the examples confused me.  I often thought  "why I would want to see what the user was typing somewhere other than in the input box?"  You don't!  In your form you don't want to have {{person.name}} display on the input form.  It is redundant.  You will want {{person.name}} inside of ng-repeat for an index page that lists all the records on the page.


Generating the model from a form is not the only way.  Usually the model is populated from a service located in a controller.  Note, the $scope variable is passed in via the dependency injection framework in AngularJs.  $scope is a canned object that is injected into your method.  After a while your controllers start to have many things passed in.  This is just the simplest example.

var MyCtrl = function($scope){
  id = '';
 $scope.person = MySvc.getById(id);
}





Thursday, May 2, 2013

Re-Usable Directive Base in AngularJS

This post describes one method for making a re-usable directive base in AngularJs.  There are other patterns than can be used to wrap this method.

I have to admit something about this feels wrong!  But it works.

Look for the RED  text in the code, this re-mapping of data is what makes this pattern work.  If you try using "this.someArg1" directly in the code...'undefined' error.

I would not use this 'pattern' if you are making a one off directive.  I am making directives for xml documents.  With 10 different xml nodes that all need the same base...I don't want to copy and paste that much code.

I'll show the usage first to add some context to the myFancyBase function. "p:" stands for person in a random xml example.

MyApp.directive('p:Name',['dependency1','dependency2',function(d1,d2){
    var fb = myFancyBase().getBase();
     fb.someArg1 = d1.foo;
     fb.someArg3 = 'whatever';
     return fb.getDirective();
}])

I've color coded things to give you a headache and to help match up instances of the same vars.

var myFancyBase = function(){

   var getBaseFn = function(){
            //This mapping just feels wrong
             var someArg1=this.someArg1,
             someArg2 =this.someArg2,
             someArg3=this.someArg3;

            var getDirectiveBaseFn = function(){
                          priority: 50,
                          replace: false,
                          transclude: false,
                          restrict: 'E',
                          scope: false,
                          link: function(scope, element, attrs) {
                             //special sauce for my fancy directive
                              var x= foo(someArg1);
                              var y = bar(x + someArg2);
                          }
                   }

        return{
            getDirectiveBase:getDirectiveBaseFn,
            someArg1:undefined,
            someArg2:'my default setting',
            someArg3:undefined
         };
    }

  return{
     getBase:getBaseFn
  };
}

Wednesday, May 1, 2013

.Net WebApi - four months later

In WCF it is easy to have one big class/service that is loaded with functions.  In WebApi it is best to limit the commands to four or five actions.

  • GET()
  • GET(id)
  • GET(id, oneMoreVar) - I don't recommend this unless it really makes sense
  • POST
  • PUT
  • DELETE
  • OPTIONS- if you want to get real fancy.
Don't try to make your own verbs unless you know all the browsers you need to support allow for custom verbs.  Internet Explorer 8 does not support custom verbs.

In one app I've recently worked on, the data was being stored as xml in sql.  I would highly recommend one WebApi controller per xml column and one WebApi controller for managing the non-xml columns.  Having a controller just for the SqlXml DML works nicely for 'PATCH' style commands, like replacing the text in an xml node vs. overwriting the whole document.

I would also recommend creating your own abstract base class for the WebApi controllers.  The base class will inherit from the built in ApiController base class.

When it comes to testing, create abstract base classes for your test classes. One for each verb.  The set up is fairly different for each verb and once the controller is initialized you are stuck with what you've got in your setup. 
 
If the controller logic is minimal and if you have a strong abstract base class for the WebApi Controllers, you may need to only test your first controller.  In my case the inherited controllers only had a few lines of code and no logic.  I could have even abstracted them away to a config based solution, but I like the option of being able to override base classes if needed.


Tuesday, April 30, 2013

CKEditor and AngularJs Modal boxes

There is a AngularJs timing issue when adding the ckEditor rich text editor to a page when using ng-include to load modal boxes.  The code below nicely solves this problem using a directive.

I can't take credit for writing this code, found it on http://stackoverflow.com/questions/11997246/bind-ckeditor-value-to-model-text-in-angularjs-and-rails/12021632#12021632


angular.module('yourApp').directive('ckEditor',function () {
    return {
        require: '?ngModel',
        link: function (scope, elm, attr, ngModel) {
           
            var ck = CKEDITOR.replace(elm[0],
                {
                    toolbarGroups: [
                      { name: 'clipboard', groups: ['clipboard', 'undo'] },   
                      { name: 'basicstyles', groups: ['basicstyles', 'cleanup'] }
                    ]
                }
            );
            
            if (!ngModel) return;

            ck.on('pasteState', function () {
                scope.$apply(function () {
                    ngModel.$setViewValue(ck.getData());
                });
            });

            ngModel.$render = function (value) {
                ck.setData(ngModel.$viewValue);
            };
        }
    };
});


Saturday, April 6, 2013

Sql Data Projects in VS2012

I highly recommend using sql data projects in VS2012.  I'm not an expert.  If there is a better way; use it and drop me a line please.

There are two things that I do 'wrong' or 'backwards'. The two items are creating new database schema's (not tables) and installing xml schema collection objects.

It just works better if I do these tasks in sql studio first, then incorporate them in my project with schema compare. Then, I can use schema compare to create deployment packages, without any help from sql server management studio.

My most often used setting in Compare Schema is "Block on possible data loss".
To find the setting to show the dialog box, look for the cog or sprocket graphic after the scroll.
Compare->Update->Scroll->Cog

Monday, April 1, 2013

Samsung Note II

I have been using Personal Digital Assistants (PDA) since the mid 90's.  I have to say my Note II is the fastest most useful phone.  My usage is 75% PDA the rest laptop, for non work use, maybe some iPad.  I don't write code on it yet. I still prefer a mouse,  keyboard, and two screens for code; do I dare say copying and pasting code?