How to avoid anemic domain models, or when to move methods from the entities into services
I think this particular issue can be solved elegantly with a Domain Event.
I think this particular issue can be solved elegantly with a Domain Event.
As you have already figured out, Dependency Injection (DI) itself is only a collection of patterns and techniques. At the root of the application we wire up all necessary object graphs. This place is called the Composition Root, and we can use a DI Container to do this wiring for us, or we can do … Read more
“Code against an interface, not an object” doesn’t make literal sense in Python because the language doesn’t have an interface feature. The rough Python equivalent is “use duck typing.” If you want to see if an object is a duck, in other words, you should check to see whether it has a quack() method, or … Read more
You can’t make constructors generic, but you can use a generic static method instead: public static Constructor CreateInstance<T>(int blah, IGenericType<T> instance) and then do whatever you need to after the constructor, if required. Another alternative in some cases might be to introduce a non-generic interface which the generic interface extends. EDIT: As per the comments… … Read more
The principle purpose of MEF is extensibility; to serve as a ‘plug-in’ framework for when the author of the application and the author of the plug-in (extension) are different and have no particular knowledge of each other beyond a published interface (contract) library. Another problem space MEF addresses that’s different from the usual IoC suspects, … Read more
For this, I need an interface and maybe a container for resolving my instances. But how you do this in C++? In the same way. The difference is that where you “program to an interface” in C#, you “program to a base class” in C++. Additionally, you have extra tools in C++ that you do … Read more
SimpleIoc crib sheet: You register all your interfaces and objects in the ViewModelLocator class ViewModelLocator { static ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); if (ViewModelBase.IsInDesignModeStatic) { SimpleIoc.Default.Register<IDataService, Design.DesignDataService>(); } else { SimpleIoc.Default.Register<IDataService, DataService>(); } SimpleIoc.Default.Register<MainViewModel>(); SimpleIoc.Default.Register<SecondViewModel>(); } public MainViewModel Main { get { return ServiceLocator.Current.GetInstance<MainViewModel>(); } } } Every object is a singleton by default. To … Read more
Having a switch case statement inside of a factory is a code smell. Interestingly, you don’t seem to be focusing on solving that issue at all. The best, most DI friendly solution for this scenario is the strategy pattern. It allows your DI container to inject the dependencies into the factory instances where they belong, … Read more
Put simply (because it’s not a problem limited to OOP world only), a dependency is a situation where component A needs (depends on) component B to do the stuff it’s supposed to do. The word is also used to describe the depended-on component in this scenario. To put this in OOP/PHP terms, consider the following … Read more
When boiled down, the main difference is that IoC containers are generally most useful with static dependencies (known at compile-time), and MEF is generally most useful with dynamic dependencies (known only at run-time). As such, they are both composition engines, but the emphasis is very different for each pattern. Design decisions thus vary wildly, as … Read more