Apr 28 2010

Would you use the Action delegate to avoid duplication of code?

I just asked a question that helps about using generics (or polymorphism) to avoid duplication of code. I am really trying to follow the DRY principle.

So I just ran into the following code...

Sub OutputDataToExcel()

        OutputLine("Output DataBlocks", 1)
        OutputDataBlocks()
        OutputLine("")

        OutputLine("Output Numbered Inventory", 1)
        OutputNumberedInventory()
        OutputLine("")

        OutputLine("Output Item Summaries", 1)
        OutputItemSummaries()
        OutputLine("")

End Sub

Should I rewrite this code to be as follows using the Action delegate...

Sub OutputDataToExcel()
    OutputData("Output DataBlocks", New Action(AddressOf OutputDataBlocks))
    OutputData("Output Numbered Inventory", New Action(AddressOf OutputNumberedInventory))
    OutputData("Output Item Summaries", New Action(AddressOf OutputItemSummaries))
End Sub

Sub OutputData(ByVal outputDescription As String, ByVal outputType As Action)
    OutputLine(outputDescription, 1)
    outputType()
    OutputLine("")
End Sub

I realize this question is subjective. I am just wondering about how religiously you follow DRY. Would you do this?

Seth

Apr 28 2010

Would you use the Action delegate to avoid duplication of code?


I just asked a question that helps about using generics (or polymorphism) to avoid duplication of code. I am really trying to follow the DRY principle.

So I just ran into the following code...

Sub OutputDataToExcel()

        OutputLine("Output DataBlocks", 1)
        OutputDataBlocks()
        OutputLine("")

        OutputLine("Output Numbered Inventory", 1)
        OutputNumberedInventory()
        OutputLine("")

        OutputLine("Output Item Summaries", 1)
        OutputItemSummaries()
        OutputLine("")

End Sub

Should I rewrite this code to be as follows using the Action delegate...

Sub OutputDataToExcel()
    OutputData("Output DataBlocks", New Action(AddressOf OutputDataBlocks))
    OutputData("Output Numbered Inventory", New Action(AddressOf OutputNumberedInventory))
    OutputData("Output Item Summaries", New Action(AddressOf OutputItemSummaries))
End Sub

Sub OutputData(ByVal outputDescription As String, ByVal outputType As Action)
    OutputLine(outputDescription, 1)
    outputType()
    OutputLine("")
End Sub

I realize this question is subjective. I am just wondering about how religiously you follow DRY. Would you do this?

Seth

Apr 28 2010

Would you use the Action delegate to avoid duplication of code?


I just asked a question that helps about using generics (or polymorphism) to avoid duplication of code. I am really trying to follow the DRY principle.

So I just ran into the following code...

Sub OutputDataToExcel()

        OutputLine("Output DataBlocks", 1)
        OutputDataBlocks()
        OutputLine("")

        OutputLine("Output Numbered Inventory", 1)
        OutputNumberedInventory()
        OutputLine("")

        OutputLine("Output Item Summaries", 1)
        OutputItemSummaries()
        OutputLine("")

End Sub

Should I rewrite this code to be as follows using the Action delegate...

Sub OutputDataToExcel()
    OutputData("Output DataBlocks", New Action(AddressOf OutputDataBlocks))
    OutputData("Output Numbered Inventory", New Action(AddressOf OutputNumberedInventory))
    OutputData("Output Item Summaries", New Action(AddressOf OutputItemSummaries))
End Sub

Sub OutputData(ByVal outputDescription As String, ByVal outputType As Action)
    OutputLine(outputDescription, 1)
    outputType()
    OutputLine("")
End Sub

I realize this question is subjective. I am just wondering about how religiously you follow DRY. Would you do this?

Seth

Apr 28 2010

How do you create a generic method in a class?

Hello, I am really trying to follow the DRY principle. I have a sub that looks like this?

Private Sub DoSupplyModel

        OutputLine("ITEM SUMMARIES")
        Dim ItemSumms As New SupplyModel.ItemSummaries(_currentSupplyModel, _excelRows)
        ItemSumms.FillRows()
        OutputLine("")

        OutputLine("NUMBERED INVENTORIES")
        Dim numInvs As New SupplyModel.NumberedInventories(_currentSupplyModel, _excelRows)
        numInvs.FillRows()
        OutputLine("")   

End Sub

I would like to collapse these into a single method using generics. For the record, ItemSummaries and NumberedInventories are both derived from the same base class DataBuilderBase.

I can't figure out the syntax that will allow me to do ItemSumms.FillRows and numInvs.FillRows in the method.

FillRows is declared as Public Overridable Sub FillRows in the base class.

Thanks in advance.

EDIT
Here is my end result

