views:

337

answers:

1

Hi Guys, I am trying to create a domain model using a product class entity an Abstract repository and a fake repository with sample data.

I have created the following product class

Namespace Entities
Public Class Product
    Dim _ProductID As Integer
    Dim _Name As String
    Dim _Description As String
    Dim _Price As Decimal
    Dim _Category As String
    Public Property ProductID() As Integer
        Get
            Return _ProductID
        End Get
        Set(ByVal value As Integer)
            _ProductID = value
        End Set
    End Property

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Public Property Description() As String
        Get
            Return _Description
        End Get
        Set(ByVal value As String)
            _Description = value
        End Set
    End Property

    Public Property Price() As Decimal
        Get
            Return _Price
        End Get
        Set(ByVal value As Decimal)
            _Price = value
        End Set
    End Property

    Public Property Category() As String
        Get
            Return _Category
        End Get
        Set(ByVal value As String)
            _Category = value
        End Set
    End Property
End Class
End Namespace

And the following interface:

Namespace Abstract
Public Interface IProductsRepository
    ReadOnly Property Products() As IQueryable(Of Product)
End Interface
End Namespace

I now need to create data using a generic LINQ list however I am stuck, this is what I have so far but I cannot add items:

Imports DomainModel.Abstract
Imports DomainModel.Entities

Namespace Concrete
Public Class FakeProductsRepository
    Implements IProductsRepository
    Private Shared fakeProducts As IQueryable(Of Product) = New List(Of Product)().AsQueryable

    Public ReadOnly Property Products() As System.Linq.IQueryable(Of Entities.Product) Implements Abstract.IProductsRepository.Products
        Get
            Return fakeProducts
        End Get
    End Property

End Class
End Namespace

Sample Item:

fakeProducts.Add(New Product() With {.Name = "Football", .Price = 25})

Help is greatly appriciated..

A: 

I now need to create data using a generic LINQ list however I am stuck, this is what I have so far but I cannot add items:

What do you mean by “create data”? Also, LINQ is used to retrieve data, not the opposite. You can simply put data into a list and return that list – it is queryable:

Private ReadOnly fakeProducs As New List(Of Product)
...

fakeProducts.Add(New Product() With {.Name = "Football", .Price = 25})

...

Public ReadOnly Property Products() As System.Linq.IQueryable(Of Entities.Product) Implements Abstract.IProductsRepository.Products
    Get
        Return fakeProducts.AsQueryable()
    End Get
End Property
Konrad Rudolph