Interesting behavior and a difference between List.Contains( and List.Exists(

by Jesse 19. August 2008 05:30

A nice little suprise this morning!  A quick hit on it - .Contains() returns a bool value and expects you to pass in an object that is in your list whereas .Exists expects a predicate (but still returns a boolean).  Let's dive right into this because its easier to show than blab/explain.  Make a new list, shove it into view state.  Feel free to add this where ever you like, just as long as it is not in pageload, make it a button event.

List<string> Ids = new List<string>();
Ids.Add(
"007");
Ids.Add(
"008");
Ids.Add(
"009");
ViewState.Add(
"Agents", Ids);

Drop a second button out there and add the following code...

List<string> SavedAgents = ViewState["Agents"] as List<string>;
string FoundAgent = "008";

bool ContainsAgent = SavedAgents.Contains(FoundAgent);

bool IsSavedAgent = SavedAgents.Exists(delegate(string agent)
{
    
return agent == FoundAgent;
});
 

This works, both values are true.  "So what's the difference?" -- Create yourself the following object...

[Serializable()]
public class Agent
{
    
public string AgentId { get; set; }
    
public string AgentName { get; set; }
    
public string CurrentLocation { get; set; }
    
public Agent(string agentId, string agentName, string currentLocation)
     {
         
this.AgentId = agentId;
         
this.AgentName = agentName;
         
this.CurrentLocation = currentLocation;
     }
}

and switch out the code just a touch that loads up the viewstate...

List<Agent> agentList = new List<Agent>();
agentList.Add(
new Agent("007", "James Bond", "Las Vegas"));
agentList.Add(
new Agent("008", "Unknown", "Unknown"));
agentList.Add(
new Agent("009", "David Brabham", "UK"));
ViewState.Add(
"Agents", agentList);

and the check behind button 2 like so...

List<Agent> SavedAgents = ViewState["Agents"] as List<Agent>;
Agent foundAgent = new Agent("008", "Unknown", "Unknown"
);
bool
 ContainsAgent = SavedAgents.Contains(foundAgent);
bool IsSavedAgent = SavedAgents.Exists(delegate(Agent
agent)
{
     return agent.AgentId == foundAgent.AgentId;

});

This might surprise you, but ContainsAgent will be false.  If you do "return agent == foundAgent" for the .Exists, it will also be false.  I'm -guessing- it's using reflection.  Because of this, I insist using .Exists instead, since you can test properties directly.  Even more curiously, using the various .Equals yeilds false, such as :

bool IsEqual = Equal(SavedAgents[1], foundAgent); ...or

bool IsEqual = SavedAgents[1].Equals(foundAgent); ...or even

bool IsEqual = foundAgent.Equals(SavedAgents[1]);

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

.Net | C# | Coding

Sitefinity modules, the easier way!

by Jesse 16. June 2008 09:07

From my previous posts, I talked about creating a custom module that complies into a dll.  I've discovered that is THE hardest way possible to create a custom control.  The experience was good, but not exactly fun.

Luck would have it, they have a much stupid simpler way.  It's not as shareable, you have to add the files to each and evey site instead of just changing up the web.config.  So what?  There's a download they provide in their documentation that's kind of burried, but no sweat, you can get it here.  It's a full project and it takes a minute to unzip.

Before we get started, let me explain what this custom module does and what we're going to do to it.  The JobsModule consists of 4 user controls, your tried and true ascx files, that do various things, a couple classes, nothing intimidating.  One neat thing they did in this example was use an interface, "IJobsControlPanel", (app_code/jobsmodule.cs:142) to switch between two modes, "Category" and "Type", but in my examples I removed this for simplicity and the real module I was creating does not call for it.  Other files you need to note are all within the /Jobs folder.  Control panel and Toolbox are for the admin side, where the "JobList" and "JobListSummary" are for the user side. 

If you haven't went though the pain and agony of unzipping that file, do so now and let's take a look at the "JobsModule.cs" under the app code directory.  Line 28 begins the Nolics database init which later on, we won't need, but note where it is.  Under the properties region, lies the name/title/descript that will show up on the admin side.  On line 93/94, these are the files that will show up for the user side which will get more into later, just note that is where they are.  Next, take a look at line 112 and 121 as these are you admin controls.  Very easy to do.  Finally, there's the enum and interface for the other items they use for this module.  This is 95% of what it takes to make a module.

For this example, I am going to completely ignore the code behind for the user controls on the public facing side as they are unimportant.  Why?  Because user controls are user controls are user controls.  Make one, make 'em all, which is a very good thing!  Now, let's make a custom module, shall we?  Let's say we have a customer that wants a very very basic control that displays how many orders recieved today with the option to look at yesterday.  Our database table will look something like this...

TableName : Orders
Id  uniqueidentifier, not null, primary key
OrderDate datetime, not null
Quantity int, not null
DollarAmount money, not null
ItemOrdered nvarchar(1000), not null

Very simple, nothing fancy.  For the next step, I went away from the example just a bit and created a "CustomModules" folder under the App_Code directory along with a "DAL" folder with two folders within named "Generated" and "Extended".  See the image below for a bit of clarity. Now move "JobsModule.cs" into the CustomModule folder. Create yourself a folder in the root named "CustomControls" and a folder within it called "Orders" -- this is where the user controls will live.

At this point, if you've never touched subsonic, I would highly suggest jumping over there to brain up on how it works as I will not be covering that aspect.  Also, if you are unfamiliar with subsonic generating only certain tables, add the tag includeTableList="Orders" to your subsonic service.  For reference, this is a comma seperated list and will restrict subsonic to generating only those tables defined there.  Very handy since sitefinity has around 100 tables out of the box.  Generate this table and dump the files into App_Code/DAL/Generated.

Next, make a new class in App_Code/CustomModules and name it "OrdersModule.cs".  On the class declaration, inherit the WebModule from the Telerik namespace, implement the abstract class and change out the constructor from public to static as seen below.

Hop over to the JobsModule and copy the Methods region (override CreateControlPanel and Override CreateToolBoxControls), paste those into your OrdersModule -- don't worry about the string values yet, we'll get to those.  Also copy over the get in JobsModule:83-99 and paste that into the "Controls" override.  Finally, create a private variable of IList<Telerik.Web.IToolboxItems> (look to JobsModule:40 for example).

Now that's prepped, hop over to the CustomControl folder and create 3 user controls - "ControlPanel.ascx", "ToolboxPanel.ascx" and "OrderList.ascx".

ControlPanel needs to inherit the Telerik.IControlPanel and go ahead and give some string values, similar to this...

#region IControlPanel Members private readonly string status = "Orders";
private readonly string title = "Orders";

string IControlPanel.Status
{
     get { return status; }
}

string IControlPanel.Title
{
    
get { return title; }
}

#endregion

In the ToolboxPanel, inherit the Telerik.IControlPanelCommand and give values where necessary (Title for one).

Finally, for OrderList, we don't have to do anything yet.  If you feel like it, drop some text on the page just for rendering reasons.  Guess what? Most of the "behind the scenes" work is already done.  No really!

At this point, it should build and give you a custom module within your sitefinify project.  For the next post, I'll talk about how to wire up the subsonic stuff and even get at some data within sitefinity!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

.Net | C# | Coding | Design

Encryption, Development and AES

by Jesse 16. May 2008 06:55

If the custom module wasn't enough, I'm now wondering off into encryption land.  A quick scouting of the System.Security.Cryptography namespace shows me a ton of stuff to play with.

Ooo, AES.  I like AES.  It runs on my router(s) @ home and is viciously annoying to crack (TKIP f0r t3h w1n!!!11).  Cool, let's use that, its good enough for top secret docs for the gov so it should be good enough for me.  But, as with anything else, there's a catch or ...20.  Here's some basic considerations.

Will this data be searched? 

Searching encrypted data is a royal PITA and a huge overhead.  Example : saving data to a db with encryption happening in the business layer.  A perfectly viable user says to the application "hey, find this" -- you cannot directly ask the database to find it, it is impossible, so every search that happens comes across, ALL OF IT (say 2 million records), decrypts, the search happens, find the records necessary and passes that on.  Not very reasonable nor scalable.  2nd option for this is do it on the sql server itself.  Fundamentally I have a problem with this for 2 reasons.  1, a purely architecture standpoint, this should never be passed off to the data source.  In the real world, it's probably ok to offload some of that overhead, but still, using the OSI model alone says "no no" -- encryption happens in the presentation level and offloading it means you pass though all 7 layers ONCE before you encrypt -- bad bad bad.  2nd, unless the data connection between app/server is encrypted to hell and back itself, your encryption is trumped and effectively worthless.

How much protection is necessary?

The question of the ages.  Understanding the CISSP-ism of protection and risk management: the amount of protection spent on it should be equal to the amount of total loss of one breach by the inverse of the possibility of recurrence.  So say the data is worth 10 million dollars for ONE loss.  The probability of loss is once every 5 years.  10m/5y = 2 million a year should be spent to protect it.  No really.  Now, if there's no REAL value to the data, ie, its personal junk you keep at home for giggles, then whatever the server can handle works fine.  Otherwise, use reasonable + 1.

I'll stop there.  Other questions can range from "Who needs access to it?" to "Where will the server be physically housed" -- but thats somewhat outside of the scope of this post.  Not saying they're unimportant, just "too much" for this post.  I think my first task will be working on getting something simple to encrypt, like a file or a string and work up from there to see how much overhead this thing creates.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

.Net | C# | Coding | Security | Architecture | Law

Codeplex and SilverSurvey

by Jesse 24. April 2008 16:31
Based on some feedback I got -- I've decided "why not?" and tossed the survey engine I started up to CodePlex.  I'm not going to allow anyone to join in just yet, not until I get the basic stuff setup and there's going to be a nasty learning curve that I feel coming.  So far, I've got the db project up, along with the core project itself (its kinda nice having TFS again!) -- as soon as that's done and rollin', feel free to join in.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

Visual Studio | Silverlight | C# | Coding | Tech | Linq

SilverEngine Source Code

by Jesse 21. April 2008 05:33

Ok, I've got my source code ready and hopefully the sql script will work (much easier/smaller than posting up a bak or mdf).  Download it, take a look, make fun of me on twitter.  Honestly, this was/is going to be used at some point for something real (survey engine) other than a research-ish project.  Anyway, enjoy!

SilverEngine.zip (750.98 kb)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.Net | Silverlight | C# | Coding | Design

Silverlight. Finally.

by Jesse 20. April 2008 08:07

Finally got the chance to sit down and dork around with silverlight.  I skipped right over 1 and 1.1 and went directly to 2.0 hoping that there's something there I can figure out.  What I ended up with was a Linqed, WCFed silverlight app that will NOT accept its rightful place out in the world.  I haven't quite figured it out yet, but I keep getting either a totally blank page (tried all 3 pages I set aside for it) and the service itself isn't working property (thought it was the end point domain but that wasn't it either).

Anyway, if you get bored, take a look at http://silver.rileytech.net and take a look (nothing comes up at the time of me typing this) and the service doesn't either but I won't be posting up that link.  If anyone would care to take a look, let me know, I'll send you the code with the database too.  At least the other site works -- www.columbusstarsbaseball.org :-)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.Net | Silverlight | C# | Coding

Selenium revisited (web testing in general)

by Jesse 11. March 2008 04:41

One of the magic's of the internet and more specifically, blogs, it puts you in touch with people you'd NEVER otherwise talk with.  I recieved a comment on my Selenium and the Kiss Theory from InCisif.net suggesting their Web unit testing.  Initially, I denied the response and asked for a more detailed comment as to why and explain how their product is better in x, y, z for reasons a, b and c -- Here's the response I recieved... 

Generally I avoid saying things like "I believe ours [product] is better in x, y and z for a, b and c reasons", making a good case will take a long paragraph, and it is just my opinion and I am bias. Anyway,  here is my case.

Selenium is the only automation tool under $1000 that support cross browser testing. And If you absolutely need functional cross browser testing, Selenium and Selenium RC (remote control) are good choices. Selenium comes with some weaknesses that are due to its architecture that allow cross browser testing (Speed of execution, installation of the Selenium Core on the server).
 
If you are more interested in implementing a lot functional testing, focusing

  • On the speed of development
  • The speed of execution
  • The quality of the IDE and debugger
  • Having a direct access to the DOM
  • You have to support only Internet Explorer or you will address the others browser manually

You could chose between Watir, WatiN, TestComplete or InCisif.net.

If you want to develop your automation in Ruby, then you will choose Watir.
 
If you want to develop your automation in DelphiScript, VBScript or JavaScript, then you will choose TestComplete.

If you are more .NET guy and want to develop in VB.NET, you probably want to look at our product, because we are committed to support VB.NET and C# to the same level (Selenium IDE does not support VB.NET, WatiN focus primarily on C#).
 
Here are some of the some differences between WatiN and InCisif.net.

By the way why do I known so much about WatiN, because I was been using it since January 2008 extensively on one web development. I think WatiN is a very good product. And I have been chatting with the author Jeroen van Menen via email.

InCisif.net has a more advanced record mode than the WatiN recorder. WatiN recorder is little buggy and still not mature. Here you must try for yourself (see our screen casts at http://www.incisif.net).

Web automation testing is not about dummy capture/replay, but a good code generator (AKA record mode can save time). See our screenshots.  Our record mode support: C#, VB.NET and IronPython.

InCisif.net has a tracing mode that allows reviewing the execution of tests. Making easier to find where exactly a test failed.

( full sized image here InCisif_net_Trace_File.jpg (466.08 kb) )

InCisif.net API to look up control is based on what we call the “HTML Control Naming Algorithm”, documented in our help file. 9 steps to determine what is the best information to identify uniquely an html control. Used at record and run time.

WatiN also as a pretty good API, but the record mode does not know how to use it.

When accessing an html control WatiN uses the type of the html control for example : 

ie.Link(Find.ById("LinkButton1")).Click();
ie.Button(Find.ById("btnSubmit")).Click();

Incisif.net uses the following syntax:

Page.Control(“LinkButton1”).Click();
Page.Control(“btnSubmit”).Click();

If the html control changes from a <button> to a <div> Incisif.net code will still work.

WatiN API is a little bit faster than InCisif.net API. Our 9 steps algorithm and our tracing feature have a cost.

I was not able to get WatiN to work with the NUnit 2.x GUI Application. InCisif.net supports NUnit, but also comes with its own test framework including a UI, similar to NUnit, but we added the concept of parameterized test (see here, here and here) and the concept of Test Dependency, very important for UI testing (see this link)

InCisif.net has some special features at record time and runtime to deal with the ASP.NET I'd like “ctl00_ContentPlaceHolder1_butCreateAccount”. See the blog we also have a screen cast about it.

InCisif.net can use some of the C# 3.5 LINQ features and some of our API can use Lambda expressionThat is not absolutely required to program web testing, but is kind of fancy.
 
InCisif.net supports to start and stop Cassini, the web server that ship with ASP.NET 2.0. 
Very usefull.
 
Other features from InCisif.net

  • Reporting API, XML report, HTML report about the tests and test suite execution.
  • Grab Screen shots when an assert failed or an un-expected error is raised.
  • Email notification of the HTML suite report. Attached in a zip file: the xml report, the text trace file and the screenshot grabbed in case of errors.
  • WatiN may support FireFox in the future, InCisif.net may support testing SilverLigth 2.0 application.

Pricing: WatiN is Free. InCisif.net is not, but we are about to reduce our price to $99 a license. We will also offer to license the source code later this year.
If you want to develop applications using InCisif.net and distribute them, we have what we call InCisif.net Run Time For Application which requires a simple files copy to install.
There are royalties between $25 and $50 per install based on the quantity.
 
Jesse, let me known what you think.

Frederic Torres
www.InCisif.net
Web Testing with C# or VB.NET

I think this week at home I'm going to take a look at this - Frederic was nice enough to send me a personal key and I just so happen to have a few sites I could use this against.  More to come on this, no doubt.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

.Net | C# | VB | Testing

Chapters in tandom

by Jesse 8. February 2008 03:23

I've been reading a chapter a night for the most part, except last night where I discovered one of them was 50+ pages and heavy on tech so I had to pay attention and it's covering good stuff that I -really- do want to know about.  Today however, I'm inbetween projects so I'm looking into a WCF book I have yet to really get into (I'm more interested in the security aspect of the book really) so, good time to do so -- learn some stuff, become a WCF ninja.  I'm all over it.

The WCF book I mentioned uses the all-mighty AdventureWorksDB as a backend.  Recently, one of the guys here at the office sent out a link for some downloads of other microsoft books for free, direct from microsoft, which I downloaded all 3 of them (why not?!) and then it hit me -- why not try to mesh the Linq book and the WCF book?  They're different and "unrelated" so this is crazy enough to work.  Kill two birds with one stone eh?

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.Net | C# | Coding | Tech

Selenium and the Kiss Theory

by Jesse 3. January 2008 03:17

I've been tasked with testing our ajax enabled admin site for a project we've been working on for a little while now.  Jim, the head dev, asked me to use this thing called Selenium to test it with since current unit tests wigs out with ajax items.  So off I go to figure this thing.  After a short learning curve, its not all that hard, but I DEFINITELY recommend some downloads to supplement.

If you're lucky enough to be like me and know absolutely nothing about how this thing works, don't worry -- it only took me ...2 hours? to get this running and firing off tests.  This should cut that down into a few minutes.  Before I send you off into the wild unknown, here's a market-less overview of what this thing is.  Selenium is a web unit tester that uses specifically formed html pages (tables, thats it) to run against your site.  That's it, thats all.  All you need is the site setup locally and this thing installed in another place, using the same IIS.  Now was that so bad?

Now for the meat.  Download this (its small, under 2MB), create a new IIS app and dump the zip file contents into it.  Wow that was hard.  Navigate your browser to your new IIS app/index.html and you should something like this.

Click the Selenium TestRunner, it'll bring you to another page, on the left, you'll see "Test Suite" and a link, click go, and a ton of stuff will appear.  At the top right, you'll see "Selenium TestRunner" in bold and "Execute tests" -- click the left most of those tests (this is run all) and wait a minute -- it'll rip though a ton of tests.  Most, if not all should pass (ideally, all pass).

Ok, great it works, now what?  Here's were I take everything on their site and ignore it.  "But what about all the functions, methods, blah blah?!?" I like to cheat and work more efficiently by letting software do the work and thankfully, they have and "IDE" for download as a firefox extension -- besides, reading the site its like reading msdn, yea its "cool" but 99.999999% of it you won't use.  Anyway, so now what?  That thing you just downloaded has ALL the stuff in it making it a million times easier to make this stuff work and now you can make tests to your hearts content, very quickly, which is great.

Now we switch gears a bit because as you will discover, its a ROYAL PITA to figure out what the controls are named and you WILL need this info.  So instead of hunting though that notepad, may I suggest you try this, IE Dev Toolbar.  Using this is easy and here's your learning curve.  After you've downloaded this go into IE and (if you don't have menu bar showing, you shall suffer) click view, Explorer Bar, IE Developer Toolbar and you'll get this at the bottom of your IE. 

You're only really worried about the first two buttons which are "Select Element by click" and "Refresh".  Basically if selecting by click isn't working, click the refresh button and it works.  To get this to work, click that first button and click something on a webpage you want to know.  The middle box will be populated and look as such.

You're only worried about "Name" which in this case is "connspeed" which you can double click on and copy/paste.  Now to bring all this together.  Go into firefox, click tools, Selenium IDE and close firefox.  You'll get this very uninteresting looking thing which is about to make your life easier.  Under "Command" in the big box, click the top most row (there's one there, believe me) and all the other controls below become active.  Oh and for the sake of blog, I'm only doing a SHORT example, open a page, type some stuff, click a link (google search).  This'll seem cumbersome at first, but its the fastest way I've found so far, so stick with me on this.

Where it says "Command/Target/Value" click the dropdown arrow and look for "open" (or just type it, it'll autocomplete), type in the base URL for your site and leave value blank.  Click the row below the command you just created (trust me again, there's one there) and now hunt for the "type" command (or again, type it) and here's where IE dev comes in handy -- open up google, and click the "Select By Element" button, click on the search text box.  You'll find its name is simply "q", so in the target column, type in Q and set your value for blog.rileytech.net.  Finally, we want to click the search button, so we give it another command of "click" with the target being the "Google Search" button which name value is "btnG".  Your result should look something like this.

Now save as html into the directory where you copied the Selenium files over earlier and remember the filename.  Only one final step to make this puppy work, you need a test master file of sorts -- its sole purpose is to house links to the tests you just created.  No more, no less.  So fire up your favorite html editor, create a table that looks something like this (or just copy this and save it)

<html><body><table><tr>
<td>Test Suite</td></tr>
<tr><td><a href="./mytest.html">google test</a></td></tr>
</table></body></html>

Go back into Selenium, Selenium Test Runner and type in the url for your newly created master file and it should come up nicely.  Run it like you did the other tests and away you go.  Other useful commands are "verify" commands which you'll find there's a nice collection of them, along with "assert" (assertCheck is a good one).

Wow that was a long post.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.Net | Visual Studio | C# | Coding | Design | Architecture

Telerik hates me

by Jesse 29. November 2007 03:01

I always manage to get the fun ones.  Infragistics, Telerik, they all hate me.  Well, thats half true, I can force the Infa controls to do stuff most of the time -- haven't figured out how to make these Telerik.  So I'm working with winforms RadTreeView.  Seems simple enough.  Has a object-ed datasource  along with various things like "datamember" "displaymember" etc, all the things you'd expect.  Checkin out their site, you can use business objects, datasets, datatables, all sorts of junk -- what could possibly go wrong?

Well, we have one already in use, works just fine but we had to manually build the "nodes" for the control.  I think its a royal bastardization BUT it works.  So, thinking I'm mr. slick, I toss in a collection (a list) into the datasource, tell it that "object.property" is where the goods are and ...build, load, nada.  Hmm ok, soooo lets try a dataset.  I try using a helper class that serializes the thing into a nice neat dataset and ...nada again.  Hmm ok fine.  I'll build it by hand, using the nodes as cumbersome as it is -- nada.  Now, throughly confused, I think to myself "why not toss it into the one that works? see what happens" -- all the above methods work ...in the existing view.  Undecided

I've left a post on the Telerik site hoping for a reply along the lines of "hey, you're a dummy, all you have to do is this" and presto, it'll work ...but until then, I'm con-freaking-fused.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.Net | Visual Studio | C# | Coding

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About the author

Like the description says, at my core, I'm a scientist and engineer.  I came from humble beginnings on a 486DX2 Packard Hell playing doom2 on IPX to in a small time retail shop and got into hardware (ISO layers FTW!) and it was all downhill from there.  I'm infinitely curious about almost everything and always wanting to know.

Some of the stuff I'm currently into/researching...

Sitefinity

Ninject

Subsonic

Java

Currently working on ...
i did the hundred 
and some extra stuff

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's, their brother nor their dog's view in anyway.  At all.  Ever.

© Copyright 2007-2008