Search

just show me the code

Saturday, February 28, 2009

SOLID - Single Responsibility Principle

here is a good video on how to implement the Single Responsibility Principle for a Report.

# 88 - Creating SOLID Code: Single Responsibility Principle (SRP)

Thanks Dimecast

--- Updates ----
# 90 - Creating SOLID Code: Open/Closed Principle (OCP)

Friday, February 27, 2009

jQuery wrap

the <ul> tag padding act different in ie and firefox. So here is an easy way (using jQuery) to change your classes on the ul to a class on the div.
thanks to this video by Ben Nadel. Here are the slides.
here are more helpful links. 1 2 3 Thanks again Ben Nadel.

here is the style sheet

   21 .ul-indent { padding: 0px 0px 0px 7px; }

here is your tag

   74     <ul class="ul-indent">
   75         <li>First</li>
   77         <li>Second</li>
   79     </ul> 

here is your jquery

   15     <script type="text/javascript">
   16         $(document).ready(function() {
   18             $(".ul-indent").wrap("<div class='ul-indent'></div>").removeClass("ul-indent");
   19         }); 
   36    </script>

Thursday, February 26, 2009

User control events

using usercontrols can be a good way to break up a huge page.
you just need to make sure you are listening to the events. here's how.

check out this post

Wednesday, February 25, 2009

Config Manager

abstract away from your code the fact that the configuration is coming from the web.config with ConfigManager class



    9     public static class ConfigManager
   10     {
   11         public static string GetSetting(string s)
   12         {
   13             return ConfigurationManager.AppSettings[s];
   14         }
   15     }

then

   58    ConfigManager.GetSetting("formcomplete")

will read from the webconfig here

   16   <appSettings>   
   25     <add key="formcomplete" value="formcomplete" /> 
   28   </appSettings>

Sunday, February 22, 2009

jquery in Asp.net

there are 2 dropdowns for the leagues and you can only select one team so when something other than select is selected on one dropdown we disable the other drop down

  225
  226         <asp:Label ID="lblAmericanLeague" runat="server" Text="American League"></asp:Label> 
  227         <asp:DropDownList ID="AmericanLeague" runat="server"> 
  228             <asp:ListItem Text="-Select-" Value="-1" ></asp:ListItem>
  229             <asp:ListItem Text="Chicago White Sox" Value="ChicagoWhiteSox" ></asp:ListItem>
  230             <asp:ListItem Text="Baltimore Orioles" Value="BaltimoreOrioles" ></asp:ListItem>
  231             <asp:ListItem Text="Boston Red Sox" Value="BostonRedSox" ></asp:ListItem>
  232             <asp:ListItem Text="New York Yankees" Value="NewYorkYankees" ></asp:ListItem>
  243         </asp:DropDownList><br />
  244
  245         <asp:Label ID="lblNationalLeague" runat="server" Text="National League"></asp:Label> 
  246         <asp:DropDownList ID="NationalLeague" runat="server"> 
  247             <asp:ListItem Text="-Select-" Value="-1" ></asp:ListItem>
  248             <asp:ListItem Text="ChicagoCubs" Value="ChicagoCubs" ></asp:ListItem>
  249             <asp:ListItem Text="AtlantaBraves" Value="AtlantaBraves" ></asp:ListItem>
  250             <asp:ListItem Text="FloridaMarlins" Value="FloridaMarlins" ></asp:ListItem>
  264         </asp:DropDownList><br />



and the jquery looks like this ...



  302
  303 <script type="text/javascript" >
  304     $(document).ready(function() { 
  305         $('#<%=NationalLeague.ClientID %>').click(function() {
  306             var SelectedVal = $('#<%=NationalLeague.ClientID %>').val();
  307             if (SelectedVal == "-1") {
  308                 $('#<%=AmericanLeague.ClientID %>').removeAttr("disabled");
  309             }
  310             else {
  311                 $('#<%=AmericanLeague.ClientID %>').val("-1");
  312                 $('#<%=AmericanLeague.ClientID %>').attr("disabled", true);
  313             }
  314         }); 
  315         $('#<%=AmericanLeague.ClientID %>').click(function() {
  316             var SelectedVal = $('#<%=AmericanLeague.ClientID %>').val();
  317             if (SelectedVal == "-1") {
  318                 $('#<%=NationalLeague.ClientID %>').removeAttr("disabled");
  319             }
  320             else {
  321                 $('#<%=NationalLeague.ClientID %>').val("-1");
  322                 $('#<%=NationalLeague.ClientID %>').attr("disabled", true);
  323             }
  324         });
  325     });
  326
  327 </script>

