Note that there are some explanatory texts on larger screens.

plurals
  1. POStructuremap constructor overloading
    primarykey
    data
    text
    <p>I have a command class that needs to have 2 constructors. However, using structuremap it seems that I can only specify one constructor to be used. I have solved the problem for now by subtyping the specific command class, which each implementation implementing it's own interface and constructor. Like the code below shows. The ISelectCommand implements two separate interfaces for the string constructor and the int constructor, just for the sake of registering the two subtypes using structuremap.</p> <p>However, I consider this a hack and I just wonder why is it not possible for structuremap to resolve the constructor signature by the type passed in as parameter for the constructor? Then I could register the SelectProductCommand as an ISelectCommand and instantiate it like: ObjectFactury.With(10).Use>(); orObjectFactury.With("testproduct").Use>();</p> <pre><code>public class SelectProductCommand : ISelectCommand&lt;IProduct&gt;, ICommand, IExecutable { private readonly Func&lt;Product, Boolean&gt; _selector; private IEnumerable&lt;IProduct&gt; _resultList; public SelectProductCommand(Func&lt;Product, Boolean&gt; selector) { _selector = selector; } public IEnumerable&lt;IProduct&gt; Result { get { return _resultList; } } public void Execute(GenFormDataContext context) { _resultList = GetProductRepository().Fetch(context, _selector); } private Repository&lt;IProduct, Product&gt; GetProductRepository() { return ObjectFactory.GetInstance&lt;Repository&lt;IProduct, Product&gt;&gt;(); } } public class SelectProductIntCommand: SelectProductCommand { public SelectProductIntCommand(Int32 id): base(x =&gt; x.ProductId == id) {} } public class SelectProductStringCommand: SelectProductCommand { public SelectProductStringCommand(String name): base(x =&gt; x.ProductName.Contains(name)) {} } </code></pre> <p>P.s. I know how to tell structuremap what constructor map to use, but my again my question is if there is a way to have structuremap select the right constructor based on the parameter passed to the constructor (i.e. using regular method overloading).</p>
    singulars
    1. This table or related slice is empty.
    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.
    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