Tuesday, May 13, 2008

Achieving Persistence Ignorance with NHibernate

with James Kovacs

Definitions

  • POCO- plain old CLR objects
  • Domain Driven Design - For most software projects, the primary focus should be on the domain and domain logic

Tools

  • RhinoETL - alternative to SSIS

Why NHIbernate

  • uses POCOs
    • NH does not impose requirement on objects
  • no persistence code strewn throughout the framework
    • like the Entity framework
  • Promotes Domain Driven Design
  • Its all about separation of concerns, single responsibility, loose coupling, testability, re-use of code. All that fun stuff.
  • Allows you to concentrate on the business logic
    • business logic is what’s important, not how it's stored

Nhibernate

  • An ISession is a conversation with a database
    • This is not thread safe. Go higher for thread safety.
  • There are three query methods
    • HQL like LINQ but string oriented
    • Criteria is OO with added parameters
    • LINQ to NH is in the works

Remember

  • You still need to test against the database
  • Isolation helps, not only with changing the data store, but with also different versions of the data store
  • In 2.0 transactions are now required even for reads.
    • You can roll back an entire transaction if an error is encountered.
  • You can use lazy loading to speed up performance
    • Use FuturyQueryOf if you are dealing with nested for each loops.
  • You need to explicitly set your cascade rules

EFPRS

  • Our database is not normalized. We have all the expired fields that will be null for most of the time in our tables. If we normalize that, we can take advantage of NHibernates polymorphism so distinguish active and expired entries.

The Speaker

  • reminds me of a Monty Python character

Notes

  • You can code backwards with ReSharper and Ctrl+r+v

XML LINQing in the Real World

with Paul D. Sheriff

What is LINQ to XML

  • LINQ to XML was developed with Language-Integrated Query over XML in mind and takes advantage of standard query operators and adds query extensions specific to XML
  • Found in System. XML. LINQ NameSpace and adds
    • XDocument type
    • XElement type
  • If you want to use XPath elements you need to add the NameSpace as the are extension methods
  • LINQ provides compile time exceptions where you’d rely on runtime exceptions before

VB.Net versus C#

  • VB.Net does a better job on the LINQ to XML stuff
  • VB.Net has XML literals
    • as long as you have an XSD file with your xml file, you'll get intellisense

Uses

  • useful for Rapid Application Development and prototyping
  • You can do CRUD of XML with LINQ too
  • Allows for easy creation of XML from scratch in code
    • use this for unit testing perhaps
  • LINQ allows you to aggregate data as well
    • VB.Net has an Aggregate/Into syntax as well. Probably not the best approach.
  • You can insert script blocks into inline SQL to create xml data that contains dynamic data <%=something%>
    • This is useful for creating xml documents from data in a database

Anonymous types

  • These are on the fly classes
  • Scoped within the block of code in which it is defined
  • You can return anonymous types, but you get no intellisense
  • These do not work with updates

EFPRS

  • The translat can be rewritten (again) to use this approach to make it more maintainable

Notes

Keynote with Scott Handslemen

Definitions

  • MCSE : minesweeper consultant and solitaire engineer
  • REST - Representational state transfer
    • Uses http
  • RESTafarian
  • atom is a formalization of syndication in the same vain as RSS
  • atom is a standard
  • monkey code is code that is easy to say, long to do
  • monkey coder is someone who codes monkeys

Tools

  • Fiddler - http debugging proxy
  • LINQpad - kicks complete ass. Like query analyser
  • TcpTrace - pocketsoap.com - port forwarding for localhost
  • DataSvcUtil.exe does auto generation of code from schema
  • LINQ to NHibernate
    • In the works

Dynamic Data Web Applications

  • provides the Web application scaffolding that enables you to build rich data-driven Web applications
  • Extends the views (dataview, etc.) in the ASP.Net controls to make data binding suck less
  • Basically seems to be a really smart, simple, and pretty way of displaying data
  • Creates a dynamic data folder which holds templates
  • Also make found column sucks less
  • Bound columns should be less horrible
  • Adds the system.web.routing namespace
  • Get vs.net 2008 sp1 to use this
  • Remember, urls do not always reflect what's on disk
  • DynamicDataRoute
  • Everything you are given can be changed
  • everything is displayed according to its Datatype (magic)
  • dynamic data is a way of keeping things DRY
  • DynacicField are used as a replacement for BoundField
  • move towards convention over configuration
    • that's what it is, live with it