Friday, February 20, 2009

Los Techies Motivational posters on the SOLID principals

Motivational posters by Los Techies about Uncle Bob Martain's SOLID principals.

if you have not already heard ...
SOLID Principles with Uncle Bob - Robert C. Martin
Uncle Bob Martin: SOLID, this time with feeling.
stack overflow Podcast #41



SOLID

Software development is not a Jenga game.

SOLID


Single Responsibility Principle

Just because you can, doesn’t mean you should.

Single Responsibility Principle 2


Open Closed Principle

Open chest surgery is not needed when putting on a coat.

Open Closed Principle 2

Liskov Substitution Principle

If it looks like a duck, quacks like a duck, but needs batteries – you probably have the wrong abstraction

Liskov Subtitution Principle

Interface Segregation Principle

You want me to plug this in, where?

Interface Segregation Principle

Dependency Inversion Principle

Would you solder a lamp directly to the electrical wiring in a wall?

Dependency Inversion Principle


Wednesday, February 18, 2009

view for many to many


    1 /****** Object:  View [dbo].[vwTalent]    Script Date: 02/18/2009 15:11:11 ******/
    2 IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[vwTalent]'))
    3 DROP VIEW [dbo].[vwTalent]
    4 GO
    5 /****** Object:  View [dbo].[vwTalent]    Script Date: 02/18/2009 15:11:15 ******/
    6 SET ANSI_NULLS ON
    7 GO
    8 SET QUOTED_IDENTIFIER ON
    9 GO
   10   
   11  
   12
   13 CREATE VIEW [dbo].[vwTalent]
   14 AS
   15 SELECT t.TalentID
   16 ,  t.FirstName 
   17 , (SELECT   l.LanguageName +  ', ' AS [text()]
   18         FROM [TalentLanguage] tl
   19         JOIN [Language] l ON tl.LanguageID = l.LanguageID 
   20         WHERE tl.TalentID = t.TalentID 
   21         ORDER by l.LanguageID
   22         FOR XML PATH('') 
   23    ) as LanguagesString
   24 , (SELECT   Convert(nvarchar(10), l.LanguageID) +  ', ' AS [text()]
   25         FROM [TalentLanguage] tl
   26         JOIN [Language] l ON tl.LanguageID = l.LanguageID 
   27         WHERE tl.TalentID = t.TalentID 
   28         FOR XML PATH('') 
   29    ) as LanguageIdsString  
   30 FROM Talent as t  
   31 WHERE t.Active = '1'
   32
   33 GO

select * from dbo.vwTalent


ID FirstName LanguagesString LanguageIdsString
----- ----------- -------------------- -----------
1 Jake English, 1,
6 test Chinese, Japanese, Romanian, 8, 9, 10,
7 Daren English, 1,
32 Jim NULL NULL
33 Andy English, Spanish, French, German, Polish, Portuguese, 1, 2, 3, 4, 5, 6,
34 Jeff English, 1,


now you can call

  204 private IQueryable<Talent> AddSearch(IQueryable<Talent> t, string s)
  205 {
  206     return t.Where(tal =>  tal.LanguagesString.ToLower().Contains(s)  );
  207 }

Monday, February 16, 2009

asp.net Images

