59 Days of Code: Fluent NHibernate OR/M

2010 March 4
by Robert Schultz

To begin elaborating on frameworks and development decisions for the 59 Days of Code contest, the first thing I wanted to invest in was a good and stable OR/M framework.  Initially I was going to go with the new version of Entity Framework 4.0 with .NET but reading a little more into my options, I am going to go with Fluent NHibernate. If you are unaware, Fluent NHibernate is an excellent OR/M tool that allows you to declare your mappings to your database in a more fluent way through code, instead of tedious XML configuration files.  This makes your life a lot easier.  Plus, you get the benefit of very abstracted code which allows me to easier plugin a test framework (more on that later). To setup and configure Fluent NHibernate, first I created a NHibernate Session object helper:

private static ISessionFactory SessionFactory
{
	get
	{
		if (_sessionFactory == null)
		{
			_sessionFactory = Fluently.Configure()
				.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("MyDbConnFromWebConfig")))
				.Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<User>().Where(t => t.Namespace == "Namespace.Data.Entities")
				.Setup(s => s.FindIdentity = property => property.Name == property.DeclaringType.Name + "Id")))
				.BuildSessionFactory();
		}
		return _sessionFactory;
	}
}

public static ISession OpenSession()
{
	return SessionFactory.OpenSession();
}

What this does is basically use a singleton pattern to use NHibernate to map my data class library to my database. For example here is what my User class looks like:

public class User
{
	public virtual long UserId { get; private set; }
	public virtual DateTime CreatedDateTime { get; set; }
	public virtual long CreatedUserId { get; set; }
	public virtual DateTime ModifiedDateTime { get; set; }
	public virtual long ModifiedUserId { get; set; }
	public virtual DateTime LastActivityDateTime { get; set; }
	public virtual int UserRoleId { get; set; }
	public virtual string Username { get; set; }
	public virtual string Email { get; set; }
	public virtual string Password { get; set; }
	public virtual string Firstname { get; set; }
	public virtual string Lastname { get; set; }
	public virtual bool Enabled { get; set; }
}

In addition to that, you can also see I am using AutoMapping and setting up my entity identities by mapping the column to EntityName + “Id” so for users it would be UserId.

Without going too much into my architecture I want to quickly explain how I am using this session factory from my repositories to actually get the data. This part is where NHibernate gets fun. Now say I want to get a user by Username and Password. Now I just do something like the following:

using (ISession session = NHibernateHelper.OpenSession())
{
	User user = session.CreateCriteria<User>().Add(Restrictions.Eq("Username", username)).Add(Restrictions.Eq("Password", password)).UniqueResult<User>();
}

Bam! That’s it. NHibernate will automatically create your mappings and map the results back to your class. A lot of the build in methods they provide are very cool and allow me to fluently wite my queries without doing any plain text SQL or stored procedures.

Next I am looking into caching with NHibernate, as they have hooks into the ASP.NET cache provider and so forth.

59 Days of Code

2010 February 17

There is a local competition brewing up here in Fresno.  It’s a mobile and web app showcase where you have two months to write an application or at least polish the one you have and present it.  There will be prizes like sponsorship, media coverage and things like that.  I think this is a very good idea and just the right push for me to finally get off my ass and start working on my idea that’s been on hiatus forever.

So for now I am going to keep wraps on what my project is called or what it does, but I am going to blog my progress as much as possible.  This goes from conception, adopting development methodologies, coding, and finally pushing the product and getting ready for testing.

Once the website is up is up I will post it here but this should be interesting!

I’m Buzzing about Google Buzz

2010 February 11
by Robert Schultz

Google Buzz was released the other day and I’m really liking it.

One of the original things that attracted me to Facebook, was the ability to easily import data from my social networks to display to others.  Be it information automatically being imported from Youtube or Hulu, but also if I wanted to share a link it would give a decent amount of metadata to my friends.  But the flip-side of that is the constant noise and junk people put out there.  And the fact it’s taken me 2 years to gain the 100+ friends that I have.

With Google Buzz, they did something very nice.  Everyone who is your friend in Gmail automatically becomes your friend in Buzz.  No more spending years to find friends, send friend requests, etc.  Plus they support a lot of my social networks automatically with I’ve already filled out in my Google Profile.  So when I make a Twitter post it automatically comes through into Buzz.  Sweet.

