Search

just show me the code

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



Saturday, January 31, 2009

xml membership

credit to Atrem XmlProviders download their code from codeplex

   54     <membership defaultProvider="XmlMembershipProvider">
   55       <providers>
   56         <clear/>
   57         <add applicationName="SampleWebsite" 
   58              name="XmlMembershipProvider" 
   59              type="Artem.Web.Security.XmlMembershipProvider" 
   60              minRequiredPasswordLength="1"
minRequiredNonAlphanumericCharacters="0"
   61              requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
   62              passwordFormat="Hashed"/>
   63       </providers>
   64     </membership>
   77     <roleManager enabled="true" cacheRolesInCookie="true" 
defaultProvider
="XmlRoleProvider">
   78       <providers>
   79         <clear/>
   80         <add applicationName="SampleWebsite" name="XmlRoleProvider" 
type
="Artem.Web.Security.XmlRoleProvider"/>
   81       </providers>
   82     </roleManager> 
   83     <profile enabled="true" defaultProvider="XmlProfileProvider">
   84       <providers>
   85         <clear/>
   86         <add applicationName="SampleWebsite" name="XmlProfileProvider"
type="Artem.Web.Security.XmlProfileProvider"/>
   87       </providers>
   88       <properties>
   89         <group name="Person">
   90           <add name="FirstName" type="String"/>
   91           <add name="LastName" type="String"/>
   92           <add name="Years" type="Int32"/>
   93         </group>
   94         <add name="PageCursor" type="String"/>
   95         <add name="Counter" type="Int32" defaultValue="0"/>
   96       </properties>
   97     </profile>

Friday, January 30, 2009

Better menu control

sitemap.xml
    1 <?xml version="1.0" encoding="utf-8" ?>
    2 <siteMap> 
    3   <siteMapNode title="Welcome" url="~/Login/Login.aspx" />
    4   <siteMapNode title="Talent" url="~/Admin/TalentPage2.aspx" />
    5   <siteMapNode title="Profile" url="~/User/Profile.aspx" />
    6   <siteMapNode title="Events" url="~/Admin/Events2.aspx" />
    7   <siteMapNode title="Contact" url="~/Login/Contact.aspx" />
    8   <siteMapNode title="Download" url="~/User/Downloads.aspx" />
    9   <siteMapNode title="Responses" url="~/SuperAdmin/ResponseView.aspx" /> 
   10 </siteMap>

Main.Master
  110 <div id="navigation" class="right rclear">
  111     <div id="menu-container" class="right">
  112         <ul id="menu">
  113             <asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
  114                 <ItemTemplate>
  115                     <li runat="server" id="item" class="item" 
  116                         visible='<%# this.CanNavigate(Eval("url"))  %>'>
  117                         <asp:HyperLink ID="hl" runat="server" 
  118                             NavigateUrl='<%# Eval("url") %>' 
  119                             Text='<%# Eval("title") %>' />
  120                     </li>
  121                 </ItemTemplate>
  122             </asp:Repeater>
  123         </ul>
  124     </div>
  125 </div>
...
  138 <asp:XmlDataSource ID="XmlDataSource1" runat="server" 
  139     DataFile="~/Master/siteMap.xml" ></asp:XmlDataSource>



Main.Master.cs
   21 protected bool CanNavigate(object o)
   22 { 
   23     string url = o.ToString();
   24     IPrincipal p = HttpContext.Current.User;
   25     bool bReturn = UrlAuthorizationModule.CheckUrlAccessForPrincipal(url, p, "get");
   26     if (Talent.IsAdminOrBetter(p) && url.EndsWith("Profile.aspx"))
   27     {
   28         bReturn = false;
   29     }
   30     if (p.Identity.IsAuthenticated && url.EndsWith("Login.aspx"))
   31     {
   32         bReturn = false;
   33     }
   34     return bReturn; 
   35 }


credit to this stackoverflow post for the jquery script that will set the 'active' menu item class

Main.Master
   59 <script type="text/javascript"> 
   60     $(document).ready(function() {
   61         markActiveLink() 
   62     }); 
   63     function markActiveLink() { 
   64         //Look through all the links in the menu
   65        $("li.item a").filter(function() { 
   66           //Take the current URL and split it into chunks at each slash
   67           var currentURL = window.location.toString().split("/"); 
   68           //return true if the bit after the last slash is the current page name
   69           return $(this).attr("href") == currentURL[currentURL.length-1]; 
   70         //when the filter function is done, you're left with the links that match.
   71         }).addClass("active"); 
   72         //Afterwards, look back through the links. If none of them were marked,
   73         //mark your default one.
   74         if ($("li.item a").hasClass("active") == false) {
   75             $("li.item:nth-child(1) a").addClass("active");
   76         }
   77     }
   78 </script>

looks like this in then end when logged in as an admin



Thursday, January 29, 2009

Fail a Build when your Test Fails