http://www.xdevsoftware.com/blog/post/Resize-Image-in-ASPNET-C.aspx



   15   <asp:ImageButton ID="ImageButton1" 
   16           runat="server"  ToolTip="Delete"  Visible='<%# EditMode%>'   
   17           OnClick="bRemove_Click"  CommandArgument='<%#Eval("TalentFileID") %>' 
   18           ImageUrl="../Content/Images/Remove.png" />&nbsp;<asp:Panel ID="pnlImage" 
   19           runat="server" ><img  
   20           src="../UserControls/ViewImage.aspx?imgId=<%#Eval("TalentFileID") %>&isThumb=1" 
   21           border="1">
   22   </asp:Panel>                
   23   <asp:Panel ID="Panel_Popup_FullPhoto" runat="server">
   24         <div style="margin-top: 0px; margin-right: 5px;"> 
   25             <img  border="1"
   26               src="../UserControls/ViewImage.aspx?imgId=<%#Eval("TalentFileID") %>&isThumb=0" >
   27         </div>
   28   </asp:Panel>
   29   <cc1:HoverMenuExtender ID="HoverMenuExtender1" runat="server"
   30       PopupControlID="Panel_Popup_FullPhoto" PopupPosition="Left" 
   31       TargetControlID="pnlImage"
   32       PopDelay="25" />




viewimages.aspx.cs
2
3



Saturday, February 14, 2009

ListView Get Item Index


  

   24   protected int EventEditId
   25   {
   26      get { return Convert.ToInt32(this.Session["EventEditId"]); }
   27      set { this.Session["EventEditId"] = value; }
   28   }


   59  protected void bEdit_Click(object sender, EventArgs e)
   60  {
   61      string Id = ((ImageButton)sender).CommandArgument;
   62      this.EventEditId = Convert.ToInt32(Id);
   63      this.lvEvents.DataBind();
   64  }


   30  protected string EventEditNewName
   31  {
   32      get { return this.Session["EventEditNewName"].ToString(); }
   33      set { this.Session["EventEditNewName"] = value; }
   34  }


   66  protected void bSave_Click(object sender, EventArgs e)
   67  {
   68      ListViewDataItem lvi = ((ImageButton)sender).NamingContainer as ListViewDataItem;
   69      EventEditNewName = ((TextBox)lvi.FindControl("tbEventName")).Text;
   70      this.odsEvents.Update();
   71      this.EventEditId = -1;
   72  }




   79    protected void odsEvents_Updating(object sender, ObjectDataSourceMethodEventArgs e)
   80    { 
   81        e.InputParameters["EventName"] = EventEditNewName;
   82        e.InputParameters["EventId"] = this.EventEditId;
   83    }


   37         <asp:ListView ID="lvEvents" runat="server" DataSourceID="odsEvents" DataKeyNames="EventID"  
   38             ondatabound="lvEvents_DataBound" >
   42             <LayoutTemplate>
   43                 <table cellpadding="0" cellspacing="0" class="main">
   44                     <tr>
   45                         <th width="95%">
   46                             <asp:LinkButton ID="lbSortEventName" Text="Event Name" 
   47                             CommandName="Sort" CommandArgument="EventName runat="server" />
   48                         </th> 
   49                         <th class="center end">Edit</th>
   50                     </tr>
   51                     <asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
   52                 </table>
   53             </LayoutTemplate>
   54             <ItemTemplate>
   55                 <tr class='<%# Container.DataItemIndex % 2 == 0?"even":"odd" %> fonts' >
   56                     <td>
   57                         <asp:Label ID="Label1" runat="server" 
   58                             Visible='<%# (this.EventEditId != Convert.ToInt32(Eval("EventID"))) %>' 
   59                             Text='<%# Eval("EventName")%>' ></asp:Label>
   60                         <asp:TextBox ID="tbEventName" runat="server" 
   61                             Visible='<%# (this.EventEditId == Convert.ToInt32(Eval("EventID"))) %>' 
   62                             Text='<%# Eval("EventName")%>'   ></asp:TextBox>
   64                     </td>   
   65                     <td class="center">
   66
   67                         <asp:ImageButton ImageUrl="../Content/Images/icon-edit.png" 
   68                             ID="ibEditEvent" AlternateText="Edit Event" runat="server"
   69                             OnClick="bEdit_Click"  CommandArgument='<%#Eval("EventID") %>' 
   70                             Visible="<%# (this.EventEditId == -1) %>"
   71                             CommandName="Select" />
   72                         <asp:ImageButton ImageUrl="../Content/Images/icon-approved.png" 
   73                             ID="ibSaveEvent" AlternateText="Save Event" runat="server"
   74                             OnClick="bSave_Click"   CommandArgument='<%#Eval("EventID") %>' 
   75                             Visible='<%# (this.EventEditId == Convert.ToInt32(Eval("EventID"))) %>' 
   76                             CommandName="Update" />
   77                         <asp:ImageButton ImageUrl="../Content/Images/icon-error-small.png" 
   78                             ID="ibCancelEditEvent" AlternateText="Cancel Edit" runat="server"
   79                             OnClick="bCancel_Click"   CommandArgument='<%#Eval("EventID") %>' 
   80                             Visible='<%# (this.EventEditId == Convert.ToInt32(Eval("EventID"))) %>' 
   81                               />
   83                     </td>
   84                 </tr>
   85             </ItemTemplate> 
   86         </asp:ListView> 

