Use of a mixin with the exit code - dart

Mixin Problem
Without the use of a mixin, the following code in the default_project works:
abstract class ProjectGen extends ConceptEntity<Project> {
With the use of the ConceptEntity mixin, there is a problem, without an error message, with the exit code=139 on Ubuntu 14.04 LTS, and the exit code=-1073741819 on Windows 7.
abstract class ProjectGen extends Object with ConceptEntity<Project> {
The restrictions on the ConceptEntity mixin in dartling are fulfilled:
ConceptEntity does not declare a constructor.
Its superclass is Object.
It does not ontain calls to super.
default_project
https://github.com/dzenanr/default_project
ProjectGen in lib/gen/default/project/projects.dart
dartling
https://github.com/dzenanr/dartling
ConceptEntity in lib/domain/model/entity.dart
class ConceptEntity<E extends ConceptEntity<E>> implements EntityApi {

Related

Get services extended from a certain interface

Is possible to get all services extended from a certain interface in grails
So I can collect them within one service
Yes, I think you can do this by autowiring to List type. Let's say you have three implementations of an interface BaseService in grails-app/services as:
grails-app/services/exampleapp/BaseService.groovy
package exampleapp
interface BaseService {
}
grails-app/services/exampleapp/OneImplService.groovy
package exampleapp
class OneImplService implements BaseService {
}
grails-app/services/exampleapp/TwoImplService.groovy
package exampleapp
class TwoImplService implements BaseService {
}
Now, you can inject all implementations of BaseSerive as follows:
grails-app/services/exampleapp/TestService.groovy
package exampleapp
import org.springframework.beans.factory.annotation.Autowired
class TestService {
#Autowired
List<BaseService> baseServiceList
void doSomething() {
assert baseServiceList.size() == 2
}
}
Please note that the above example is tested with Grails 5 web application. But, I would be surprised if this does not work in the previous versions of Grails.

Are there any sealed classes alternatives in Dart 2.0?

I have Android development background and I'm learning Flutter.
In Android it's a common practice to use Kotlin sealed classes to return a state from ViewModel e.g.
sealed class MyState {
data class Success(val data: List<MyObject>) : MyState()
data class Error(val error: String) : MyState()
}
I want to use similar pattern in Flutter and return a State object from the BLOC class. What is the best way to achieve the same in Flutter?
Such use case would be done using named factory constructors.
It requires a lot more code, but the behavior is the same.
class MyState {
MyState._();
factory MyState.success(String foo) = MySuccessState;
factory MyState.error(String foo) = MyErrorState;
}
class MyErrorState extends MyState {
MyErrorState(this.msg): super._();
final String msg;
}
class MySuccessState extends MyState {
MySuccessState(this.value): super._();
final String value;
}
RĂ©mi Rousselet's answer is somehow correct but as sindrenm mentioned:
Unfortunately, this isn't the same thing. Kotlin sealed classes guarantee that there are no other implementations of the given class outside of the file they're defined in. That means you can exhaust when statements (switch in Dart) by just providing all possible alternatives as cases, not having to think about potential sub-classes elsewhere
While there is an active discussion about this feature on dart language: Algebraic Data Types, but there is some libraries that can help you implement this behavior. You can use this libraries:
Sealed Unions
Super Enum
Sealed Class
And if you are using BLoC library you can use this lib:
Sealed Flutter Bloc
I hope that dart language add this feature ASAP
Soon Dart is going to support sealed classes.
GitHub Code: source
sealed class Either {}
class Left extends Either {}
class Right extends Either {}
You can now test the sealed class
test(Either either) {
switch (either) {
case Left(): print('Left');
case Right(): print('Right');
}
}
Here is the package for the Sealed Classes/Unions in Flutter
Freezed
This Package provided the features to deal with Data Classes, Sealed Class in Dart/Flutter
Here is the link which explains the beast use of freezed package in Flutter
Use of Freezed Package in Flutter/Dart

What is the purpose of the getter methods in Components in Dagger 2?

I am trying to understand Components in Dagger 2. Here is an example:
#Component(modules = { MyModule.class })
public interface MyComponent {
void inject(InjectionSite injectionSite);
Foo foo();
Bar bar();
}
I understand what the void inject() methods do. But I don't understand what the other Foo foo() getter methods do. What is the purpose of these other methods?
Usage in dependent components
In the context of a hierarchy of dependent components, such as in this example, provision methods such as Foo foo() are for exposing bindings to a dependent component. "Expose" means "make available" or even "publish". Note that the name of the method itself is actually irrelevant. Some programmers choose to name these methods Foo exposeFoo() to make the method name reflect its purpose.
Explanation:
When you write a component in Dagger 2, you group together modules containing #Provides methods. These #Provides methods can be thought of as "bindings" in that they associate an abstraction (e.g., a type) with a concrete way of resolving that type. With that in mind, the Foo foo() methods make the Component able to expose its binding for Foo to dependent components.
Example:
Let's say Foo is an application Singleton and we want to use it as a dependency for instances of DependsOnFoo but inside a component with narrower scope. If we write a naive #Provides method inside one of the modules of MyDependentComponent then we will get a new instance. Instead, we can write this:
#PerFragment
#Component(dependencies = {MyComponent.class }
modules = { MyDependentModule.class })
public class MyDependentComponent {
void inject(MyFragment frag);
}
And the module:
#Module
public class MyDepedentModule {
#Provides
#PerFragment
DependsOnFoo dependsOnFoo(Foo foo) {
return new DependsOnFoo(foo);
}
}
Assume also that the injection site for DependentComponent contains DependsOnFoo:
public class MyFragment extends Fragment {
#Inject DependsOnFoo dependsOnFoo
}
Note that MyDependentComponent only knows about the module MyDependentModule. Through that module, it knows it can provide DependsOnFoo using an instance of Foo, but it doesn't know how to provide Foo by itself. This happens despite MyDependentComponent being a dependent component of MyComponent. The Foo foo() method in MyComponent allows the dependent component MyDependentComponent to use MyComponent's binding for Foo to inject DependsOnFoo. Without this Foo foo() method, the compilation will fail.
Usage to resolve a binding
Let's say we would like to obtain instances of Foo without having to call inject(this). The Foo foo() method inside the component will allow this much the same way you can call getInstance() with Guice's Injector or Castle Windsor's Resolve. The illustration is as below:
public void fooConsumer() {
DaggerMyComponent component = DaggerMyComponent.builder.build();
Foo foo = component.foo();
}
Dagger is a way of wiring up graphs of objects and their dependencies. As an alternative to calling constructors directly, you obtain instances by requesting them from Dagger, or by supplying an object that you'd like to have injected with Dagger-created instances.
Let's make a coffee shop, that depends on a Provider<Coffee> and a CashRegister. Assume that you have those wired up within a module (maybe to LightRoastCoffee and DefaultCashRegister implementations).
public class CoffeeShop {
private final Provider<Coffee> coffeeProvider;
private final CashRegister register;
#Inject
public CoffeeShop(Provider<Coffee> coffeeProvider, CashRegister register) {
this.coffeeProvider = coffeeProvider;
this.register = register;
}
public void serve(Person person) {
cashRegister.takeMoneyFrom(person);
person.accept(coffeeProvider.get());
}
}
Now you need to get an instance of that CoffeeShop, but it only has a two-parameter constructor with its dependencies. So how do you do that? Simple: You tell Dagger to make a factory method available on the Component instance it generates.
#Component(modules = {/* ... */})
public interface CoffeeShopComponent {
CoffeeShop getCoffeeShop();
void inject(CoffeeService serviceToInject); // to be discussed below
}
When you call getCoffeeShop, Dagger creates the Provider<Coffee> to supply LightRoastCoffee, creates the DefaultCashRegister, supplies them to the Coffeeshop constructor, and returns you the result. Congratulations, you are the proud owner of a fully-wired-up coffeeshop.
Now, all of this is an alternative to void injection methods, which take an already-created instance and inject into it:
public class CoffeeService extends SomeFrameworkService {
#Inject CoffeeShop coffeeShop;
#Override public void initialize() {
// Before injection, your coffeeShop field is null.
DaggerCoffeeShopComponent.create().inject(this);
// Dagger inspects CoffeeService at compile time, so at runtime it can reach
// in and set the fields.
}
#Override public void alternativeInitialize() {
// The above is equivalent to this, though:
coffeeShop = DaggerCoffeeShopComponent.create().getCoffeeShop();
}
}
So, there you have it: Two different styles, both of which give you access to fully-injected graphs of objects without listing or caring about exactly which dependencies they need. You can prefer one or the other, or prefer factory methods for the top-level and members injection for Android or Service use-cases, or any other sort of mix and match.
(Note: Beyond their use as entry points into your object graph, no-arg getters known as provision methods are also useful for exposing bindings for component dependencies, as David Rawson describes in the other answer.)

