abstract-factory

Is it a bad practice to set dependencies to NULL in a IoC container and supply the dependencies at runtime?

空扰寡人 提交于 2019-12-02 02:12:30
问题 I have a SocketManager class that contains a Socket and other fields. All fields except the Socket can be injected during the composition of the object graph with a DI framework. My idea was to simply build the entire object graph upfront by leaving Socket empty and set it during runtime. This would allow me to complete the SocketManager instantiation at one point in the code and use that instance throughout my entire program (as it was already set as an dependency through the DI framework)?

Is it a bad practice to set dependencies to NULL in a IoC container and supply the dependencies at runtime?

偶尔善良 提交于 2019-12-02 00:16:12
I have a SocketManager class that contains a Socket and other fields. All fields except the Socket can be injected during the composition of the object graph with a DI framework. My idea was to simply build the entire object graph upfront by leaving Socket empty and set it during runtime. This would allow me to complete the SocketManager instantiation at one point in the code and use that instance throughout my entire program (as it was already set as an dependency through the DI framework)? Is that the standard way to "inject" runtime dependencies or is it bad practice? A abstract factory

Why does Abstract Factory use abstract class instead of interface?

那年仲夏 提交于 2019-12-01 15:22:14
问题 I am learning about design patterns and the first example in the book is about Abstract Factory. I have built the exercise in VS and all looks good, but there is one question that I wonder about. In the book the factory class is implemented like this: public abstract class AbstractVehicleFactory { public abstract IBody CreateBody(); public abstract IChassis CreateChassis(); public abstract IGlassware CreateGlassware(); } After completing the exercise I have noted that the above class can be

Dependency Injection with Ninject, MVC 3 and using the Service Locator Pattern

瘦欲@ 提交于 2019-11-30 06:49:35
Something that has been bugging me since I read an answer on another stackoverflow question (the precise one eludes me now) where a user stated something like " If you're calling the Service Locator, you're doing it wrong. " It was someone with a high reputation (in the hundred thousands, I think) so I tend to think this person might know what they're talking about. I've been using DI for my projects since I first started learning about it and how well it relates to Unit Testing and what not. It's something I'm fairly comfortable with now and I think I know what I'm doing. However, there are a

How should I select which concrete implementation should be instantiated based on the user choice?

冷暖自知 提交于 2019-11-29 12:49:59
I have an interface Fruit with two implementations Apple and Banana . I want to create a Fruit instance. The choice whether the concrete implementation should be an Apple or a Banana should be made by the user. I did not yet design the user interface, so there is no restriction how this choice is made by the user. I know there are the following options: usage of the abstract factory pattern usage of reflection to create an instance from a given class name usage of reflection to create an instance from a given class object What are the pros and cons of these options? Please note that while

How to implement an [GoF]-ish Abstract Factory Pattern using an IoC like Ninject

老子叫甜甜 提交于 2019-11-29 12:07:49
Abstract When the design requires an "Abstract Factory Pattern" like stated by the [GoF] including several products and over some product families, then setting up an IoC can become a bit tricky. Especially when the specific factory implementations need to be dispatched by runtime parameters and shared among some subsequent components. Given the follwing API, i was trying to set up my IoC (Ninject in this case) to retrieve Configuration objects configured through a IConfigurationFactory . The configuration stores an IFactory instance whoose implementation is determined by a runtime parameter

Dependency Injection with Ninject, MVC 3 and using the Service Locator Pattern

我们两清 提交于 2019-11-29 07:12:31
问题 Something that has been bugging me since I read an answer on another stackoverflow question (the precise one eludes me now) where a user stated something like " If you're calling the Service Locator, you're doing it wrong. " It was someone with a high reputation (in the hundred thousands, I think) so I tend to think this person might know what they're talking about. I've been using DI for my projects since I first started learning about it and how well it relates to Unit Testing and what not.

Avoiding all DI antipatterns for types requiring asynchronous initialization

梦想的初衷 提交于 2019-11-28 18:15:25
I have a type Connections that requires asynchronous initialization. An instance of this type is consumed by several other types (e.g., Storage ), each of which also require asynchronous initialization (static, not per-instance, and these initializations also depend on Connections ). Finally, my logic types (e.g., Logic ) consumes these storage instances. Currently using Simple Injector. I've tried several different solutions, but there's always an antipattern present. Explicit Initialization (Temporal Coupling) The solution I'm currently using has the Temporal Coupling antipattern: public

Why does Abstract Factory deal with families, and Factory Method with generating a single object?

落花浮王杯 提交于 2019-11-28 10:54:20
问题 From what I have read, the abstract factory pattern typically concerns itself with creating several objects which are all associated with the same family, and the factory method pattern concerns itself with generating a single object. Consider the following example, which flips those concerns: // Factory Method (base class) allowing for creation of families of objects public class BasePizzaCreator{ abstract ISauce CreateSauce(); abstract IToppings CreateToppings(); abstract ICrust CreateCrust

How should I select which concrete implementation should be instantiated based on the user choice?

旧城冷巷雨未停 提交于 2019-11-28 06:27:55
问题 I have an interface Fruit with two implementations Apple and Banana . I want to create a Fruit instance. The choice whether the concrete implementation should be an Apple or a Banana should be made by the user. I did not yet design the user interface, so there is no restriction how this choice is made by the user. I know there are the following options: usage of the abstract factory pattern usage of reflection to create an instance from a given class name usage of reflection to create an