C# book samples

Though I wanted to keep samples gathered together in one post.
Starting with just a few.

First there is a guide for getting your envir up to date.

Get the environment up to date

Installers

Xaml/Silverligt/WP

1) Via NuGet
2) Included in VS 2010 SP1 – Visual Web Developer Tools part.
3) Included in SL X.0 Tools for VS2010.
4) Controls and components released outside SL release cycle.

Other Xaml Controls (Toolkits)

MVC

  • Start with Windows server 2008 R2 / .NET4.0 / VS2010 SP1 /SQL 2008 (any version)
    Note: VS SP1 includes MVC2
  • Get up to date with MVC3
    • From Web platform Installer search for MVC 3
    • Install MVC 3 (VS 2010)
      This gives you “C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\” and “\Microsoft ASP.NET\ASP.NET Web Pages” (Razor)
      In VS you get a “ASP.NET MVC 3 Web App” project template
  • Get up to date with MVC4
    • From Web platform Installer search for MVC
    • Install latest (currently MVC 4 Tools Update w. Lang. Packs – Aug 2012) 
      this gives you “C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 4\”
      In VS you get a “ASP.NET MVC 4 Web App” project template

MVC Controls (Toolkits)

\Views\Shared\Site.Master – Before:

    	<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
   <script type="text/javascript" src="../../Scripts/jquery-1.6.1.min.js"></script>

\Views\Shared\Site.Master – After:

    <%         var appPath = Url.Content("~/"); //replaces "../../"
         if (!appPath.EndsWith("/")) { appPath += "/"; }
        var hrefPath = @"href=""" + appPath;     %>
   
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" />

    <link <%=hrefPath%>Content/Site.css" rel="stylesheet" type="text/css" />
   <script type="text/javascript" src="<%=appPath%>Scripts/jquery-1.6.1.min.js"></script>

Likewise in \Views\Account\ToDoDisplayItem.ascx – After

    <%: Html.ImgDataButton(DataButtonType.Edit, appPath + "Content/small_pencil.gif", null)%>

The hrefPath is a MVC fix. MVC will add “Views/Shared/” after “href=”, if href is in html instead of inside the asp-tags (“<%”).
The EmulateIE9 prevents IE browsers to go into IE7 mode and thus not use HTML5/CSS3 features like “border-radius”.

Books

Layered applications

  1. MVVM Book: Prism 4.1 – Developer’s Guidesamples
  2. MVVM Book: Building Enterprise Applications with Windows® Presentation Foundation and the Model View ViewModel Pattern
    By Raffaele Garofalo – Samples
  3. MVC Article: Rapid Prototyping, the MVC Working Model – samples
  4. MVC Article: CQRS Journey (Command & Query Responsibility Segregation) – Samples
  5. MVC Links: Several MVC – samples

Ad 1) Prism 4.1

Ad 3) Prototyping with MVC

The sample don’t require higher version than MVC 2.

  • Start with Windows server 2008 R2 / .NET4.0 /SQL 2008 (any version)
  • The sample uses IISExpress. If you don’t want to use that then
    • Edit \RapidPrototypingMVCExamples.csproj – change the line (true to false):
      • <UseIISExpress>false</UseIISExpress>
    • Edit \RapidPrototypingMVCExamples\RapidPrototypingMVCExamples.csproj – change the line from
    • to some other url
  • Otherwise continue with IISExpress. Web Platform Installer might want to go out to fetch it.
    • Next Visual Studio will prompt you for creating a virtual directiry for http://localhost:9393 – Answer yes.
    • Now you can build the solution.
  • Press F5 for debugging the app. This will show a page:
    • Press the “Demo App” button. This gives you:
    • The Demo App shows how flow and placeholder  content and images will be for your app.
  • x

Ad 2) Applications with WPF and MVVM

