I’m converting a legacy system’s data layer to use NHibernate. The old db is loaded with Stored Procs that I want to map. I’m getting torn up by the naming of things. These procs do not return simple entities, they return a mess. An example would be something like this:
USE [MYDB]
GO
/****** Object: StoredProcedure [dbo].[GetInvoiceRenewals] Script Date: 06/21/2012 10:24:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[GetInvoiceRenewals]
@ClientId int,
@UserId int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF (@ClientId IN (SELECT ClientId FROM dbo.UserClientIds(@UserId)))
BEGIN
SELECT I.InvoiceId, I.ClientId, I.InvoiceTypeId, I.InvoiceNumber, I.QuoteNumber, I.PONumber,
I.TotalListPrice, I.EstimatedFee, I.ActualFee, I.DateCreated, u.firstname + ' ' + u.lastname as 'UserCreated',i.billingAddressID,i.shippingAddressID,
I.ActualClientPrice,
(select InvoiceStatus from InvoiceStatus where invoiceStatusID = (Select Case When Not Exists (Select * from InvoiceStatusHistory where InvoiceId=I.InvoiceID) Then 1
Else (Select top 1 InvoiceStatusID from InvoiceStatusHistory where InvoiceId=I.InvoiceID Order By ActionDate Desc) End)) as Status
FROM Invoice I, [user] u
WHERE I.ClientId = @ClientId AND I.IsActive = 1 AND I.InvoiceTypeId = 3 and u.userID = i.userCreated
END
END
--Select Case When Not Exists (Select * from InvoiceStatusHistory where InvoiceId=I.InvoiceID) Then 1
-- Else (Select InvoiceStatusID from InvoiceStatusHistory where InvoiceId=I.InvoiceID) End
--
--InvoiceStatusID is NULL then 1 else InvoiceStatusID END from InvoiceStatusHistory
-- where InvoiceId=1860 Order By ActionDate Desc
Now picture hundreds of those. I’m obviously going to write a script to generate all of these into hbm files and to create the entities as well. What I’m looking for is a good naming structure for these proc entities? Any thoughts?
I don’t think that NHibernate is really a good fit for this. If you’re converting it to NHibernate then you should really map your entities to tables (roughly), not to stored procs.
Also in this example you have logic in your stored proc which, imo, would be better expressed in code, which would require mapping your entities to tables and working from those entities.
Since you want to migrate to NHibernate, I would suggest mapping your entities to tables and then migrate the application in steps to using entities, and leave it as is using stored procs (if you want, you can always call the stored procs using session.CreateSQLQuery(“…”))