So is this the end of me having to keep both HootSuite and Facebook open during the day to get my news and information?  Possibly.  I’m in Gmail all day as well anyway since it’s my main email client.  There is still some polishing up to do but overnight I think this has created major threats to Facebook, considering they have yet to integrate FriendFeed into their application for real-time social meta importing.

Goodbye Expensive Premium TV Service

2010 January 8
tags: ,
by Robert Schultz

Over the years I among everyone else has noticed the cost of cable or satellite television services has been increasing almost on an annual basis.  For me, this is becoming a burden because of the high costs I have to pay my cable provider, Comcast, each month.  Currently I am paying almost $70/month on cable TV which includes a DVR and also HD option.  I watch pretty much all my shows on networks which broadcast over the air for free, except for a few shows that come on Comedy Central or Adult Swim.

Why should I have to pay a premium for content I only watch about 10% of?  To think that in 2010 the cable and satellite providers have still not provided a la carte options is mind numbing.  Look at the music or movie model.  Apple among others have single handedly disrupted these age old models and re-invented them.  I no longer have to spend $15 on a CD just to hear two songs I want.  I can spend $2 within iTunes.  I can rent a movie digitally and completely bypass the rental companies or even having to buy a movie I will only watch once.

So why has the cable industry not changed?

Well, I think that is next.  If what this article says is true, then Apple is about to turn the cable/satellite provides on their heads.  Since most of the major networks own all of the smaller cable channels anyway, I would have no problem paying $30/mo for access to shows from specific networks that I want to watch.  Then, I could watch it when I wanted and where I wanted.  Probably even on my iPhone as well.

But before I get to that, I wanted to mention my current initiative I am taking: I am dumping cable TV completely.

That’s right, I cancelled my TV and to even do this was a very difficult thing for me to do.  Since I was probably 12 I’ve had a TV either with or without cable but mostly having cable.  Now days I don’t watch so much cable especially since the shows are just brain drain shows based on ridiculousness.  My plan is to go completely internet based for my TV options.  This will include a little jewel of a software called Boxee.

If you didn’t know, Boxee is a software that’s been around for a while in closed beta that essentially aggregates online content together into one clean interface.  It also has app support so you can get things like Pandora, Last.fm, Facebook and Twitter in there too.  Boxee is very cool as it can become a content hub for your living room.  They even have Netflix streaming support and are coming out with their own device which will be under $200 and have HDMI, 802.11n and stream 1080p.

I will be blogging my experiences here especially when I get the device to begin using instead of my netbook for now so continue reading here to discover is Boxee is worth it or not.

Favorite Places to Eat in Fresno

2010 January 4

I try to eat at as many difference places that I can, despite my willingness to still give in to fast food (but that’s supposed to change, see my 2010 resolutions here).  But I wanted to list a few of the ones that are my favorite here in Fresno especially from the past year that I have discovered.

Don Pepe’s Taqueria

I love this place.  I get the Al Pastor burrito every time I go here.  These guys really know how to make a killer burrito.

La Boulangerie

One of my favorite places to get a sandwich.  Sometimes the crowd is a little upper-class yuppy status but the food makes up for it.

Limón

This place is very good, it is Peruvian and is based out of San Francisco.  I really loved the decor and the friendliness of the staff.

Pho #90

I recently got into Pho and this is my current favorite Pho place in Fresno.

The Million Elephant

I still like eating my Thai at Million Elephant, it’s good and the staff is always very cool.

Veni Vidi Vici

For some reason I barely discovered this place to eat at.  For all of the years we’ve been coming here for night life events I’ve never once ate a real meal here.  Now I have and now it’s going to be a monthly ritual.  The Pork Chop was out of this world.

Charlotte’s Bakery

Hands down my favorite place for a fresh and delicious sandwich in the Tower District.

Samba Churrascaria Brazilian Steakhouse

My favorite place for Brazilian, the food here is awesome and the owner is one of the most inviting people I’ve met.

Reporting Engines for Simplicity and .NET

2009 December 23

One of the biggest challenges that faces our project is reporting.  Most of the custom requests that are made are regarding reports and currently when each report is created, it has to be done by hand by a developer.

This creates an extreme amount of strain on resources especially within a company that is not that large.  When I read The Enterprise and Scrum, one of the trending patterns the author recognized when consulting companies for Scrum was they were spending an unnecessary amount of time on support and bug fixing their product.

I wanted to find a way to avoid this and make reporting as easy as possible.  The primary requirements we wanted to have were:

  • Integration with the 3.5 framework well
  • Ability for a non-technical person to create or change reports
  • Ability to create reports 100% outside of Visual Studio environment
  • Reports can be portable
  • As cost effective as possible
  • Ability to create complex data structures and pivots

