Tag: 工厂

没有服务定位器的工厂模式

我目前只是在努力编写一个不依赖服务位置的工厂类。 我能想到的唯一其他选择是使用构造函数注入来注入所有可能的实例,但这可能会导致意外,因为类是通过引用传递的。 一旦可能的提供商数量增加,它也可能会变得昂贵和混乱。 提供者本身是完全复杂的类,它们有自己的依赖关系,因此手动构造是不可能的。 更新了服务位置示例: public class ProviderFactory : IProviderFactory { private readonly IProviderConfigurationService _providerConfigurationService; public enum SearchType { Foo, Bar } public ProviderFactory(IProviderConfigurationService providerConfigurationService) { _providerConfigurationService = providerConfigurationService; } public Collection GetProviderInstances(SearchType searchType) { // Provider configuration service will read a XML/DB store to retrieve list of search providers applicable for a search type […]

在不知道类型的情况下返回通用

我有一种情况,我有一个类,在其generics类型参数中接受某个对象类型的实例。 布局是这样的: public abstract BaseClass { … } public DiamondClass : BaseClass { … } public SilverClass : BaseClass { … } public Handler where T : BaseClass { … } 我希望能够创建一个方法来返回Handler或Handler的实例,而无需在输入时定义类型。 我试过这些方面的东西: public Handler GetHandler(HandlerType type) { switch(type) { case HandlerType.Diamond: return new Handler(); case HandlerType.Silver: return new Handler(); default: throw new InvalidOperationException(“…”); } […]

工厂基于Typeof或是

在下面的代码中,我希望根据另一个类的类型返回派生类,这是一种更优雅的方法。 if (option_ is Rectangle) { modelInputs = new Foo(); } else if (option_ is Circle) { modelInputs = new Bar(); } else if (option_ is Triangle) { modelInputs = new Bar2(); }

IoC容器是否取代了工厂的使用

我刚刚开始使用IoC容器,如果这是一个愚蠢的问题,请道歉。 我在应用程序中有如下代码 internal static class StaticDataHandlerFactory { public static IStaticDataHandler CreateHandler(StaticDataUpdate staticDataUpdate) { if (staticDataUpdate.Item is StaticDataUpdateOffice) { return new OfficeUpdateHandler(); } if (staticDataUpdate.Item is StaticDataUpdateEmployee) { return new EmployeeUpdateHandler(); } if (staticDataUpdate.Item == null) { throw new NotImplementedException( string.Format(“No static data provided”)); } else { throw new NotImplementedException( string.Format(“Unimplemented static data type of {0}”, […]

我的DDD工厂类应该采用哪些方法?

我正在努力了解我的工厂类应该在我的DDD项目中做什么。 是的,工厂应该用于创建对象,但究竟应该做什么。 考虑以下工厂类: public class ProductFactory { private static IProductRepository _repository; public static Product CreateProduct() { return new Product(); } public static Product CreateProduct() { //What else would go here? } public static Product GetProductById(int productId) { //Should i be making a direct call to the respoitory from here? Greener.Domain.Product.Product p = _repository.GetProductById(productId); return p; […]

抽象工厂设计模式

我正在为我的公司开发一个内部项目,项目的一部分是能够将XML文件中的各种“任务”解析为稍后要运行的任务集合。 因为每种类型的Task都有许多不同的关联字段,所以我认为最好用一个单独的类来表示每种类型的Task。 为此,我构建了一个抽象基类: public abstract class Task { public enum TaskType { // Types of Tasks } public abstract TaskType Type { get; } public abstract LoadFromXml(XmlElement task); public abstract XmlElement CreateXml(XmlDocument currentDoc); } 每个任务都inheritance自此基类,并包含从传入的XmlElement中创建自身所需的代码,以及将自身序列化为XmlElement。 一个基本的例子: public class MergeTask : Task { public override TaskType Type { get { return TaskType.Merge; } } // Lots […]

如何防止类在工厂外实例化

我有一个工厂。 我不想允许这个工厂生产的类在工厂外实例化。 如果我将它们变成抽象的,静态的,或者给它们私有的构造函数,那么它们就不会是可实例化的! 这是语言限制还是什么? 我不想允许这个 var awcrap = new Extrude2013 (); // BAD !!! awcrap.extrudify (); // I don’t want to allow this 其余代码: using System; namespace testie { public enum ExtrudeType { Extrude2013, Extrude2014 } public interface IExtrudeStuff { void extrudify(); } public class Extrude2013 : IExtrudeStuff { public void extrudify(){ Console.WriteLine (“extrudify 2013”); […]

GoF Factory的命名约定?

此模式使用抽象工厂,然后是工厂的实现。 我确信这两个类有一个标准的命名约定,但我不知道它是什么。 例如: public abstract class ChocolateFactory { }; public class MyChocolateFactory { } : ChocolateFactory 这里的标准惯例是什么? 我正在考虑ChocolateFactoryBase或ConcreteChocolateFactory,但也许还有别的东西(很像Enum往往以Enum为后缀,例如PetTypeEnum这样你就可以做PetTypeEnum PetType; 希望这不是主观的。

C#任务工厂超时

我必须在一个线程中执行一个长进程操作,并继续将结果返回给一个函数。 这是我的代码: Task.Factory.StartNew(() => { try { // long operation which return new ProductEventArgs with a list of product } catch (Exception e) { return new ProductEventArgs() { E = e }; } }).ContinueWith((x) => handleResult(x.Result), TaskScheduler.FromCurrentSynchronizationContext()); 问题实际上我没有超时。 我想放一个计时器,以便返回这样的东西: new ProductEventArgs() { E = new Exception(“timeout”) }; 如果达到超时。 不能使用await / async。 非常感谢 !

是否可以使用工厂方法使用ac#object initializer?

我有一个带有静态工厂方法的类。 我想调用工厂来检索类的实例,然后通过c#object initializer语法进行额外的初始化: MyClass instance = MyClass.FactoryCreate() { someProperty = someValue; } VS MyClass instance = MyClass.FactoryCreate(); instance.someProperty = someValue;