1

I have a service which fetches the data object from the server. There is another class which is a general utility class. It has a static method in which i want to use service instance. but due to method being static this wont work. example code:

export class DataUtil {
    constructor(public core:CoreStructureService){

    }

    static fetchContact(id:string):ContactModel{
        for(let contact of this.core.contactList){
        if(contact.contactId == id)
            return contact
        }
    }

}

This will not compile as the line this.core.contactList in the for stmt is not valid. being method static i cannot refer core as this.core. So how do i fix this.

4
  • Why is fetchContact static? Commented Jan 17, 2018 at 23:45
  • because it is a generic method i need to use frequently. i do not want to instantaniate the class everytime i need to search contact matching the id. Commented Jan 17, 2018 at 23:46
  • This is not a generic method per se. It's a generalized method probably. Commented Jan 17, 2018 at 23:47
  • sure but that i see a fairly common usecase for me Commented Jan 17, 2018 at 23:48

2 Answers 2

1

At the risk of the reputation I'm posting this as an answer.

Do NOT access core service methods from a static method. This is just bad. And this is exactly why the compiler is slapping you.


Have your fetchContact method an instance method instead of static.

export class DataUtil {
    constructor(public core:CoreStructureService){

    }

    fetchContact(id:string):ContactModel{
        for(let contact of this.core.contactList){
        if(contact.contactId == id)
            return contact
        }
    }

}

But... yes, you could work around it in a very dirty, half-working way. I am intentionally not going to describe it.

But, but, but... No, you're trying to do a bad thing.

Sign up to request clarification or add additional context in comments.

6 Comments

how do u gonna do that if the object contactList is in the service ?
@Vik get rid of the static and do inject DataUtil as an instance wherever you need it. Please stop using static classes where we can easily avoid them. It will make everyone's life better. More readable code, more testable code, etc.
here is a problem with that. so if i make it a service instead and start injecting so my another class which create object and need this would need to inject it as well in the constructor. as a result the constructor becomes constructor(sv1:DataUtil, val1, va2) now where ever i create the object of above call that would also require me to pass this injected param. this does not look right
now where ever i create the object of above call that would also require me to pass this injected param. This is totally all right. This actually tells you that you have a dependency you were not thinking well enough. It also says you are trying to construct something by hand which you're not supposed to do if you have a DI container set up
what do u mean by settng up a DI container?
|
0

If DataUtil was a service injected by Angular, it would not be destroyed until the corresponding injector is destroyed. I doubt you will have performance problems because of class instantiation. Try it with a regular service and only come back to this problem, if you really do have performance issues.

For further information, have a look at the Angular docs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.