I have a class in which I need to use a service but this class needs to be instantiated.
It's like this:
class EntrySubject implements ISubject {
constructor(entry: EntryEntity) {}
}
Since I need a service there, I could do this:
class EntrySubject implements ISubject {
constructor(entry: EntryEntity, entryService: EntryService) {}
}
And when using this class inside EntryService I would just instantiate my class as:
const entrySubject = new EntrySubject(entry, this);
But in this case, as far as I understand, every new subject instance would have its own EntryService, but what should I do if I want a single instance of EntryService?
All you have to do is to decorate your EntitySubject, or any other class that you wish to inject, with #Injectable(), and have that class in the 'providers' array of the module. That way, when the constructor has the class in its params, nest will inject a singletone instance of that class.
Please note that when using #Injectable, the default value that is used is #Injectable({scope: DEFAULT}) which means a singletone instance of the class.
For more info on injection scopes visit :
https://docs.nestjs.com/fundamentals/injection-scopes
Related
I am trying to use InheritedWidget approach to share state down the Widget tree and i use three classes one is Bloc class i want to pass as a provider second class is Provider class to pass Bloc class as provider and the third class is implement that provider to share state down the Widget tree but The line with code
context.dependOnInheritedWidgetOfExactType<Provider>() as Provider).bloc
seem to be null for some reason. When looking at samples and doc, it passes the Provider in the widget tree and return it . However, i want to pass as a Bloc class in Provider class but I am getting complaints from flutter tool that it is null. And, in deed it is null when asserted as well.
What is the reasoning here for failed return here? And, how do I need to do it such that I can receive the instance?
import 'package:flutter/material.dart';
import 'package:login_bloc_provider/bloc/bloc.dart';
import 'bloc.dart';
import 'bloc.dart';
//to set variable as a Scoped instance
class Provider extends InheritedWidget{
Provider({super.key, required super.child,});
final bloc=Bloc();
#override
bool updateShouldNotify(oldWidget) {
return true;
}
static Bloc of(BuildContext context){
return(context.dependOnInheritedWidgetOfExactType<Provider>() as Provider).bloc;
}
}
I have a service which needs to instantiate other class every time when method get called.
E.g.
class SomeClass { ... }
#Injectable()
class SomeService {
constructor(...) {}
async doStuff() {
new SomeClass() // new instance every time;
}
}
How can i inject it (SomeClass) properly, and create instance of it every time when method doStuff get called?
If you're needing to instantiate the class everytime for only that method, then what you're already doing is fine. No reason to bring in dependency injection if you're going to be instantiating that class, because Nest's DI system will instantiate it for your otherwise
I need to prevent class A from being instantiated anywhere but only from another class B, then class B can return the created instance of class A which can be used in any other class.
I understand that B could be a Factory in this example, I looked in the factory pattern in the Haxe code cookbook but it does not seem suit what I am looking for.
In my example class B is doing some work then should return the result in an instance of class A.
no one should be able to create an instance of class A because it is the result of the work that class B performs. anyone needs an instance of A should ask B to do the work and return the resulted A instance
hope I explained it clearly
You would usually do this by using #:allow() metadata in combination with a private constructor:
A.hx:
class A {
#:allow(B)
private function new() {}
}
B.hx:
class B {
public static function create():A {
return new A(); // compiles
}
}
Trying to instantiate A outside of B will result in a compiler error:
class Main {
static function main() {
new A(); // Cannot access private constructor of A
}
}
Note that it's still possible to work around this by using #:access() or #:privateAccess metadata - in Haxe, nothing is ever truly private. It follows a philosophy of "the programmer knows best", which can be very powerful.
Also, you might want to declare A as #:final so nothing can subclass it, because subclasses can access private fields in Haxe. But again, this can be overriden with #:hack metadata.
is there a way to get the name of a child class inside of a super class?
class ChildClass inherits SuperClass { }
class SuperClass {
notify { "$NAME_OF_CHILD_CLASS is inheriting me": }
}
The expected output would be ChildClass is inheriting me. I'd need something similar to $module_name which contains the name of the class that contains the current resource's definition.
Is there a way to achieve this? I don't mind if I need a custom function for that...
Consider the following class:
class MyContext : DbContext
{
public DbSet<Order> Orders { get; set; }
}
and instantiating a new object:
var mycontext = new MyContext();
Why mycontext.Orders is not null? When it was initialized? Who has initialized it? I'm really confused because the base class (DbConetxt) cannot access the derived class properties so it is not possible that the automatic property was initialized in the base object.
From looking at the reflected code, when the DbContext (the base class) is constructed it makes a call to the DbSetDiscoveryService (an internal clasS) - which essentially uses reflection to find all properties on the DbContext, and then initializes those that need initializing.
So in short - using reflection in the constructor.