To compile the sample you need to

  • Start with Windows server 2008 R2 / .NET4.0 /SQL 2008 (any version)
  • Copy the database files to where other SQL database files are located and create the DB by attaching to the files. If the file are located out of the SQL database path you might get an error.
  • Remove the references to the CTP version of Entity Framework (EF)
  • Add reference to Entity Framework via NuGet: http://msdn.microsoft.com/en-us/data/ee712906. Currently it will get EF version 5.0.0
  • Where compile error due to missing “ModelBuilder” replace it with “DbModelBuilder”
  • Add dlls for AvalonDock. See http://nuget.org/packages/AvalonDock or download them from http://avalondock.codeplex.com/releases/view/48794. In my case I installed v1.3.3571. V2.0.x gave a compile error.
  • Now build solution should work.
  • Change the connection string in the projects:
    • CRM.Dal.NHibernate.Fixture \App.config:
      • <addname=DatabaseConnectionconnectionString=Data Source=.;Initial Catalog=MVVMDatabase;Integrated Security=True/>
    • CRM.Dal.EF.Fixture – As above
    • CRM.MVVM.WPF – As above
  • Rebuild Solution
  • Run the Fixtures in the NHibernate project or in the EF project in order to create the database schema:
    • Execute NUnit (located in \Assembly\NUnit-2.5.9.10348\NUnit-2.5.9.10348\bin\net-2.0\)
    • File – Open – \CRM.Dal.NHibernate.Fixture\bin\Debug\CRM.Dal.NHibernate.Fixture.dll
    • Press Run button
    • Verify 3 tests passed
    • Look in tab Text Output to see what has been done. Tables has been created and data has been inserted:
      • ***** CRM.Dal.NHibernate.Fixture.DatabaseFixture.CanCreateACustomerNHibernate: INSERT INTO Person (BirthDate, FirstName, LastName, IsActive, Title, PK_Person) VALUES (@p0, @p1, @p2, @p3, @p4, @p5);@p0 = 29-08-2012 13:18:42 [Type: DateTime (0)], @p1 = ‘Raffaele’ [Type: String (4000)], @p2 = ‘Garofalo’ [Type: String (4000)], @p3 = False [Type: Boolean (0)], @p4 = ‘Mr.’ [Type: String (4000)], @p5 = b2f2d5cb-a0dd-441f-ada0-a0bc00db604d [Type: Guid (0)]NHibernate: INSERT INTO Customer (DefaultAddress, PrimaryKey) VALUES (@p0, @p1);@p0 = NULL [Type: Guid (0)], @p1 = b2f2d5cb-a0dd-441f-ada0-a0bc00db604d [Type: Guid (0)]NHibernate: INSERT INTO Contact (ContactType, Description, IsDefault, Name, Number, PersonPrimaryKey, PrimaryKey) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6);@p0 = 5 [Type: Int32 (0)], @p1 = NULL [Type: String (4000)], @p2 = True [Type: Boolean (0)], @p3 = ‘E-mail address’ [Type: String (4000)], @p4 = ‘raffaeu@raffaeu.com’ [Type: String (4000)], @p5 = b2f2d5cb-a0dd-441f-ada0-a0bc00db604d [Type: Guid (0)], @p6 = 0ecbc1af-0966-4dc1-af24-a0bc00db6062 [Type: Guid (0)]***** CRM.Dal.NHibernate.Fixture.DatabaseFixture.PrepareDatabaseNHibernate: INSERT INTO Person (BirthDate, FirstName, LastName, IsActive, Title, PK_Person) VALUES (@p0, @p1, @p2, @p3, @p4, @p5);@p0 = 12-06-1978 00:00:00 [Type: DateTime (0)], @p1 = ‘Raffaele’ [Type: String (4000)], @p2 = ‘Garofalo’ [Type: String (4000)], @p3 = False [Type: Boolean (0)], @p4 = ‘Mr.’ [Type: String (4000)], @p5 = f6fa9762-26ee-4fcd-8f27-a0bc00db60d5 [Type: Guid (0)]NHibernate: INSERT INTO Customer (DefaultAddress, PrimaryKey) VALUES (@p0, @p1);@p0 = NULL [Type: Guid (0)], @p1 = f6fa9762-26ee-4fcd-8f27-a0bc00db60d5 [Type: Guid (0)]NHibernate: INSERT INTO Contact (ContactType, Description, IsDefault, Name, Number, PersonPrimaryKey, PrimaryKey) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6);@p0 = 5 [Type: Int32 (0)], @p1 = ‘E-mail address’ [Type: String (4000)], @p2 = True [Type: Boolean (0)], @p3 = ‘E-mail’ [Type: String (4000)], @p4 = ‘raffaeu@raffaeu.com’ [Type: String (4000)], @p5 = f6fa9762-26ee-4fcd-8f27-a0bc00db60d5 [Type: Guid (0)], @p6 = 522716d4-627e-4c55-a849-a0bc00db60d6 [Type: Guid (0)]NHibernate: INSERT INTO Address (AddressLine1, AddressLine2, City, Country, IsDefault, State, Town, ZipCode, PrimaryKey) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8);@p0 = ‘4 Henry Vale’ [Type: String (4000)], @p1 = NULL [Type: String (4000)], @p2 = ‘Warwick’ [Type: String (4000)], @p3 = ‘Bermuda’ [Type: String (4000)], @p4 = True [Type: Boolean (0)], @p5 = ‘Bermuda’ [Type: String (4000)], @p6 = ‘Warwick’ [Type: String (4000)], @p7 = ‘PG01’ [Type: String (4000)], @p8 = 989a3a51-cd36-4a5b-bef9-a0bc00db60d6 [Type: Guid (0)]NHibernate: UPDATE Customer SET DefaultAddress = @p0 WHERE PrimaryKey = @p1;@p0 = 989a3a51-cd36-4a5b-bef9-a0bc00db60d6 [Type: Guid (0)], @p1 = f6fa9762-26ee-4fcd-8f27-a0bc00db60d5 [Type: Guid (0)]NHibernate: INSERT INTO Person (BirthDate, FirstName, LastName, IsActive, Title, PK_Person) VALUES (@p0, @p1, @p2, @p3, @p4, @p5);@p0 = 12-06-1978 00:00:00 [Type: DateTime (0)], @p1 = ‘Mary’ [Type: String (4000)], @p2 = ‘Mouse’ [Type: String (4000)], @p3 = False [Type: Boolean (0)], @p4 = ‘Mrs.’ [Type: String (4000)], @p5 = 36293049-9de6-4a32-add6-a0bc00db60ef [Type: Guid (0)]NHibernate: INSERT INTO Employee (PrimaryKey) VALUES (@p0);@p0 = 36293049-9de6-4a32-add6-a0bc00db60ef [Type: Guid (0)]NHibernate: INSERT INTO Contact (ContactType, Description, IsDefault, Name, Number, PersonPrimaryKey, PrimaryKey) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6);@p0 = 5 [Type: Int32 (0)], @p1 = ‘E-mail address’ [Type: String (4000)], @p2 = True [Type: Boolean (0)], @p3 = ‘E-mail’ [Type: String (4000)], @p4 = ‘mary@raffaeu.com’ [Type: String (4000)], @p5 = 36293049-9de6-4a32-add6-a0bc00db60ef [Type: Guid (0)], @p6 = 6e19ab2d-e223-4720-bff3-a0bc00db60ef [Type: Guid (0)]NHibernate: INSERT INTO Product (AmountInStock, Code, Description, Name, Price, PrimaryKey) VALUES (@p0, @p1, @p2, @p3, @p4, @p5);@p0 = 10 [Type: Int32 (0)], @p1 = ‘ABC123’ [Type: String (4000)], @p2 = ‘Sample product 01’ [Type: String (4000)], @p3 = ‘ABC 123’ [Type: String (4000)], @p4 = 100,5 [Type: Decimal (0)], @p5 = 252d4239-da61-42b3-886c-a0bc00db60f8 [Type: Guid (0)]NHibernate: INSERT INTO Product (AmountInStock, Code, Description, Name, Price, PrimaryKey) VALUES (@p0, @p1, @p2, @p3, @p4, @p5);@p0 = 25 [Type: Int32 (0)], @p1 = ‘CDE456’ [Type: String (4000)], @p2 = ‘Sample product 02’ [Type: String (4000)], @p3 = ‘CDE 456’ [Type: String (4000)], @p4 = 45 [Type: Decimal (0)], @p5 = ef75aae9-f644-4876-9441-a0bc00db60f8 [Type: Guid (0)]
    • In Visual Studio open your database via Server Explorer and notice data has been added to the tables
  • Run the WPF project and access and read the data

SharePoint

  • MSDN – samples
  • Microsoft SharePoint 2010 Software Development Kit (SDK) – Samples

The End

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: