使用Castle Windsor创建对象而不是工厂类

发布于 2025-01-07 19:01:02 字数 1916 浏览 1 评论 0原文

好的,我有一个基本类型:

ProductBase

然后我有一些产品:

Product1: ProductBase

Product2: ProductBase

Product3: ProductBase

然后是一个工厂类:

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;
        }
    }

我知道温莎可以做到这一点,我知道这是一件好事。

据我所知,productBase 需要是一个接口,由产品实现它,但是我需要通过温莎中的枚举开关来制作它们?

感谢帮助和指示(链接)。这是在 MVC.net 3 项目中,我为控制器和日志记录设置了温莎,所以我得到了这一点 - 尽管我对 IoC 和 DI 的理解显然存在巨大差距!

一种方法是这样,代替 ProductFactory

来回答我自己的问题 - 从这里开始: http://codeblitz.wordpress.com/ 2009/05/06/using-factory-method-pattern-with-systemactivator/

而不是使用productFactory:

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;
    }
}

还有其他建议吗?

ok so I have a base type:

ProductBase

and then i have some products:

Product1: ProductBase

Product2: ProductBase

Product3: ProductBase

Then a factory class:

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;
        }
    }

I know windsor can do this, I know this is a good thing.

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?

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!

One way of doing it is this, in place of the productFactory

going to answer my own question - from here:
http://codeblitz.wordpress.com/2009/05/06/using-factory-method-pattern-with-systemactivator/

instead of using the productFactory:

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;
    }
}

any more suggestions anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回旧景 2025-01-14 19:01:02

我建议您 Typed Factory

您可以解决通过 id 或类型本身需要具体类型,这取决于您如何接收有助于做出具体选择的参数。

根据您的示例,枚举数可能与组件 id 相关。

I suggest you Typed Factory

You may resolve the needed concrete type by id or type itself, that depends on how you receive the parameter that helps to make the concrete choice.

Base on your sample, enumerator may be related to component id.

苏璃陌 2025-01-14 19:01:02

如何使用这样的东西:

public class ProductFactory
{
    public T Create<T>() where T : ProductBase, new()
    {
        return new T();
    }
}

但我认为你的问题与依赖注入无关。

编辑:
如果您需要使用 castle 来创建产品,那么您可以使用如下内容:

public interface IProductFactory
{
    T Create<T>() where T : ProductBase, new();
    void Destroy(ProductBase obj)
}

public class ProductFactory : IProductFactory
{
    private readonly WindsorContainer _container;

    public ProductFactory(WindsorContainer container)
    {
         _container = container;
    }

    public T Create<T>() where T : ProductBase, new()
    {
        return _container.Resolve<T>();
    }

    public void Destroy(ProductBase obj)
    {
        _container.Release(obj);
    }
}

您需要向 ProductFactory 注册 IProductFactory 并且它应该可以工作。很抱歉,如果它们是代码中的一些错误,但我现在没有 Visual Studio 来测试它。

How about to use something like this:

public class ProductFactory
{
    public T Create<T>() where T : ProductBase, new()
    {
        return new T();
    }
}

But I think your question has nothing to do with dependenci injection.

Edit:
If you need to use castle for Products creation then you can use something like this:

public interface IProductFactory
{
    T Create<T>() where T : ProductBase, new();
    void Destroy(ProductBase obj)
}

public class ProductFactory : IProductFactory
{
    private readonly WindsorContainer _container;

    public ProductFactory(WindsorContainer container)
    {
         _container = container;
    }

    public T Create<T>() where T : ProductBase, new()
    {
        return _container.Resolve<T>();
    }

    public void Destroy(ProductBase obj)
    {
        _container.Release(obj);
    }
}

You need to register IProductFactory with ProductFactory and it should work. Sorry if they are some mistakes in code but I don't have a Visual Studio now to test it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文