Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7842225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:16:14+00:00 2026-06-02T16:16:14+00:00

The essence of my question is how to compose these objects (see below) in

  • 0

The essence of my question is how to compose these objects (see below) in a sensible way with MVC3 and Ninject (though I am not sure DI should be playing a role in the solution). I can’t disclose the real details of my project but here is an approximation which illustrates the issue/question. Answers in either VB or C# are appreciated!

I have several different products with widely varying properties yet all of them need to be represented in a catalog. Each product class has a corresponding table in my database. A catalog entry has a handful of properties specific to being a catalog entry and consequently have their own table. I have defined an interface for the catalog entries with the intent that calling the DescriptionText property will give me very different results based on the underlying concrete type.

Public Class Clothing
    Property Identity as Int64
    Property AvailableSizes As List(Of String)
    Property AvailableColor As List(Of String)
End Class

Public Class Fasteners
    Property Identity as Int64
    Property AvailableSizes As List(Of String)
    Property AvailableFinishes As List(Of String)
    Property IsMetric As Boolean
End Class

Public Interface ICatalogEntry
    Property ProductId as Int64
    Property PublishedOn As DateTime
    Property DescriptionText As String
End Interface

Given that the DescriptionText is a presentation layer concern I don’t want to implement the ICatalogEntry interface in my product classes. Instead I want to delegate that to some kind of formatter.

Public Interface ICatalogEntryFormatter
    Property DescriptionText As String
End Interface

Public Class ClothingCatalogEntryFormatter
    Implements ICatalogEntryFormatter

    Property DescriptionText As String
End Class

Public Class FastenerCatalogEntryFormatter
    Implements ICatalogEntryFormatter

    Property DescriptionText As String
End Class

In a controller somewhere there will be code like this:

Dim entries As List(Of ICatalogEntry)
                   = catalogService.CurrentCatalog(DateTime.Now)

In a view somewhere there will be code like this:

<ul>
@For Each entry As ICatalogEntry In Model.Catalog
    @<li>@entry.DescriptionText</li>
Next
</ul>

So the question is what do the constructors look like? How to set it up so the appropriate objects are instantiated in the right places. Seems like generics or maybe DI can help with this but I seem to be having a mental block. The only idea I’ve come up with is to add a ProductType property to ICatalogEntry and then implement a factory like this:

Public Class CatalogEntryFactory
    Public Function Create(catEntry as ICatalogEntry) As ICatalogEntry
        Select Case catEntry.ProductType
        Case "Clothing"
            Dim clothingProduct = clothingService.Get(catEntry.ProductId)
            Dim clothingEntry = New ClothingCatalogEntry(clothingProduct)
            Return result
        Case "Fastener"
            Dim fastenerProduct = fastenerService.Get(catEntry.ProductId)
            Dim fastenerEntry = New FastenerCatalogEntry(fastenerProduct)
            fastenerEntry.Formatter = New FastenerCatalogEntryFormatter
            Return fastenerEntry
    ...     
    End Function
End Class

Public ClothingCatalogEntry
    Public Sub New (product As ClothingProduct)
        Me.Formatter =  New ClothingCatalogEntryFormatter(product)
    End Sub

    Property DescriptionText As String
        Get
            Return Me.Formatter.DescriptionText
        End Get
    End Property
End Class

...FastenerCatalogEntry is omitted but you get the idea...

Public Class CatalogService
    Public Function CurrentCatalog(currentDate as DateTime)
        Dim theCatalog As List(Of ICatalogEntry)
                                  = Me.repository.GetCatalog(currentDate)

        Dim theResult As New List(Of ICatalogEntry)

        For Each entry As ICataLogEntry In theCatalog
            theResult.Add(factory.Create(entry))
        Next

        Return theResult
    End Function
End Class

IMHO, I am not really getting any smells off this code other than having to change the factory for every new product class that comes along. Yet, my gut says that this is the old way of doing things and nowadays DI and/or generics can do this better. Suggestions on how to handle this are much appreciated (as are suggestions on a better title…)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:16:16+00:00Added an answer on June 2, 2026 at 4:16 pm

    So making a few small changes I got this to work using the Ninject Factory extension.
    Biggest change is that my entities have enough info to display either type (clothes or fasteners in my contrived example) if the item is actually clothes then the fastener specific properties will be null and vice versa.

    Public Interface IDescribable
        ReadOnly Property DescriptionText As String
    End Interface
    
    Public Enum ProductType
        CLOTHING
        FASTENER
    End Enum
    
    Public Interface ICatalogEntry
        Inherits IDescribable
        ReadOnly Property ProductId As Int64
        ReadOnly Property PublishedOn As DateTime
        ReadOnly Property ProductType As ProductType
    End Interface
    
    Public Class CatalogEntryEntity
        Public Property ProductId As Long
        Public Property ProductType As ProductType
        Public Property PublishedOn As Date
        Public Property DescriptionText As String
        Public Property Color As String
        Public Property Finish As String
        Public Property IsMetric As Boolean
    End Class
    

    Then with this in place I can define my catalog service as follows:

    Public Class CatalogService
        Private ReadOnly _factory As ICatalogEntryFactory
        Private ReadOnly _repository As CatalogRepository
    
        Public Sub New(entryFactory As ICatalogEntryFactory, repository As CatalogRepository)
            Me._factory = entryFactory
            Me._repository = repository
        End Sub
    
        Public Function CurrentCatalog(currentDate As DateTime) As List(Of ICatalogEntry)
            Dim items = Me._repository.GetCatalog()
            Return (From item In items Select _factory.Create(item.ProductType.ToString(), item)).ToList()
        End Function
    End Class
    
    Public Interface ICatalogEntryFactory
        Function Create(bindingName As String, entity As CatalogEntryEntity) As ICatalogEntry
    End Interface
    

    Ninject will provide the factory (which is awesome!) assuming I setup the bindings like this:

    theKernel.Bind(Of ICatalogEntry)().To(Of ClothingCatalogEntry)().Named("CLOTHING")
    theKernel.Bind(Of ICatalogEntry)().To(Of FastenerCatalogEntry)().Named("FASTENER")
    theKernel.Bind(Of ICatalogEntryFactory)().ToFactory(Function() New UseFirstParameterAsNameInstanceProvider())
    

    I’ve omitted the FastenerCatalogEntry for brevity; the ClothingCatalogEntry is like this:

    Public Class ClothingCatalogEntry   
        Public Sub New(ByVal entity As CatalogEntryEntity)
    ...
    

    It was this post that helped me the most to figure this out. I used UseFirstParameterAsNameInstanceProvider exactly as shown there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Not sure if this is the usual sort of question that gets asked around
Not sure how to phrase this question, but here is the problem. I have
This question seems to be the essence of several others on this forum. I
My question is related to this link stackoverflow ques In essence repeating the figure
I have a question regarding jQuery. I have multiple images that are, in essence,
Disclaimer, I not do anything in particular with regards this question, just curious. Is
[EDITED: I left the original question below, with some more context and code to
Slightly strange question, but hopefully someone can help. In essence, if the time was
I have a basic question regarding the const pointers. I am not allowed to
SO! :) Simple question -- it's probably been asked, but I could not find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.