Thursday, December 20, 2012

What a year. Sometimes is difficult to keep up with technology. It is a great thing that developers have a good community that shares and great learning tools like Pluralsight. Merry Christmas and happy New Year.

This is my version of 12 days of Christmas. Copy in a console application and tell me what you think. (Besides than I am crazy)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TwelveDaysOfChristmas
{
    class Program
    {
   
        static void Main(string[] args)
        {
            Console.WriteLine(TwelveDaysOfChristmas(1,""));
            Console.ReadLine();
        }
        private static string  TwelveDaysOfChristmas(int day,string Screen)
        {
            Screen = Screen + String.Format("On the {0} days of Christmas my Pluralsight gave to me \r\n",getDay(day));
            if (day >= 12)   
                Screen = Screen + "Twelve (courses) of Javascript";         
            if (day >= 11) Screen = Screen + "Eleven (courses) of LINQ + NodeJS + Security \r\n";
            if (day >= 10) Screen = Screen + "Ten courses of debugging \r\n";
            if (day >= 9) Screen = Screen + "Nine (courses) of Entity Framework \r\n";
            if (day >= 8) Screen = Screen + "Eight courses of TFS \r\n";
            if (day >= 7) Screen = Screen + "Seven (courses) of WCF \r\n";
            if (day >= 6) Screen = Screen + "Six courses of architecture \r\n";
            if (day >= 5) Screen = Screen + "FIVE (courses) of JQUERY \r\n";
            if (day >= 4) Screen = Screen + "Four (courses) of C# \r\n";
            if (day >= 3) Screen = Screen + "Three (courses) of CSS \r\n";
            if (day >= 2) Screen = Screen + "Two (courses) of XML \r\n";
            if (day == 1)
                Screen = Screen + "A course of RUBY and HTTP \r\n";
            else
            {
                Screen = Screen + "And a course of RUBY and HTTP \r\n";
               
            }
            day += 1;
            if (day > 12)
                return Screen;
            return TwelveDaysOfChristmas(day,Screen);
        }
        private static string  getDay(int day)
        {
            string DayToReturn="";
            switch (day){
                case 1:
                   DayToReturn = "first";
                   break;
                case 2:
                   DayToReturn = "second";
                   break;
                case 3:
                   DayToReturn = "Third";
                   break;
                case 4:
                   DayToReturn = "Fourth";
                   break;
                case 5:
                   DayToReturn = "Fifth";
                   break;
                case 6:
                   DayToReturn = "Sixth";
                   break;
                case 7:
                   DayToReturn = "Seventh";
                   break;
                case 8:
                   DayToReturn = "Eight";
                   break;
                case 9:
                   DayToReturn = "ninth";
                   break;
                case 10:
                   DayToReturn = "Tenth";
                   break;
                case 11:
                   DayToReturn = "Eleventh";
                   break;
                case 12:
                   DayToReturn = "Twelfth";
                   break;
            }
            return DayToReturn;
        }

  
    }
}

Wednesday, August 8, 2012

Safe thread in a listview. Getting the CheckListViewCollection

I am working with an application in .NET Framework 2.0. I am trying to work with a Safe-Thread method that will help me to get the checkitems in a listview. This is a reminder to my self. I had seen a lot of examples with textboxes, but few with listviews.

 First we need to create a delegate.
'Delegate for getting the CheckListItems
Private Delegate Function GetCheckListItems(lst As ListView) As ListView.CheckedListViewItemCollection

'this maybe in a button
Dim t As New Threading.Thread(AddressOf yourMethod)   
t.start
Private Sub YourMethod()    
'Something TODO....    
Dim x As String = GetValuesFromList()
end
Private Function GetValuesFromList() As String    
'there is not validation of any kind on this.    
'the purpose is just to use a safe-thread with a
' listview.CheckedListViewCollection
Dim mylist As New StringBuilder    
Dim lst As ListView.CheckedListViewItemCollection = getListViewItems(MyListView)    
For Each item As ListViewItem In lst
  mylist.Append(item.Tag.ToString & ",")    
Next    
Return mylist.ToString
End

'This is the safe call. We need to copy the items from one listview to another
' that way we avoided the cross-thead
' It is important set Checkboxes property to true. Without that the selected check items
' will not be copied to the temp variable.
Function Private Function getListViewItems(lstview As ListView) As ListView.CheckedListViewItemCollection    
 Dim temp As New ListView    
 temp.CheckBoxes = True    
If Not lstview.InvokeRequired Then        
 For Each item As ListViewItem In lstview.Items            
     temp.Items.Add(DirectCast(item.Clone(), ListViewItem))        
  Next        
  Return CType(temp.CheckedItems, ListView.CheckedListViewItemCollection)    
Else        
 Return DirectCast(Me.Invoke(
   New GetCheckListItems(AddressOf getListViewItems), New Object() {lstview}),  
   ListView.CheckedListViewItemCollection)    
End If
End Function