Fail a Build when your Test Fails
ref: http://blogs.msdn.com/aaronhallberg/archive/2007/11/05/how-to-fail-a-build-when-tests-fail.aspx

Why
When a team build compiles with no errors, it is a success. If the test fail it gets marked as partially successful. Most would rather a failed test to mark the build as Failed.


Step1: Change your Target
The following code should be pasted into the bottom of your Team Build poject file and edited as necessary.

  252   <Target Name="AfterTest"> 
  253     <!-- Refresh the build properties. -->
  254     <GetBuildProperties TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
  255       BuildUri="$(BuildUri)"
  256       Condition=" '$(IsDesktopBuild)' != 'true' ">
  257       <Output TaskParameter="TestSuccess" PropertyName="TestSuccess" />
  258     </GetBuildProperties>
  259
  260     <!-- Set CompilationStatus to Failed if TestSuccess is false. -->
  261     <SetBuildProperties TeamFoundationServerUrl="$(TeamFoundationServerUrl)"
  262       BuildUri="$(BuildUri)"
  263       CompilationStatus="Failed"
  264       Condition=" '$(IsDesktopBuild)' != 'true' and '$(TestSuccess)' != 'true' "  >
  265     </SetBuildProperties>
  266   </Target>

UPDATE
------------------------
http://drycode.blogspot.com/2009/04/tfs-fail-build-when-unit-test-fails.html


Tuesday, January 27, 2009

add paramaters to object data source dynamicaly


   45 <asp:ObjectDataSource ID="odsXxxxx" runat="server"
   46     SelectMethod="GetXxxxx" 
   47     TypeName="Xxxxx.Xxxxx.Web.ObjectDataSource.XxxxxOds" 
   48     InsertMethod="InsertXxxxx0" onselecting="odsXxxxx_Selecting" 
   49     oninserting="odsXxxxx_Inserting" oninserted="odsXxxxx_Inserted"  >
   50     <InsertParameters>
   51         <asp:Parameter Name="UserName" Type="String" />
   52         <asp:Parameter Name="UserPassword" Type="String" />
   53         <asp:Parameter Name="Email" Type="String" />
   54         <asp:Parameter Name="UserId" Type="String" /> 
   55         <asp:Parameter Direction="Output" Name="XxxxxId" Type="Int32" />
   56     </InsertParameters>
   57     <SelectParameters>
   58         <asp:Parameter Name="XxxxxId" Type="Int32" />
   59     </SelectParameters>
   60 </asp:ObjectDataSource>


Code Behind:

   47         protected void odsXxxxx_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
   48         {
   49             this.XxxxxId = Convert.ToInt32(e.OutputParameters["XxxxxId"]);
   50             Response.Redirect("~/User/ProfileAdd1.aspx?XxxxxId=" + this.XxxxxId.ToString() );
   51         }

   38         protected void odsXxxxx_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
   39         {
   40             MembershipUser u = Membership.GetUser(this.CreateUserWizard1.UserName); 
   41             e.InputParameters["UserId"] = u.ProviderUserKey.ToString();  
   42             e.InputParameters["UserName"] = u.UserName;
   43             e.InputParameters["UserPassword"] = "";
   44             e.InputParameters["Email"] = u.Email;   
   45         }

Ods.cs file:

   55         public void InsertXxxxx0(string UserName, string UserPassword, string Email, string UserId, out int XxxxxId)
   56         {
   57             //UserName, UserPassword, Email
   58             Xxxxx t = new Xxxxx();
   59             t.UserName = UserName;
   60             t.Email = Email;
   61             t.UserId = new Guid(UserId);
   62             _service.SaveTalent(t);
   63             XxxxxId =  t.Id;
   64         }

Tuesday, January 20, 2009

Auto deploy a TFS build


  
  228   <Target Name="AfterDropBuild">
  229     <CreateProperty Value="\\webdev1\Domains\Xxx_Xxxx_XxxxxXxxxx">
  230       <Output
  231         TaskParameter="Value"
  232         PropertyName="WebPreviewDropLocation"/>
  233     </CreateProperty>
  234     <CreateItem
  235         Include="$(DropLocation)\$(BuildNumber)\Release\_PublishedWebsites\Xxxx.XxxxXxxxx.Web\**\*">
  236       <Output
  237          TaskParameter ="Include"
  238          ItemName ="MyDropFiles"/>
  239     </CreateItem>
  240
  241     <RemoveDir Directories="$(WebPreviewDropLocation)"
  242                Condition="'$(BuildBreak)'!='true'" />
  243     <MakeDir Directories="$(WebPreviewDropLocation)"
  244              Condition="'$(BuildBreak)'!='true'" /> 
  245    <Copy
  246       SourceFiles="@(MyDropFiles)"
  247       DestinationFiles="@(MyDropFiles->'$(WebPreviewDropLocation)\%(RecursiveDir)%(Filename)%(Extension)')" 
  248       Condition="'$(BuildBreak)'!='true'" />
  249   </Target>

Contributors