Looking at all of the reporting options and what I had worked with, the following reporting suites came to mind:

  • Crystal Reports
  • Data Dynamics Active Reports
  • Microsoft SQL Reporting Services

Crystal Reports

I have worked with Crystal Reports in the past but have had a lot of limitations with it.  Especially if you’re using the version that ships with Visual Studio, it has serious performance limitations.  Here is what an MSDN article says regarding this:

While you may allow access to the runtime software by many users, the version of the runtime software provided with Crystal Reports for Visual Studio .NET is a performance-limited edition intended for development purposes and use with Server/Web applications accessed by small groups. If a report is accessed while the server is at its performance limit, the request will be delayed until an existing request finishes. When a request is delayed, the server writes a warning message to the event log. After a finite period of time, the request will time-out. When this happens, the server writes another warning to the event log and throws an OutOfLicense exception.

This obviously would become an issue since we want to spend the least on licensing costs.  Also as far as I know Crystal Reports doesn’t have an independent report designer.

Data Dynamics Active Reports

We’ve used Active Reports on many projects and it’s actually quite a great reporting tool.  We use it to design our ticket reports which need a lot of flexibility since they’re printing out of a Boca printer and require custom ticket stock.  But the tickets themselves are pretty straight forward and don’t need complex reporting data structures.  The grouping logic in Active Reports isn’t bad but very complex groups seemed to not work the way I wanted.

Also the report designer component would need be built as well using their control which we don’t have time to do.

Microsoft SQL Reporting Services

I have been wanting to really spend some time with SQL Reporting Services for a while, I just haven’t had time to do so.  Now that I finally have I am glad I did.

SQL Reporting Services is a really great reporting component Microsoft has provided.  Since SQL Server 2005 Microsoft has shipped Reporting Services free of charge with your SQL Server license.

Sounds good to me.

What about complex reporting data?  Most definitely.  Reporting Services provides a control you use called a “Tablix” which allows you to build a very complicated grouping structure within your report tables.  It’s really, really powerful.

Also, almost every property on every control can be controlled via “Expressions”.  These are basically VB style code snippets that allow you to dynamically set groups, visibility, colors, you name it.

Microsoft also provides a report tool to design your reports 100% outside any development environment, for free.  This is called Report Builder.  It is an awesome tool and provides even more than you could find in the Visual Studio version of the report designer.

And lastly, we can embed the actual SQL query within the report file making it portable and transferable between different customers.

LocalReport vs ServerReport Snafu

After I initially did my tests, all was good.  Except one thing.  I found out that when you want to have your reports portable (saving to a local .rdl file), the report has a few problems:

  • In LocalReport mode, the report will NOT save any connection information with the report
  • But in the flip side, we don’t want to save connection info since the reports are supposed to be portable
  • There is no way to dynamically set the connection at all in LocalReport mode
  • Microsoft assumes if you’re in LocalReport mode, you would pass your data objects already constructed to the report.

But if this is the case, what is the point of designing the query within your report?  You can’t because if you need to allow the users to construct the query there is no way you can pass it from the server.

Creating a Custom RdlReport

What I ended up doing was writing up some sophisticated code to handle local reports.  This is what it does:

  • Report criteria page dynamically parses the selected report .rdl file and displays the parameters to the user
  • User selects parameters and runs the report.  When this happens, I automatically parse out all queries contained within the report, and add them to a Queries enumeration, also with each parameter within the query
  • Each query is dynamically constructed, with all parameters and ran on the server
  • The resulting data objects are then passed back to the report with the LocalReport.DataSources enumeration

In a nutshell, this is how we are dynamically running all report queries on the server and binding them back to the report.  Not including additional parameters being passed.  Then the result is being rendered to the client either in PDF or Microsoft Excel.

What About CSV?

One last requirement we had was we also need CSV option.  Crap.  But actually, after thinking about this for a second it was actually pretty easy.  All I need to do is render the Excel, then use OleDb to read the contents, and create a custom CSV by just parsing the spreadsheet.  Then stream this CSV to the response stream.  Problem solved.

This phase is wrapping up at the end of this year but I think this solution will be a very promising and flexible option.  I will try to post some code here soon to explain more in detail how I worked with the report structure dynamically.

2010

2009 December 3
by Robert Schultz

