.NET how to prevent calling a method returning a string multiple times

Stijn Wingens

I'm trying to build a string of a SQL query upon initialization of a Repository class.

The repository resembles this:

public class SomeRepository<T>
{
    private readonly string _sql;

    public SomeRepository() 
    {
        _sql = GetSql();
    }

    private string GetSql()
    {
        // Build SQL string based on type T.
    }

    public IEnumerable<T> Read()
    {
        // Get data with the sql string
    }
}

Every time I call Read(), the _sql string is rebuilt, even though it will never change. How can I prevent this?

I have tried making it a constant but then it must be declared in a static way which would not work with the generic typing of T, which is dependant on the instance of the repository.

The repository is injected with a transient scope.

Peter Dongan

By definition a readonly field can only be set in the constructor so it is certain that a new instance of the repository is being created every time you see the value being set.

I am reasonably sure that you need to change its lifetime in your DI configuration. You said you are injecting it with Transient scope. That means that every repository object you pass will be different. It seems very likely that what is happening here is that you have a repository injected and used in more than one place. It is also possible that you making multiple requests and expecting it to be initialized once, in which case you would want to use Singleton for its scope.

Here is a very clear and concise description of the differences:

Transient objects are always different; a new instance is provided to every controller and every service.

Scoped objects are the same within a request, but different across different requests.

Singleton objects are the same for every object and every request.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to prevent calling a function multiple times in Jquery

From Dev

prevent calling ajax multiple times

From Dev

Calling an async method multiple times

From Dev

How to stop multiple times method calling of didUpdateLocations() in ios

From Dev

How to use the Scanner in a separate method without calling it multiple times

From Dev

How to prevent grep from printing the same string multiple times?

From Dev

Prevent a method to be called multiple times in React JS

From Dev

ViewPager InstatiateItem method is calling multiple times

From Dev

Reflected Method calling constructor multiple times

From Dev

Calling same method multiple times and show progress

From Dev

Multiple times method calling from angular template

From Dev

Calling a function returning a String within the main method

From Dev

Calling method isn't returning string

From Dev

How to prevent AttributeError for undeclared variables and methods and fix __getattr__ method call multiple times?

From Dev

Why Angular 2+ innerHTML is calling method multiple times in a single statement, How to solve this

From Dev

How i prevent calling nested then method?

From Dev

How to avoid Observable calling multiple times RxDart

From Dev

How do I prevent "maxing out" of CPU: Synchronous method calling multiple workers asynchronously & throttling using SemaphoreSlim?

From Dev

How to prevent multiple fetch calling in RxJS?

From Dev

How to format a String multiple times?

From Dev

Swift: NavigationLink calling destination's init method multiple times

From Dev

Async Await with method calling external resource or returning local string

From Dev

NSNotificationCenter is calling multiple times

From Dev

Returning a String Endless Times

From Dev

String interpolation with R's glue on a vector, without calling it multiple times

From Dev

Angular Promise - $q.all not returning result and calling Post method several times not working

From Dev

How to mock a method that is called multiple times in single call using Moq in C#.NET?

From Dev

How to verify a method which was called multiple times

From Dev

How can a Groovy method return multiple times?

Related Related

  1. 1

    how to prevent calling a function multiple times in Jquery

  2. 2

    prevent calling ajax multiple times

  3. 3

    Calling an async method multiple times

  4. 4

    How to stop multiple times method calling of didUpdateLocations() in ios

  5. 5

    How to use the Scanner in a separate method without calling it multiple times

  6. 6

    How to prevent grep from printing the same string multiple times?

  7. 7

    Prevent a method to be called multiple times in React JS

  8. 8

    ViewPager InstatiateItem method is calling multiple times

  9. 9

    Reflected Method calling constructor multiple times

  10. 10

    Calling same method multiple times and show progress

  11. 11

    Multiple times method calling from angular template

  12. 12

    Calling a function returning a String within the main method

  13. 13

    Calling method isn't returning string

  14. 14

    How to prevent AttributeError for undeclared variables and methods and fix __getattr__ method call multiple times?

  15. 15

    Why Angular 2+ innerHTML is calling method multiple times in a single statement, How to solve this

  16. 16

    How i prevent calling nested then method?

  17. 17

    How to avoid Observable calling multiple times RxDart

  18. 18

    How do I prevent "maxing out" of CPU: Synchronous method calling multiple workers asynchronously & throttling using SemaphoreSlim?

  19. 19

    How to prevent multiple fetch calling in RxJS?

  20. 20

    How to format a String multiple times?

  21. 21

    Swift: NavigationLink calling destination's init method multiple times

  22. 22

    Async Await with method calling external resource or returning local string

  23. 23

    NSNotificationCenter is calling multiple times

  24. 24

    Returning a String Endless Times

  25. 25

    String interpolation with R's glue on a vector, without calling it multiple times

  26. 26

    Angular Promise - $q.all not returning result and calling Post method several times not working

  27. 27

    How to mock a method that is called multiple times in single call using Moq in C#.NET?

  28. 28

    How to verify a method which was called multiple times

  29. 29

    How can a Groovy method return multiple times?

HotTag

Archive