Tuesday, August 7, 2012

Installing Ruby and Rails. First try..

I decided to use RubyInstaller.org. There is nothing especial in my choice than just a quick choice. After following the defaults in the installer and click in all the checkboxes in the following picture:
After that I installed also the development kit with all default options. Run the following commands
ruby dk.rb init
ruby dk.rb review
ruby dk. rb install
gem install rails --include-dependencies --no-rdoc --no-ri

Later I tried the command to create a new rails app in a new folder: rails new my_app.
However it fails because apparently I did not have some packages. So I did the following commands:

gem install rake -v '0.9.2.2'
gem install coffee-script-source -v '1.3.3'
gem install execjs - v '1.4.0'
gem install coffe-script -v '2.2.0'
gem install json -v '1.7.4'
gem install rdoc -v '3.12'
gem install coffee-rails -v '3.2.2'
gem install jquery-rails -v '2.0.2'
gem install sass -v '3.1.20'
gem install sqlite3 -v '1.3.6'
gem install uglifier -v '1.2.7'

Then I tried again the command to create the app: rails new my_app.

This time it works.

I will post what will happen next.

Thursday, March 8, 2012

Code First MIgrations

I really like work with the entity framework. It is one of the tools that every developer should use. Entity Framework help us to create a database from scratch using one of the newest version called code first.
I like code first, but one of the biggest problem was that every time that the database was recreated by using any initializer, like dropdatabasealways or dropdatabaseifmodelchanges we ended up loosing data. In the Entity Framework 4.3.1 we find an answer for that problem called migrations. Creating a new database with migrations is easy. However, using an existing database is a little bit challenging. So, instead of showing how work with migrations creating a new database, I am going to show how
to work with migrations with an existing database.

In this example I worked with VS 2010, I used C#, Nuget, Entity Framework powertools and AdventureWorksLT Database.
First thing to do: Create a New Solution in Visual Studio. I followed the same organization that I saw in one of the videos of Julie Lerman (BTW, I just got her book DBContext, yeahhhh).
The order is as follow:
One Console Application App for our main project
Two Class Library, one for our Context (Datalayer), and another one for our domain classes. Which after I created I deleted the default class and I have the following structure:

Now, using Nuget, add entity framework to the solution. The projects that I added were the console app and the datalayer.


Remember that I had installed EF power tools, which you are going to use to get all the tables and the dbcontext for our database doing reverse engineering. Right click on the data layer and click on reverse engineering. After that chose AdventureWorksLT database.


So far, so good. Now we have two folders in our datalayer project, one for entities and another for mapping. EF powertools helped us to do this without write all that code by ourselves.
Then I cut the Entities folder and I paste it into the DomainClasses project. After that change every reference to the Entities in your project.

using DataLayer.Entities;
to
using DomainClasses.Entities;



Of course moving those folders will create references error to our project. One thing you have to do is add the DomainClasses project as reference to the Datalayer, and also change the using clause for both entities and mapping references.

Build your project and you are almost there.

Let's put some code in the Console app.
        static void Main(string[] args)
        {
            var Context = new AdventureWorksLTContext();
            foreach (Address ad in Context.Addresses )
            {
                Console.WriteLine(ad.AddressLine1);
            }
            Console.ReadLine();
        }
Before run this, check your Maps classes (AddressMap, ProductMap, etc.) there is a problem with these classes. Look at this line in the AddressMap class:

this.ToTable("Address");

That line is saying that our table is using dbo schema, which is not. If we open Sql Express we will notice that the schema for almost all the tables is SalesLT, so you should use:

 this.ToTable("Address","SalesLT");

Change that for all the tables that have different schema than dbo.

At this moment our project does not know anything about migrations, so using Nuget let's enable migrations.

Using the Nuget Package Manager Console pointing to DataLayer as Default project run the following command: Enable-Migrations.


After that we need our first migration, which must be empty because we have a database already! so, first we need to run this command on the Nuget Package Manager Console:

Add-Migration Initial

Which gave us a class called initial with two methods, Up and Down. Up create all the tables and Down drop all the tables. But, since we have a database and we do not want to drop it let's delete these two methods so we left them empty.

Also, you have to add the following lines in your app.config in your console app (After defaultConnectionFactory tag):
<entityFramework>
   .....
   .....
   <contexts>
      <context type="DataLayer.AdventureWorksLTContext, DataLayer">
        <databaseInitializer
          type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[DataLayer.AdventureWorksLTContext, DataLayer],
               [DataLayer.Migrations.Configuration,  DataLayer]], EntityFramework" />
      </context>
    </contexts>
<entityFramework>

After that run the following command: update-database, we are going to see the migration table in the system tables in AdventureWorksLT.

Run the console app and you will get the list of addresses. There it is, use code first with an existing database.

Happy Code First Coding!




Sunday, March 4, 2012

Projects Projects

Wow, It is almost spring. I have been busy these days. I will try to upload something before at the end of this month.

Happy coding.