Category Archives: Uncategorized

NHibernate ‘IN’ clause with linq

NHibernate was giving me fits, not letting me use ‘.In’ in the linq statements.

Here is how I got it to go:

 IQueryable<int?> idsForOrgList = _repo.Get<OrganizationThingyXref>().Where(thingy => thingy.OrganizationID == orgId && thingy.IdICareAbout != null).Select(_=>_.IdICareAbout);
           
List<People> ThingysInOrg = _repo.Get<Thingy>().Where(_ => idsForOrgList.Contains(_.IdICareAbout)).ToList();

Selenium test .net webforms with data-attributes

I was struggling to E2E Selenium test a repeater, in a user control, in an old .net webforms page – tall ask right?

Here is the solution I came up with:

I added a custom attribute (“data-serviceid”) to the element the repeater is drawing – inside the code behind of the user control (a link button in this case):

var lbNewReport = (LinkButton)args.Item.FindControl("lbNewReport");
lbNewReport.Attributes.Add("data-serviceid", myServiceId);

Then I used this static helper method for selenium:

public static By SelectorByAttributeValue(string attributeName, string attributeValue)
{
return (By.XPath($"//*[@{attributeName} = '{attributeValue}']"));
}

With This call:

var linkIWant = _driver.FindElement(SelectorByAttributeValue("data-serviceid", myServiceId));

And voila – I have the control in hand that I want to interact with(clicking in this case for me).

Idempotent (run twice safe) sql statements

if (select count(*) from neatoSchema.YourTable where SomeColumnYouCanCheckDataOn = 2018)> 0
begin
print 'That row is already inserted'
end
else
begin
INSERT into neatoSchema.[ConferenceDate]
(... your data ...)
end

You can also interrogate information_schema.tables or information_schema.columns if you are doing a change to a table to ensure that doesn’t run twice!

Refactoring – or Code Remodeling

I think that most days, code remodeler would be the right job title for a lot of us. We take working code and just make it better working code.

The book that got me started thinking of myself as a ‘code remodeler’ is Refactoring: Improving the Design of Existing Code by Martin Fowler.

If you haven’t read it, go get it quick! (Of course, and then read it…) Fowler lays out refactoring techniques in a straightforward way. You can start applying it immediately. I use Replace magic number with symbolic constant and Extract Method almost every day when I am helping a team with an older codebase.

Thanks for stopping by, and happy coding,

-Jim

Javascript get sending element in all browsers

   function mySweetClickEvent(event) {

        event = window.event || event; //NOTE: another lovely hack to make Firefox work…

        var target = event.target ? event.target : event.srcElement; //NOTE: another lovely hack to make IE work

        var senderToUse;

        if (target) {

            senderToUse = target; //this works for ie and chrome

        } else {

            senderToUse = event; //this works for firefox

        }

…do stuff with the sender here…

}

CSS to replace cellpadding and cellspacing

I was having trouble properly formatting a table in html with css and I found this on Stack Overflow.

Replace cellpadding with:

th, td { padding: 5px; }

Replace cellspacing with:

table { border-collapse: separate; border-spacing: 5px; } // cellspacing=”5″
table { border-collapse: collapse; border-spacing: 0; } // cellspacing=”0″

Replace valign with:
th, td { vertical-align: top; }

Replace align (center) with:
table { margin: 0 auto; }

The original article is here.