dll initializer method

what I need is to register concrete classes for certain event types in each dll.
public interface IDomainEventHandler<T> where T : IDomainEvent
{
void Handle(T domainEvent);
}
I do not want to scan for the EventHandlers, due to getting same handler registered and executed in inherited classes again and again, but I would like an initializer method in each dll to be called in the structuremap bootsrapper.
so in each dll (module) I would include something like
public void SetEntityEventHandlers()
{
ObjectFactory.Initialize(x =>
{
x.For<IEntityBeforeUpdateEvent<Person>>()
.Add<PersonBizObj>();
x.For<IEntityBeforeUpdateEvent<Person>>()
.Add<EmployeeBizObj>();
//lets say I have a ManagerBizObj which inherits from EmployeeBizObj and I do not want it in the //event handler loop
}
}
so does StructureMap have a convenient way of doing this, adding an assembly (dll) wise initializer function?
I use the Registry class to do this. Each dll has a registry class that initializes the classes that it owns. The StrucureMap bootstrapper adds each registry to the configuration.
http://docs.structuremap.net/RegistryDSL.htm#section1
A.dll contains a FooRegistry class
B.dll contains a BarRegistry class
Each of these registry classes has code that initializes classes that it contains. Something like:
ForRequestedType<IRepository>().TheDefaultIsConcreteType<InMemoryRepository>();
The bootstrapper class has the following code:
ObjectFactory.Initialize(x =>
{
x.AddRegistry<FooRegistry>();
x.AddRegistry<BarRegistry>();
});

Using #Inject with generic type

I've been searching here about it, but haven't found an answer.
In my application, I've an abstract main class for my controllers, with some methods and properties. And I want to inject the DAO automatically.
abstract class AbstractController<E extends AbstractEntity, D extends AbstractDAO<E>> {
#Inject
private D dao;
// getters and setters
}
abstract class AbstractDAO<E extends AbstractEntity> {
#PersistentContext
private EntityManager em;
// finds returns E
}
// implemenation/usage
class CarController extends AbstractController<Car, CarDAO> {
}
Getting the exception:
org.jboss.weld.exceptions.DefinitionException: WELD-001407 Cannot declare an injection point with a type variable: [field] #Inject private AbstractController.dao
Using: Glassfish 3.1 and JSF 2.1.
Is there a workaround or alternative for this?
Thanks.
It's technically very complicated for reflection to detect the proper runtime type by a generic declaration in the source and cast to it. Weld simply don't and won't support it.
Better declare it against AbstractDAO<E>:
private AbstractDAO<E> dao;
You gain nothing with declaring it against D anyway.

Resources