Astoria - ADO.Net Data Services

  • SOAP versus REST both have their place (REST when you just want to get data)
  • Gives access to data contexts
    • Can expose tables/views in an aspx page
  • Can set granular data access
    • Security etc.
  • .svc files are simply pointers
    • I don’t really know what these are, but if you see them, they are just pointers
  • Lock down development server port for simplicity
    • This is normally set up dynamically, we could change these to static so we don’t have to change anything relying on them
  • The web is about interlinked resources
    • The web is a resource oriented architecture
  • Data is returned as an atom feed
    • Just like a blog site would be.
    • The result is just syndicated data
  • This is a formalization of a data model
  • 16 verbs in http used in REST
    • get, post. put, delete are CRUC operations
  • object identity is not the same as object structure
  • So, just because two objects look alike, doesn’t mean they are the same
  • Remember, http header contains a lot of information you can use as a developer

Concepts

  • any problem in programming can be solved by an additional layer of abstraction
  • any advance technology is indistinguishable from magic
  • therefore, any sufficient layer of abstraction is indistinguishable from magic

LINQ

  • remember, you can LINQ to anything, not just SQL
  • LINQ is not tunneled, doesn’t use no strings
  • LINQ has deferred execution

Notes

  • %20 represents a 'space' in a url
  • Microsoft offers MVC (Model-View-Controller) as a new project type too

DevTeach Notes

  • Touch typing is very important!

Day 2

Alright, we just finished up our second long day of DevTeach. We left the hotel at 7 this morning and I just got back at 6:15, so I’m tired and I somehow came up with the idea what I should start posting all this info I wrote down. I knew this was a bad idea at lunch time when I’d already taken over 150 lines of notes. So, you’ll start to see these trickle up onto the web as soon as I can get around to it. Both Mark and I attended the Keynote talk this morning, but for the rest of the day we attended different talks; 5 in all. I’ll probably make a separate section for each. So, you’ll pretty much get to have the proxy DevTeach Experience without the very very long days and uncomfortable chairs. However, you also don’t get the advantage of sitting through a Beth Massi talk either, so it’s give and take.

Monday, May 12, 2008

Pragmatic Application Design

with William R. Vaughn

Stored Procedures

I haven’t really been a big fan of Stored Procedure (SPs) lately, however, after this talk I’m leaning towards these ground rules.

1. If your code is simply doing CRUD (create/read/update/delete) then SPs are probably a clumsy way of handling these. Try an O/R mapper maybe.

2. If your code is retrieving a block of data, doing some processing on it, and then storing it back to the database. This should probably be done on the database side in a SP.

3. If the data you are retrieving does not map to objects in your use cases (reports for example contain specific data which do not match entities in your use cases) then you could probably benefit from SPs.

4. If your code is not running as fast as it needs to, then it is time to optimize. Run tests on different approaches to see which produces the best real time performance.

Performance

  • Don’t put anything in form load for windows applications.
  • Connections are not free. Do not create any more than you need.
  • Performance is more often about how intelligently you ask the database your question
  • Send answers to client, not the data to find the answer. Saves bandwidth.
  • Select * is dangerous in a query. Returns too much unneeded data.
  • Fully qualify objects in your select statements so time is not wasted qualifying them on the database side.
  • Keep blobs out of the database. You only have to much cache space, don’t waste them on photos.
  • Keep transactions short. Long transactions are expensive to commit and rollback.
  • Watch your rowset size. Don’t bring back too much data.
  • Make design change decisions based upon REAL performance information. Don’t prematurely optimize your queries.
  • Don’t' bring data to the client for processing. Not only is it slow, it’s not secure.
  • Keep data on the server where it is faster and more secure.
  • Process data on the server which is designed to process data.
  • SPs run the same as ad hoc queries as long as the ad hoc query plan is cached. Or, once a SQL statement has been run once, it will run as fast as a SP as long as it is still “compiled” on the server.
  • Return focused subsets of your data using where clauses. Only return what you need.
  • Be careful of the using operator to close connections. The connections will not closed until GC is called and you have no idea when that will be.
  • VSs.net does not regenerate the data schema when the database changes so be careful of any auto generated code.

