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

Sitefinity and Custom Modules - Part 2.5

by Jesse 14. May 2008 03:44

in the past couple posts, I've been covering custom modules in sitefinity with subsonic.  This morning, I hit a snag and I'm not sure how I'm going to go about fixing it.  Immediately after one of the overloads, the SalesStatManager constructor is called where it gathers the provider info and promptly bombs out as such...

Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Could not find a type for a name.  The type name was 'SalesStats.DefaultProvider'.

Source Error:
Line 215: <providers>
Line 216: <clear/>
Line 217: <add name="Sales" securityProviderName="" type="SalesStats.DefaultProvider" conectionStringName="DefaultConnection" visible="true"/>
Line 218: </providers>
Line 219:</salesStats>

I've submitted a question to one of their devs in hopes they can tell me its something stupid (it has to be) and I can move forward with my uber cool research.  I love these projects.

Be the first to rate this post

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

Tags: , ,

.Net | Misc

Sitefinity and Custom Modules

by Jesse 12. May 2008 03:41

I don't know why but it seems the crazy complicated research projects get tossed at me - "Hey, figure out how ________ works".  Well today, it's Sitefinity and custom modules.  Consider this my notes as I work to make this stupid simple and easy.

Before I go down this path, I've collected a ton of links with various things on it that hopefully will make my life easier.  So far, not so much.  I've got a couple problems I'll have to overcome.

  • The ORM (I'm guessing) has generated classes -somehow- and I have no idea where to make those change/update/whatever.  In the sample project Sample.Contracts.Data there's a "Department.dbclass" -- how that is made I have no idea.  There's no app.config, nothing that might tell me how, but I'm guessing I'll have to look into "Nolics.Engine.v4.2" (A referenced class) to find out how its generating this info.  Again, there's no explaination in the sample -- I expect a google search to fix this though.
     
  • The example's out of date (1 year old, May 2007).  Right when I loaded it up, one of the implementations is obsolete - "IControlPanelCommand" needs to be replaced with "CommandPanelBase".  Doesn't seem to be an issue (builds no problem), I'm just wondering what other goodies might be inside...

Good news is it builds right after you re-reference the dlls, no problems there.

*Update* 11:30am - ugh, found the ORM and its freakin expensive, 950 euros.  I'd rather use subsonic but who knows what kinda problems that'll cause...I feel more research coming on.

*more Updates* 5pm - ok, subsonic is very doable, I might even have it all setup and ready.  I'm going to pound out a bit more code and I think its done.  We'll see.

Be the first to rate this post

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

Tags:

.Net | Coding

SilverSurvey

by Jesse 28. April 2008 15:22

Quick post -- I tried to get into the project to take out a reference to an object but oh well, my internet connection doesn't want to stay connected for long.  For now, its live, everyone can view it and watch the progress.  I need to get wcf finalized and the visuals are a bit woeful (see the post on the silverengine) but we'll see where this goes!

Take a look at www.codeplex.com/silversurvey

Be the first to rate this post

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

Tags: ,

.Net | Silverlight | Linq

Sitefinity

by Jesse 25. April 2008 03:07

I'm starting to like these types of research projects -- I learn a ton of stuff, sometimes painfully, but its never time wasted.  Now I'm playing with SiteFinity by telerik -- which is why I needed the virtual PC.  Install is easy, download it from their site BUT there's a slight process involved.

Upon completion of my VPC, you have to install IIS (duh)

So I do.  I install sql server express with advanced services (I want the management studio) -- done and done.  Set the network service account to have write permissions to C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files (you'll get an error later).  Next, install the exe and away you go.

From here, it's stupid simple.  Click create project and you'll end up with a nice CMS template site to start playing with.

Be the first to rate this post

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

Tags: , ,

.Net | Tech

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

ClubStarterKit Beta 3 (w/ subsonic)

by Jesse 9. April 2008 16:01

This past fall, I was asked to help coach a kids team, 12-13 year olds.  I played for years, know a ton of stuff about it -- I use to pitch, play first and left (I got wheels and can track down a ball).  Not to be overjocked, I knew we needed a website of some kind and remembered the club starter kit -- gah! There's 3 now.  I notice one is on codeplex and has a dazzling "2.0" on it ...I'm skeptical so I check it out.  Subsonic with a beta 3 is out!  Well, luck would have it I'm using subsonic on the project I'm currently working on.  Ha, this'll be easy.

Right out of the box, there were a couple little things with just general clicking around and removing the default content, no big deal.  I removed the forums area (nothing like having little Johnny's psycho mom going nuts on a public site) and started to completely rewire the stats area.  The site really isn't designed out of the box for a baseball team, so I went off and added a batting and pitching section, along with a base page that displays these.  Slap on some update panels and presto, I got a nice, clean looking stats page with uber cool updating.  I divided up the adding into two parts, Game and Player.  From there, you can add players to a "Roster" and therefore change what players were in that game, you can then input their stats which drive the main page.  This is a members only area of course, but otherwise, coooool stuff.

baselist.JPG (13.15 kb) - this is an empty stats page, extra links added along with "Sponsors"

mod-addgame.JPG (16.80 kb) - add game "pop up"

mod-addplayer.JPG (8.92 kb) - add player "pop up"

I'll post up my code here sometime soon (meant to do it last night when I started this writing) along with a blank database. 

Be the first to rate this post

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

Tags: ,

.Net | Coding | VB

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

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