开发者

.net data transformation (one object type to another) - looping

I've been banging my head against this problem for a couple of hours now, if anyone here can offer any pointers I would be grateful. I think I am now snow blinded by this one so a fresh set of brain cells will probably see a really simple solution!

Basically, I have some data I am pulling from a legacy database that needs to be transformed into our stock management system by constructing our own Stock object.

The simplified model is shown below:

Public Class Stock
    Public Property Attributes As List(Of StockAttribute)
End Class

Public Class StockAttribute
    Public Property AttributeName As String
    Public Property AttributeValue As String
End Class

The source data comes in as this object:

Public Class SourceOption
    Public Property OptionName As String
    Public Property OptionValue As String
End Class

Some sample data can be constructed using these methods:

Function GetSourceList1() As List(Of SourceOption)

    'prepare the source data
    Dim options As New List(Of SourceOption)

    Dim opt As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "Small"

    Dim o开发者_如何学JAVApt2 As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "Medium"

    Dim opt3 As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "Large"

    Dim opt4 As New SourceOption
    opt.OptionName = "Colour"
    opt.OptionValue = "Black"

    Dim opt5 As New SourceOption
    opt.OptionName = "Colour"
    opt.OptionValue = "Purple"


    options.Add(opt)
    options.Add(opt2)
    options.Add(opt3)
    options.Add(opt4)
    options.Add(opt5)

    Return (options)

End Function


Function GetSourceList2() As List(Of SourceOption)

    'prepare the source data
    Dim options As New List(Of SourceOption)

    Dim opt As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "40"

    Dim opt2 As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "41"

    Dim opt3 As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "42"


    options.Add(opt)
    options.Add(opt2)
    options.Add(opt3)

    Return (options)

End Function

Function GetSourceList3() As List(Of SourceOption)

    'prepare the source data
    Dim options As New List(Of SourceOption)

    Dim opt As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "Small"

    Dim opt2 As New SourceOption
    opt.OptionName = "Size"
    opt.OptionValue = "Large"

    Dim opt3 As New SourceOption
    opt.OptionName = "Colour"
    opt.OptionValue = "Red"

    Dim opt4 As New SourceOption
    opt.OptionName = "Colour"
    opt.OptionValue = "Blue"

    Dim opt5 As New SourceOption
    opt.OptionName = "Style"
    opt.OptionValue = "A"

    Dim opt6 As New SourceOption
    opt.OptionName = "Style"
    opt.OptionValue = "B"

    options.Add(opt)
    options.Add(opt2)
    options.Add(opt3)
    options.Add(opt4)
    options.Add(opt5)
    options.Add(opt6)

    Return (options)

End Function

So, my "Convert" method needs to take the SourceOption lists and construct Stock lists (with Attributes) as explained here:

Sub Convert()

'we need to convert the SourceOption list (in GetSourceList) into a list of Stock, each with a list of StockAttribute. So in the case of GetSourceList1 we would have
'six stock objects, each stock object containing two StockAttribute objects (one for colour and one for size):

'STOCK1 - Attribute1: Size:Small Attribute2: Colour:Black
'STOCK2 - Attribute1: Size:Medium Attribute2: Colour:Black
'STOCK3 - Attribute1: Size:Large Attribute2: Colour:Black
'STOCK4 - Attribute1: Size:Small Attribute2: Colour:Purple
'STOCK5 - Attribute1: Size:Medium Attribute2: Colour:Purple
'STOCK6 - Attribute1: Size:Large Attribute2: Colour:Purple

'in the case of GetSourceList2 we would have three stock objects with only one stockattribute each (for size):

'STOCK1 - Attribute1: Size:40
'STOCK2 - Attribute1: Size:41
'STOCK3 - Attribute1: Size:42

'in the case of GetSourceList3 we would have eigth stock objects with three stockattribute each (for size, colour and style):

'STOCK1 - Attribute1: Size:Small Attribute2: Colour:Red Attribute3: Style:A
'STOCK2 - Attribute1: Size:Large Attribute2: Colour:Red Attribute3: Style:A
'STOCK3 - Attribute1: Size:Small Attribute2: Colour:Blue Attribute3: Style:A
'STOCK4 - Attribute1: Size:Large Attribute2: Colour:Blue Attribute3: Style:A
'STOCK5 - Attribute1: Size:Small Attribute2: Colour:Red Attribute3: Style:B
'STOCK6 - Attribute1: Size:Large Attribute2: Colour:Red Attribute3: Style:B
'STOCK7 - Attribute1: Size:Small Attribute2: Colour:Blue Attribute3: Style:B
'STOCK8 - Attribute1: Size:Large Attribute2: Colour:Blue Attribute3: Style:B

End Sub

I do hope that's clear, and any feedback would really be appreciated before I tear all of my hair out!

Thanks Carl


I think you will find helpful to have a look here at ValueInjecter. It can help you to convert from object to object by appling some convention you can define.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