使用Castle Windsor创建对象而不是工厂类
好的,我有一个基本类型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您 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.
如何使用这样的东西:
但我认为你的问题与依赖注入无关。
编辑:
如果您需要使用 castle 来创建产品,那么您可以使用如下内容:
您需要向 ProductFactory 注册 IProductFactory 并且它应该可以工作。很抱歉,如果它们是代码中的一些错误,但我现在没有 Visual Studio 来测试它。
How about to use something like this:
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:
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.