Note that there are some explanatory texts on larger screens.

plurals
  1. POComposing polymorphic objects in ASP.NET MVC3 project
    primarykey
    data
    text
    <p>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!</p> <p>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. </p> <pre><code>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 </code></pre> <p>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.</p> <pre><code>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 </code></pre> <p>In a controller somewhere there will be code like this:</p> <pre><code>Dim entries As List(Of ICatalogEntry) = catalogService.CurrentCatalog(DateTime.Now) </code></pre> <p>In a view somewhere there will be code like this:</p> <pre><code>&lt;ul&gt; @For Each entry As ICatalogEntry In Model.Catalog @&lt;li&gt;@entry.DescriptionText&lt;/li&gt; Next &lt;/ul&gt; </code></pre> <p>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:</p> <pre><code>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 </code></pre> <p>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...)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload