locked
EntityFramework Class Instance Or Static Methods RRS feed

  • Question

  • User-1428409812 posted

    I have been thinking about this for a while, and have searched on this topic, but have not found a decent answer. So basically I am working on this framework and we have plans to make it both desktop and web. At the moment we use static methods for most methods, but is this good practice or should we use class instance?

    The reason why most methods are static is because it would be cumbersome making a new instance every time you needed to access a method. And most classes don't really require any constructor arguments so that's why we went with static methods.

    Can someone shed some light on this? When should we use static methods instead of class instance and vice versa.

    URL.Add(Url);//this
    
    //or this..?
    URL url = new URL()
    url.Add(urlentity);

    Monday, August 3, 2015 12:47 PM

Answers

  • User281315223 posted

    Is there really an advantage then or does it depending on what you're trying to do/achieve? 

    Well there can be advantages. 

    Since static classes only have a single instance, it can make things like accessing them much easier (assuming you have a reference to the namespace they are declared in) and allow you to define them at a high level such that they can be accessible throughout most of your application (if you are using them a great deal). Another benefit would be in the area of resource allocation since you can only ever have a single instance of the class, you don't have to worry about generating tons of individual instances as you should never need more than one.

    It really all is situational though. As mentioned earlier, I usually use static classes for things like generators (i.e. I need something to create a class for me or utility methods like String formatters, etc.). I'd highly recommend reading over some of the discussions that I provided previously for some additional recommendations / suggestions.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 3, 2015 3:58 PM

All replies

  • User281315223 posted

    I generally use static classes for things like utilities (e.g. generators, utilities, etc) where as non-static classes are actually going to allow you to create instances of objects and use those instances. One thing to remember about static classes is that you cannot create instances of them (i.e. you couldn't create a car object). A static class always only has a single instance that exists at any given time.

    In the examples that you provided, the first example doesn't seem to make much sense off the bat, but I suppose it ultimately depends on usage :

    // This syntax would really only make much sense if it returned something as it isn't very
    // intuitive about what is occur (e.g. what is the new Url object actually being added to? A collection?
    URL.Add(Url);
    
    // This seems to intuitive; You are creating a Url object and loading a Url entity into it (perhaps it can store
    // a collection of Url objects and then those will be passed on into something
    URL url = new URL()
    url.Add(urlentity);

    There are numerous great discussions out there on this very topic that I'll provide you with as they'll go into far more depth than I could :

    Monday, August 3, 2015 12:51 PM
  • User-1428409812 posted

    I generally use static classes for things like utilities (e.g. generators, utilities, etc) where as non-static classes are actually going to allow you to create instances of objects and use those instances. One thing to remember about static classes is that you cannot create instances of them (i.e. you couldn't create a car object). A static class always only has a single instance that exists at any given time.

    In the examples that you provided, the first example doesn't seem to make much sense off the bat, but I suppose it ultimately depends on usage :

    // This syntax would really only make much sense if it returned something as it isn't very
    // intuitive about what is occur (e.g. what is the new Url object actually being added to? A collection?
    URL.Add(Url);
    
    // This seems to intuitive; You are creating a Url object and loading a Url entity into it (perhaps it can store
    // a collection of Url objects and then those will be passed on into something
    URL url = new URL()
    url.Add(urlentity);

    There are numerous great discussions out there on this very topic that I'll provide you with as they'll go into far more depth than I could :

    In the above examples both methods would return true and save the entity into the database sorry it was bad example, I just wrote it randomly... Most classes we've got are not static but the methods are. Is there really an advantage then or does it depending on what you're trying to do/achieve? 

    Monday, August 3, 2015 2:10 PM
  • User281315223 posted

    Is there really an advantage then or does it depending on what you're trying to do/achieve? 

    Well there can be advantages. 

    Since static classes only have a single instance, it can make things like accessing them much easier (assuming you have a reference to the namespace they are declared in) and allow you to define them at a high level such that they can be accessible throughout most of your application (if you are using them a great deal). Another benefit would be in the area of resource allocation since you can only ever have a single instance of the class, you don't have to worry about generating tons of individual instances as you should never need more than one.

    It really all is situational though. As mentioned earlier, I usually use static classes for things like generators (i.e. I need something to create a class for me or utility methods like String formatters, etc.). I'd highly recommend reading over some of the discussions that I provided previously for some additional recommendations / suggestions.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 3, 2015 3:58 PM
  • User1368374114 posted

    I prefer to use the static classes where I have defined functionality of the function and if some data are passed in it it will return the calculated or the formatted value. In my application must of the static classes have a static functions like getting a connection string from web.config, formatting a string   and getting data from the database. I prefer those classes to be static because I don't have to create a instance for that . But remember the method you create inside the static class should be static too.

    Thursday, August 20, 2015 10:28 AM