Private Sub DoSupplyModels()

    DoSupplyModelType("ITEM SUMMARIES",New DataBlocks(_currentSupplyModel,_excelRows)
    DoSupplyModelType("DATA BLOCKS",New DataBlocks(_currentSupplyModel,_excelRows)

End Sub

Private Sub DoSupplyModelType(ByVal outputDescription As String, ByVal type As DataBuilderBase)
    OutputLine(outputDescription)
    type.FillRows()
    OutputLine("")
End Sub

But to answer my own question...I could have done this...

Private Sub DoSupplyModels()

    DoSupplyModelType(Of Projections)("ITEM SUMMARIES")
    DoSupplyModelType(Of DataBlocks)("DATA BLOCKS")

End Sub

Private Sub DoSupplyModelType(Of T as DataBuilderBase)(ByVal outputDescription As String, ByVal type As T)
    OutputLine(outputDescription)
    Dim type as New DataBuilderBase (_currentSupplyModel,_excelRows)
    type.FillRows()
    OutputLine("")
End Sub

Is that right?

Seth

Apr 15 2010

Excel pivot tables do not support COUNT DISTINCT aggregation.

Just got confirmation from MR EXCEL that Excel does not support COUNT DISTINCT aggregation.

HERE is my question…

Is there a way to do a count distinct aggregation on a pivot table without messing with the source data.

For example. If I do a row labels of Customers and “customerProductsPurchased” like this…

CUSTOMERA 10
   SHIRT  5
   SHORTS  5
CUSTOMERB    12
   SHIRT    3
   SHORTS    5
   POLOSHIRT    4

This gives the total of all of each product purchased and the grand total on the customer header row.

What I want is something like this…

CUSTOMERA  2
   SHIRT
   SHORTS
CUSTOMERB  3
   SHIRT
   SHORTS
   POLOSHIRT

Notice that this tells me the number of distinct products that I sell that the customer purchased. CUSTOMERA is a customer for 2 of my products and CUSTOMERB is a customer for 3 of my products.

In the database world this is called a distinct count.

Googling seems to indicate that this cannot be done in the pivot table without doing it first in the source. But I thought I would ask here to confirm.

Here is the reply I got…

No, you can’t. You have to do it in the source data.

To read the whole thread (there’s a bit more)…

http://www.mrexcel.com/forum/showthread.php?t=461802

Seth

Apr 13 2010

How do I use Linq-to-sql to iterate db records?

I asked on SO a few days ago what was the simplest quickest way to build a wrapper around a recently completed database. I took the advice and used sqlmetal to build linq classes around my database design.

Now I am having two problems. One, I don't know LINQ. And, two, I have been shocked to realize how hard it is to learn. I have a book on LINQ (Linq In Action by Manning) and it has helped some but at the end of the day it is going to take me a couple of weeks to get traction and I need to make some progress on my project today.

So, I am looking for some help getting started.

Click HERE To see my simple database schema.
Click HERE to see the vb class that was generated for the schema.

My needs are simple. I have a console app. The main table is the SupplyModel table. Most of the other tables are child tables of the SupplyModel table.

I want to iterate through each of Supply Model records. I want to grab the data for a supply model and then DoStuff with the data. And I also need to iterate through the child records, for each supply model, for example the NumberedInventories and DoStuff with that as well.

I need help doing this in VB rather than C# if possible. I am not looking for the whole solution...if you can supply a couple of code-snippets to get me on my way that would be great.

Thanks for your help.

EDIT
For the record I have already written the following code...

Dim _dataContext As DataContext = New DataContext(ConnectionStrings("SupplyModelDB").ConnectionString)
Dim SMs As Table(Of Data.SupplyModels) = _dataContext.GetTable(Of Data.SupplyModels)()
Dim query = From sm In SMs Where sm.SupplyModelID = 1 Select sm

This code is working...I have a query object and I can use ObjectDumper to enumerate and dump the data...but I still can't figure it out...because ObjectDumper uses reflection and other language constructs I don't get. It DOES enumerate both the parent and child data just like I want (when level=2).

PLEASE HELP...I'M stuck. Help!

Seth

Apr 07 2010

What is the preferred method of building extremely lightweight business object / DAL now that I have finished a simple database?

Hello,

I have completed a simple database for a project. Only 6tables. Of the 6, one is a "lookup" table.

There is one "master" table that is the driver for the system. It is referenced as a foreign key by the other four tables.

Give that this step is completed. What is the FASTEST, EASIEST way to create POCOs/BizObjects that can load load the data and the child data.

Here are my CAVEATS.

*I don't want to spend more than 30-60 minutes learning how?
*There is very little biz logic needed in the POCOs. They will pretty much load data. Don't even really need to write back data.
*I already know CSLA (up to version 3) but I feel that is overkill for this little project.
*Nevertheless, I would love it if it ROOT objects could have collection classes that contain the CHILD objects as in CSLA...but again, without using CSLA.
*Please give the answer for .NET 35 but also if I was restricted to only use .NET 20.
*Ideally I could just point a tool at the database and the POCOs would be genn'ed. *FREE

Just curious what you guys use for this kind of scenario.

I understand that this question is subjective but I want to hear a variety of answers.

Seth

Apr 06 2010

To return a padded string from an INT in SQL do the following…

In my example, I wanted to pad the ID (int) of a table with leading zeros(0).  Here is the select to do it.

SELECT DivisionID, right( replicate( ‘0’, 5 ) + cast( DivisionID as varchar(10)) ,5) as ID_STRING FROM tblDivisions

Notice that the number 5 in the statement has to be the number of chars (length) of the padded string.  The ‘0’ is the padding.  This could just as easily be an space or and underscore.

This select returns the following:

DivisionID  ID_STRING
———– ———
827         00827
825         00825
766         00766
767         00767

Seth