Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <pre><code>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 </code></pre> <p>Then with this in place I can define my catalog service as follows:</p> <pre><code>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 </code></pre> <p>Ninject will provide the factory (which is awesome!) assuming I setup the bindings like this:</p> <pre><code>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()) </code></pre> <p>I've omitted the FastenerCatalogEntry for brevity; the ClothingCatalogEntry is like this:</p> <pre><code>Public Class ClothingCatalogEntry Public Sub New(ByVal entity As CatalogEntryEntity) ... </code></pre> <p>It was <a href="https://stackoverflow.com/q/9416132/1246347">this post</a> that helped me the most to figure this out. I used UseFirstParameterAsNameInstanceProvider exactly as shown there.</p>
 

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