Previous posts can be found here: Part One – The Beginning Part Two – The Domain Model In part two we set up our domain model. Now, before we can test nhibernate’s ability to work with and persist objects, we need to ensure that we’ve defined our schema well enough that NHibernate can create the Schema for us (since that was kind of the point). Now is where NUnit starts to become very useful.
This is an archive of the posts published to LessThanDot from 2008 to 2018, over a decade of useful content. While we're no longer adding new content, we still receive a lot of visitors and wanted to make sure the content didn't disappear forever.
Which books should you read/buy when you are a programmer? I have listed 5 books that have helped me a lot. The books that I have chosen are not specific to any language although some of the books have examples in one language only. Design Patterns has examples in smalltalk and C++ but since the code is not very complicated you should have no problem converting it to your language of choice. I have included links to sample chapters for the books where I could find them. For some of the books I have also provided links to the author’s site; some of them have additional material so that you can look at that. I have also provided Amazon links so that you can read reviews. All of these books are rated 4 stars or higher. I have also provided alternate books if I felt that there were more choices for the same subject
Channel 9 has two screencast that deal with the new geography data type in SQL Server 2008. Saving Virtual Earth Polygons to SQL Server 2008 Marc Schweigert shows you how to draw a polygon on a Virtual Earth map and save it using ASP.NET AJAX, Windows Communication Foundation (WCF), LINQ to SQL, and the new geography data type in SQL Server 2008. Rendering Polygons from SQL Server 2008 on Virtual Earth
Lets say I have a class teacher which inherits from a class person (hard to believe I know ;-)). And we want to bind a list of Iteachers (the interface) to a datagridview because that is the easiest way of getting things working for the datagridview. This will give a surprising (perhaps not) result. First lets look at the Person class and IPerson interface. Public Interface IPerson Property Name() As String Property FirstName() As String End Interface Public Class Person Implements IPerson Private _name As String Private _firstname As String Public Sub New() End Sub Public Sub New(ByVal Name As String, ByVal Firstname As String) _name = Name _firstname = Firstname End Sub Public Property Name() As String Implements IPerson.Name Get Return _name End Get Set(ByVal value As String) _name = value End Set End Property Public Property FirstName() As String Implements IPerson.FirstName Get Return _firstname End Get Set(ByVal value As String) _firstname = value End Set End Property End Class And then the Teacher class and ITeacher interface which inherits from IPerson.
Part One – The Beginning In part one we discussed what has brought me to the shameful point of using an object-relational mapper. At the risk of being ostracized from the database community, I really think this is going to be helpful for my project. The next step is to actually build up the domain model, and set up the mappings for NHibernate. I won’t be pasting all the code in here, but I will be attaching the project itself to the next post if anyone’s interested. I ended up working ahead of myself so I had to kind of go backwards to create a “part one” project (I got too carried away with working, forgot to check in for a few days 🙁 ). First let us look at the “recipe” domain object, which is at the center of everything.
[After writing about the extensions methods for a string][1] (the basic stuff) I was having so much fun that I decided to write another one. This time for an image. I always thought the getThumbnail method of the image class was to difficult to use and to hard to remember. So I extended Image and added a ToThumbnail method to it. First I wrote some unittests (of course) Imports NUnit.Framework Imports TDB2007.Utils.CommonFunctions.Extensions Imports System.Drawing Namespace Extensions ''' <summary> ''' A TestClass ''' </summary> ''' <remarks></remarks> <TestFixture()> _ Public Class TestImageExtensions #Region " Private members " ''' <summary> ''' ''' </summary> ''' <remarks></remarks> Private _Image As Image ''' <summary> ''' ''' </summary> ''' <remarks></remarks> Private _NewImage As Image #End Region #Region " Setup and TearDown " ''' <summary> ''' Sets up the Tests ''' </summary> ''' <remarks></remarks> <SetUp()> _ Public Sub Setup() _Image = Nothing _NewImage = Nothing End Sub ''' <summary> ''' Tears down the test. Is executed after the Test is Completed ''' </summary> ''' <remarks></remarks> <TearDown()> _ Public Sub TearDown() End Sub #End Region #Region " Tests " ''' <summary> ''' A Test ''' </summary> ''' <remarks></remarks> <Test()> _ Public Sub Test_To_see_if_thumbnail_is_created() Dim _Graphics As Graphics _Image = New Bitmap(100, 100) _Graphics = Graphics.FromImage(_Image) _Graphics.FillRectangle(Brushes.Blue, New Rectangle(0, 0, 100, 100)) _Graphics.DrawLine(Pens.Black, 10, 0, 10, 100) Assert.IsNotNull(_Image) _NewImage = _Image.ToThumbnail(10) Assert.IsNotNull(_NewImage) Assert.AreEqual(10, _NewImage.Width) Assert.AreEqual(10, _NewImage.Height) Assert.AreEqual(Color.FromArgb(255, 0, 0, 255), (CType(_NewImage, Bitmap)).GetPixel(0, 0)) Assert.AreEqual(Color.FromArgb(255, 0, 0, 229), (CType(_NewImage, Bitmap)).GetPixel(1, 1)) End Sub ''' <summary> ''' A Test ''' </summary> ''' <remarks></remarks> <Test()> _ Public Sub Test_To_see_if_keepsaspectratio() Dim _Graphics As Graphics _Image = New Bitmap(100, 200) _Graphics = Graphics.FromImage(_Image) _Graphics.FillRectangle(Brushes.Blue, New Rectangle(0, 0, 100, 200)) _Graphics.DrawLine(Pens.Black, 10, 0, 10, 200) Assert.IsNotNull(_Image) _NewImage = _Image.ToThumbnail(20, True) Assert.IsNotNull(_NewImage) Assert.AreEqual(10, _NewImage.Width) Assert.AreEqual(20, _NewImage.Height) End Sub ''' <summary> ''' A Test ''' </summary> ''' <remarks></remarks> <Test()> _ Public Sub Test_To_see_if_thumbnail_doesnt_keep_aspec_ratio() Dim _Graphics As Graphics _Image = New Bitmap(100, 200) _Graphics = Graphics.FromImage(_Image) _Graphics.FillRectangle(Brushes.Blue, New Rectangle(0, 0, 100, 200)) _Graphics.DrawLine(Pens.Black, 10, 0, 10, 200) Assert.IsNotNull(_Image) _NewImage = _Image.ToThumbnail(20, False) Assert.IsNotNull(_NewImage) Assert.AreEqual(20, _NewImage.Width) Assert.AreEqual(20, _NewImage.Height) End Sub #End Region End Class End Namespace``` I even checked to see if the thumbnail was created by checking if the color of the pixels was correct. Look at how I cheated by first running the test and then seeing which color it came up with for where the black line was suppossed to be. but since the black line was very small the pixel will be another color because of loss of data. And now for the most important part. The Extension method ```vbnet ''' <summary> ''' ''' </summary> ''' <param name="Input"></param> ''' <param name="MaximumSize"></param> ''' <param name="KeepAspectRatio"></param> ''' <returns></returns> ''' <remarks></remarks> <Extension()> _ Public Function ToThumbnail(ByVal Input As Image, ByVal MaximumSize As Integer, Optional ByVal KeepAspectRatio As Boolean = True) As Image Dim ReturnImage As Image Dim _Callback As Image.GetThumbnailImageAbort = Nothing Dim _OriginalHeight As Double Dim _OriginalWidth As Double Dim _NewHeight As Double Dim _NewWidth As Double Dim _NormalImage As Image Dim _Graphics As Graphics _NormalImage = New Bitmap(Input.Width, Input.Height) _Graphics = Graphics.FromImage(_NormalImage) _Graphics.DrawImage(Input, 0, 0, Input.Width, Input.Height) _OriginalHeight = _NormalImage.Height _OriginalWidth = _NormalImage.Width If KeepAspectRatio = True Then If _OriginalHeight > _OriginalWidth Then If _OriginalHeight > MaximumSize Then _NewHeight = MaximumSize _NewWidth = _OriginalWidth / _OriginalHeight * MaximumSize Else _NewHeight = _OriginalHeight _NewWidth = _OriginalWidth End If Else If _OriginalWidth > MaximumSize Then _NewWidth = MaximumSize _NewHeight = _OriginalHeight / _OriginalWidth * MaximumSize Else _NewHeight = _OriginalHeight _NewWidth = _OriginalWidth End If End If Else _NewHeight = MaximumSize _NewWidth = MaximumSize End If ReturnImage = _ _NormalImage.GetThumbnailImage(Convert.ToInt32(_NewWidth), Convert.ToInt32(_NewHeight), _Callback, _ IntPtr.Zero) _NormalImage.Dispose() _NormalImage = Nothing _Graphics.Dispose() _Graphics = Nothing _Callback = Nothing Return ReturnImage End Function``` And yes all the test pass. Cool huh! Perhaps not because it lacks some documentation, but then again that’s real life programming ;-). <font color="Red">Need help with VB.Net? Come and ask a question in our <a href="http://forum.lessthandot.com/viewforum.php?f=39">VB.Net Forum</a></font> [1]: /index.php/DesktopDev/MSTech/vb-net-extending-string-with-extension-m
This article is based on something [Jeff Atwood][1] did. It reminded me of something I missed an something that could save me some time. I need a simple left and right method on a string variable. So I went about and did it ;-). First I googled around a bit to find out how I should do it in VB.Net (plenty of c# examples can be found). And I found this [blogpost by Chris Rock][2] which helped a lot.
It can be usefull to be able to create an instance of a class via reflection. I wouldn’t use it to much because it has a code smell. But anyway. I wrote this little windowsapplication that creates some controls. This is the code in that form. Imports System.Reflection Public Class Form3 Dim controlsList As List(Of Control) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Not String.IsNullOrEmpty(Me.ComboBox1.Text) Then Dim asm As [Assembly] '*** get assembly from WordAssist.com asm = [Assembly].GetAssembly(GetType(System.Windows.Forms.Control)) '*** create object instance by having full class name Dim obj As Control = asm.CreateInstance("System.Windows.Forms." & Me.ComboBox1.Text, True) obj.Top = 20 * controlsList.Count obj.Left = 20 obj.Height = 20 obj.Width = 100 obj.Visible = True obj.Name = Me.ComboBox1.Text & controlsList.Count obj.Text = obj.Name controlsList.Add(obj) Me.SplitContainer1.Panel2.Controls.Add(obj) End If End Sub Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load controlsList = New List(Of Control) End Sub End Class``` I used a form and this is the designer code for that. ```vbnet <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Partial Class Form3 Inherits System.Windows.Forms.Form 'Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) Try If disposing AndAlso components IsNot Nothing Then components.Dispose() End If Finally MyBase.Dispose(disposing) End Try End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.ComboBox1 = New System.Windows.Forms.ComboBox Me.Label1 = New System.Windows.Forms.Label Me.Button1 = New System.Windows.Forms.Button Me.SplitContainer1 = New System.Windows.Forms.SplitContainer Me.SplitContainer1.Panel1.SuspendLayout() Me.SplitContainer1.SuspendLayout() Me.SuspendLayout() ' 'ComboBox1 ' Me.ComboBox1.FormattingEnabled = True Me.ComboBox1.Items.AddRange(New Object() {"TextBox", "ComboBox", "ListBox", "Label"}) Me.ComboBox1.Location = New System.Drawing.Point(61, 3) Me.ComboBox1.Name = "ComboBox1" Me.ComboBox1.Size = New System.Drawing.Size(121, 21) Me.ComboBox1.TabIndex = 0 ' 'Label1 ' Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(3, 3) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(40, 13) Me.Label1.TabIndex = 1 Me.Label1.Text = "Control" ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(198, 3) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(75, 23) Me.Button1.TabIndex = 2 Me.Button1.Text = "create" Me.Button1.UseVisualStyleBackColor = True ' 'SplitContainer1 ' Me.SplitContainer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill Me.SplitContainer1.Location = New System.Drawing.Point(0, 0) Me.SplitContainer1.Name = "SplitContainer1" Me.SplitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal ' 'SplitContainer1.Panel1 ' Me.SplitContainer1.Panel1.Controls.Add(Me.Label1) Me.SplitContainer1.Panel1.Controls.Add(Me.Button1) Me.SplitContainer1.Panel1.Controls.Add(Me.ComboBox1) Me.SplitContainer1.Size = New System.Drawing.Size(639, 414) Me.SplitContainer1.SplitterDistance = 46 Me.SplitContainer1.TabIndex = 3 ' 'Form3 ' Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(639, 414) Me.Controls.Add(Me.SplitContainer1) Me.Name = "Form3" Me.Text = "Form3" Me.SplitContainer1.Panel1.ResumeLayout(False) Me.SplitContainer1.Panel1.PerformLayout() Me.SplitContainer1.ResumeLayout(False) Me.ResumeLayout(False) End Sub Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents SplitContainer1 As System.Windows.Forms.SplitContainer End Class
Microsoft Security Bulletin MS08-040 Important Vulnerabilities in Microsoft SQL Server Could Allow Elevation of Privilege (941203) This security update resolves four privately disclosed vulnerabilities. The more serious of the vulnerabilities could allow an attacker to run code and to take complete control of an affected system. An authenticated attacker could then install programs; view, change, or delete data; or create new accounts with full administrative rights. This security update is rated Important for supported releases of SQL Server 7.0, SQL Server 2000, SQL Server 2005, Microsoft Data Engine (MSDE) 1.0, Microsoft SQL Server 2000 Desktop Engine (MSDE 2000), Microsoft SQL Server 2005 Express Edition, Microsoft SQL Server 2000 Desktop Engine (WMSDE), and Windows Internal Database (WYukon).
Tibor Karaszi has created a very useful index information stored procedure for SQL Server 2005 and up. This stored procedure will tell you the following" What indexes exists for a or each table(s) Clustered, non-clustered or heap Columns in the index Included columns in the index Unique or nonunique Number rows in the table Space usage How frequently the indexes has been used Check it out here: http://www.karaszi.com/SQLServer/util_sp_indexinfo.asp