So I originally went looking for my 2009 resolutions post I wrote on this blog back when it was hosted on wordpress.com but I believe it never got imported over.  I wanted to reflect back on 2009 and see how many of my goals I accomplished.  Some of the items I can remember and have listed below.

Start learning Ruby and Ruby on Rails
Move my blog to a hosted Wordpress account
Finish my longtime in the making web application I’ve been working on
Update Microsoft certification
Pay off a lot of debt

So I think with those I didn’t do too bad.  But I need to really step it up in 2010.  I am turning 30 and I had a lot of things I really want to accomplish for myself.  So since it’s already December, I’m going to initiate my goals for 2010 already with some pretty clear projections.  These are going to get very specific but hey, if I mention it here maybe I will do it.

Continue to evolve with the Ruby language more
Pick up .NET 4.0 and start learning and using the new features
Update my Microsoft certification
Ship ShoWare 5.0
Complete development and ship 1.0 of my platform API service (details later)
Even though I have a gym membership, start going normally and get in shape and cut out junk food (this is hard)
Start utilizing DI, IoC and mock frameworks
Pay off the remaining of my debt
Pay off my wife’s car loan
Purchase furniture for the living room in our apartment
Purchase a large LCD television, preferably 52″ with 120hz refresh rate (Samsung baby)
Purchase a Wii and/or Xbox360 again
Purchase two CDJ turntables and a mixer to start mixing again
Do more volunteer work
Buy a 27″ iMac
Go to Burning Man 2010
Turn my music blog into something more than just a blog

    That is most of the items I can think of for now.  I really think I can do most of them, and having this list here will push me to cross off items as I do them.  2009 was a bad year and a lot of deaths, and I hope 2010 will be a happier place.

    Music Page

    2009 December 1
    by Robert Schultz

    I’ve created a page here giving an overview of all artists or DJs I’ve seen.  I just wanted to actually think about all of the people I’ve paid money to see and who influenced my life musically.

    Try Cloudberry S3 Explorer

    2009 November 12
    by Robert Schultz

    Recently I had the need to work with Amazon’s Simple Storage Service (S3) on large volumes and needed a way to easily work with the files.  Since their S3 service is on fact a service, I needed some sort of front-end application to manage this.  My friend referring me to a very cool application called Cloudberry S3 Explorer.

    I’m not going to go into all the details but this app is very cool.  Also free!  As in beer.  You can manage your S3 buckets, distributions which I use, since I’m using CloudFront as well.  Also it has backup-like features in it for synchronizing folders between your desktop and your S3 bucket.

    Did I mention it was free?

    They also have a bunch of other products as well but they have a fee, though it is nominal.  I recommend checking them out.

    PayPal X Innovate 2009 Was A Success

    2009 November 7

    My trip down to San Francisco for the first ever PayPal X Innovation conference has wrapped up.  I must say I left with an overall feeling of excitement this is by far one of the best conferences I’ve attended.  And for the $49 price tag we paid for the whole thing not counting hotel, I can’t complain.

    First off, one of our intentions of going was to determine if PayPal is going to integrate their new Parallel and Chained payments into PayFlow Pro.  Well they’re not, at least for now.

    The first day I went to four breakout sessions covering mobile, the new x.com developer portal and the new chained and parallel payment features.  The second day similiar with an added talk with some big venture capital people in a Q&A session.  The main features that were announced for Adaptive Payments are:

    • Currency Conversion: Allow currencies to automatically be converted across all currencies PayPal supports in the 180 countries they operate in
    • Pay Anyone: Pay anyone even if they don’t have a PayPal account (uses email).  If they don’t, they will be prompted to create a PayPal account to retrieve the funds
    • Pre-Approvals: Allow calls from the API to pre-approve specific amounts, which can automatically be deducted from later without approval.  These pre-approvals can also be configured with rules, expirations and so forth
    • Chained Payments: Allows payment from one sender to be split between multiple recipients; the sender is unaware of where each piece is going
    • Parallel Payments: Allows payment from one sender to be split between multiple recipients; the sender will know which receiver is getting each piece
    • Adaptive Accounts: API to create PayPal account on users behalf

    For me, the Chained Payments is key.  This is necessary when charging a fee for someone using your services (B-to-B-to-C) so that I don’t have to turn into a collections company billing customers each month for my fees.

    Also, everyone who was registered received a FREE Asus Netbook.  Pretty cool if you ask me.  And the dinner the first night was top class.

    See ya next year PayPal!