Good Design

  • Leverage existing resources (code, people, libraries, etc.)
  • Ensure your user knows you application has started. Don’t make them wait. Show a splash screen at the very least.
  • Make your interface intuitive. Actually watch how the user uses your system.
  • Be conscious of who owns data and who can change it. Remember concurrency. Do you lock data or let multiple users update a record (pessimistic versus optimistic). How do you handle collisions?
  • Protect your data. Encryption.
  • Manage your data in the database, no on the client.
  • Get data to the user as soon as possible. Optimize where it needs to be done. Don’t leave your user waiting.
  • Set indexes based on real life usage data, not expectations.
  • Good design and architecture requires discipline.
  • Use the right tools for the right job.
  • Work with the client for reports!
  • Use a gui and tools when it's easier to use a gui and tools
  • Soft code business rules and values. Morph to business rules to data. Don’t hardcode.
  • Before you use LINQ, do a lot of research. This applies to all new technologies.
  • Logically / physically separate concerns.
  • Asp.net must used disconnected data
  • Test your backups (restores) before you need them.
  • Remember, you must restore the base system as well (try virtualization).

Database design

  • People and users are different things. Accounts are for roles in a database. Don’t have a Mark Gerow user in the database, have a developer role.
  • Leverage nulls where nulls are appropriate (to show unknown values).
  • Consider concurrency: Optimistic versus Pessimistic
  • SQL server is half-duplex while Oracle is full duplex
  • Distributed client systems (database sync) are coming for SQL Server.
  • A middle-tier layer and a Data Access Layer are different.
  • SQL Sever Compact Edition is a file database for client side databases.
  • You can start SQLS manually or automatically.
  • SQL Server has two files master/log = .mdb/.ldb
  • Don't optimize database column sizes unnecessarily

Security

  • Developers should run under least privileges. How little access does my code need?
  • Ad hoc SQL queries are dangerous. They are vulnerable to SQL injection attacks.
  • Use the runas command to test access restrictions.
  • Do not edit data on an un-trusted client.

Environment

  • Bill recommends dual monitors. Management studio on one, VS.Net on the other. Saves ALT-Tabbing and makes you more productive.

Bulk Data

  • SQL is not designed for bulk operations.
  • Use SSIS for bulk data operations even with oracle.

EFPRS improvement ideas

  • Use the timestamp on an update to determine collisions.
  • Create views or SPs for reports.

General DevTeach Stuff

  • We need business cards for these courses
  • Laptops are a great investment

The CLR and Stored Procedures

  • CLR sp are dangerous and hard.
  • Don't debug on prod server as it will stop working.
  • There are performance issues.
  • When you debug sp, all CLR executables within SQLSERVER are paused (CLR engine stops)

Definitions

  • OSFA – One Size Fits All
  • A DataTable is a RowSet. Set of rows returned from a query

The Book

  • Chapter 9 is wrong. You can get the new one from the website

Notes

  • Use .\Name for local database instances
  • Tools: manager, profiler, bis, ssis, configuration manager, server explorer

Day 1

At DevTeach in 2007 Dean and I took a lot of notes, well, at least I know I did. From those notes we tried to compile a list of book, blogs, industry resources, and learning topics which we tried to shared with the group. However, at the end of the day those notes ended up in our filing cabinets largely forgotten. This year, since I have access to a laptop for the entire conference, I decided to try taking my notes on the PC and then compiling them at the end of the day and posting them online. This way everyone can view the notes, get an idea of what was covered, and, hopefully, in this way the data will be persisted a little more. So, with no further ado, here we go!