Wednesday, February 11, 2009

Outer join Linq to Sql with a many-to-many


   79 public IQueryable<Model.Model.Talent> GetTalents()
   80 {
   81     var tal = from t in _db.Talents 
   82               join tre in _db.Responses on t.EyeColorID equals tre.ResponseID
   83               into tempEyes
   84               from rEyes in tempEyes.DefaultIfEmpty()  
   85               let tLanguage = GetTalentLanguages(t.TalentID)
   86               where t.Active == true
   87               select new Model.Model.Talent
   88               {
   89                   Id = t.TalentID,
   90                   FirstName = t.FirstName,
   91                   LastName = t.LastName,
   92                   EyeColorID = t.EyeColorID ?? -1,
   93                   EyeColor = rEyes.ResponseName, 
   94                   TalentLanguages = new LazyList<Model.Model.TalentLanguage>(tLanguage),
   95                   //LanguagesString = t.TalentLanguages.ToLanguageNameString(_LanguageRepository.GetLanguages()),
   96                   LanguagesString = String.Join(", "
   97                       ,(from tl in _db.TalentLanguages
   98                         join l in _db.Languages on tl.LanguageID equals l.LanguageID
   99                         where tl.TalentID == t.TalentID
  100                         select l.LanguageName.ToString()).ToArray())
  101               };
  102     return tal ;
  103 }










From D.R.Y. code

Saturday, February 7, 2009

visual studio settings

How to avoid the "I didn't even know I had that checked out"


tools -> options ->




Thursday, February 5, 2009

Intersect in Linq using Comparer

with help from Lost In LoC
  

  172         private static readonly char[] SplitDelimiters = " ".ToCharArray();
...
  215         private IQueryable<Talent> BasicSearch(string searchExpression)
  216         {
  217             IQueryable<Talent> t;
  218             string[] sa = searchExpression.Trim().Trim()
.ToLower()
.Split(SplitDelimiters,
StringSplitOptions.RemoveEmptyEntries);
  219             t = _repository.GetTalents();
  220             foreach (string s in sa)
  221             {
  222                 t = t.Intersect(AddBasicSearch(s), new TalentComparer()); 
  223                 //http://lostinloc.com/2008/02/06/the-principle-of-least-astonishment/
  224             }
  225             return t;
  226         }




   13     public class TalentComparer : IEqualityComparer<Talent> // defines Equals and GetHashCode
   14     {
   15         public bool Equals(Model.Model.Talent x, Model.Model.Talent y)
   16         {
   17             return x.Id == y.Id;
   18         }
   19 
   20         // implements the IEqualityComparer.GetHashCode(T obj) : int
   21         public int GetHashCode(Model.Model.Talent obj)
   22         {
   23             return obj.Id.GetHashCode();
   24         }
   25     }

Monday, February 2, 2009

read from the web.config

web.config

   16   <appSettings>
   28     <add key="EndDate" value="2009-02-03 03:00 AM"/> 
   29   </appSettings>


Code behind

    3 using System.Configuration;
    ...
   54         string sEnd = ConfigurationManager.AppSettings["EndDate"].ToString();
   55         DateTime dtEnd = DateTime.ParseExact(sEnd, "yyyy-MM-dd HH:mm tt", null);
 

Sunday, February 1, 2009

Database Publishing Wizard

Generate insert scripts.
creates a single SQL script file which can be used to recreate a database
http://www.codeplex.com/sqlhost/Wiki/View.aspx?title=Database%20Publishing%20Wizard



Contributors