Note that there are some explanatory texts on larger screens.

plurals
  1. POuse Castle windsor to create an object instead of a factory class
    primarykey
    data
    text
    <p>ok so I have a base type:</p> <pre><code>ProductBase </code></pre> <p>and then i have some products:</p> <pre><code>Product1: ProductBase Product2: ProductBase Product3: ProductBase </code></pre> <p>Then a factory class:</p> <pre><code>public class productFactory { public productBase GetProduct(Enums.product buildProduct) { var outProduct = new ProductBase(); switch (buildProduct) { case Enums.Products.Product1: outMis = new Product1(); break; case Enums.Products.Product2: outMis = new Product2(); break; default: outMis = new Product1(); break; } return outProduct; } } </code></pre> <p>I know windsor can do this, I know this is a good thing. </p> <p>As far as I can see productBase will need to be an interface, with the products implementing it, but then I need to make them via the enum switch in windsor?</p> <p>Help and pointers (links) appreciated. This is in a MVC.net 3 project and I have windsor setup for the controllers and logging, so that bit I get - though clearly a massive gap in my understanding of IoC and DI!</p> <p>One way of doing it is this, in place of the productFactory</p> <p>going to answer my own question - from here: <a href="http://codeblitz.wordpress.com/2009/05/06/using-factory-method-pattern-with-systemactivator/" rel="nofollow">http://codeblitz.wordpress.com/2009/05/06/using-factory-method-pattern-with-systemactivator/</a></p> <p>instead of using the productFactory:</p> <pre><code>public class productFactory { public static MisProduct CreateProduct(Enums.Product productType) { string assemblyName = Assembly.GetExecutingAssembly().GetName().Name; //creates an instance based on the enum name ProductBase myProduct = Activator.CreateInstance( null, string.Format("{0}.Models.Products.{1}", assemblyName, productType) ).Unwrap() as ProductBase; if (myProduct == null) { //todo throw an Exception here. } return myProduct; } } </code></pre> <p>any more suggestions anyone?</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.
 

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