logo
down
shadow

Is it a good practice to have setter in the get instance property of a singleton class


Is it a good practice to have setter in the get instance property of a singleton class

By : rakibi rakib
Date : October 25 2020, 07:10 PM
wish helps you TL;DR:
If you need to release unmanaged resources - the scenario where we would use IDisposable, then implement IDisposable and don't make the class a singleton.
code :
services.AddSingleton<ThingThatIOnlyWantOneOf>();


Share : facebook icon twitter icon
C++ Singleton class - inheritance good practice

C++ Singleton class - inheritance good practice


By : Lasrevin
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I'm not sure I understand the situation you're dealing with fully, and whether or not it's possible or appropriate to derive from the singleton depends very much on how the singleton is implemented.
But since you mentioned "good practice" there's some general points that come to mind when reading the question:
Using getter / setter inside a class - good or bad practice?

Using getter / setter inside a class - good or bad practice?


By : Cholponbek Esenbek U
Date : March 29 2020, 07:55 AM
this will help It is more common to access the field directly. The value of a setFieldName method is more obvious for programmers using your code in other classes. With the implementation details hidden, they might not realize what ranges of values are acceptable, so keeping fields private and forcing other developers to go through a setter makes sense. But inside your own class, the case for using a setter is much weaker. If you look at the source for the java API you'll find that getter / setter methods are generally not used within a class.
Property to be assigned once in a singleton - good practice?

Property to be assigned once in a singleton - good practice?


By : user2178805
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The problem with passing the value into getInstance() every time is that all calling classes will have to know where the value for Name comes from and how to fetch it. Maybe they do all have this access, but then it makes it redundant storing this data on the singleton object as all callers already know its value.
Assuming that some callers know the value, and others don't, you could use a property similar to the way that you have suggested yourself:
code :
public class MySingleton
{
    // Singleton properties omitted

    private string name;

    public string name
    {
        get{return this.name;}
        set
        {
            if(String.IsNullOrEmpty(this.name))
                name = value;
            // The exception could be left out, depending on how critical this is
            else
                throw new exception("The property 'name' can only be set once");
        }
    }
}
public MySingleton()
{
     name = configuration.getName(); // or wherever it is coming from
}
Is it a good practice for derive class to be singleton

Is it a good practice for derive class to be singleton


By : Nicks
Date : March 29 2020, 07:55 AM
like below fixes the issue The answer is: it depends.
Keep in mind that inheritance is primarily not about avoiding code duplication. It is meant as tool. You say "A extends B" when A is-A B. And that of course means that "rules" such as the Liskov Substitution Principle should be followed.
code :
Base baseA = ... nonsingelton subclass of Base
Base baseA2 = ... nonsingelton subclass of Base
Base baseB = singleton instance of Derived
baseA(baseA2) maybe works
baseA(baseB) 
Is it possible to inherit a singleton class with a base class (non-singleton class). If yes, is it a good practice?

Is it possible to inherit a singleton class with a base class (non-singleton class). If yes, is it a good practice?


By : Feek
Date : March 29 2020, 07:55 AM
help you fix your problem I am actually trying to implement something where a singleton class would extend a non-singleton class. ,
1) Is this possible in java?
code :
public enum SomeSingleton { 
  INSTANCE;
  ...
List<Parent> someInstances = Arrays.asList(parentInstanceA, parentInstanceB, thatSingletonInstanceOfMySingleton);
Related Posts Related Posts :
  • Moving picture box fast causes flickering C#
  • The remote server returned an error: (407) Proxy Authentication Required
  • XNA and Ninject: Syntax for dependency arguments?
  • How do I use C# and ADO.NET to query an Oracle table with a spatial column of type SDO_GEOMETRY?
  • Using C# to iterate form fields with same name
  • C# regex to validate "realistic" IP values
  • C# background worker and timer loop
  • How do I backup and restore the system clipboard in C#?
  • Colon ':' error - Not all named parameters have been set in Nhibernate?
  • Decoupling the view, presentation and ASP.NET Web Forms
  • Passing variables from a thread to another form using C#
  • How to get target path for UserName/My Pictures folder?
  • Blob Storage trigger is not getting fired upon uploading an image to the container, why?
  • How to solve encoding problem reading feed
  • how do you set Parallelizable Attribute in dotnet core for Nunit tests?
  • error MSB4226 MSBuild.Community.Tasks.Targets" was not found
  • Question about how to use strong typed dataset in N-tier application for .NET
  • MouseEnter and MouseLeave events from a Panel and its child controls
  • Moving a member of a List to the Front of the List
  • use inheritance to create a custom, type safe, null safe collection?
  • Problem saving excel file after inserting data
  • C# Setting Properties using Index
  • Placing & deleting element(s) from a object (stack)
  • Generic Singleton Façade design pattern
  • Having an outline for MouseOver for a WPF ListView
  • How to create resource manager in ASP.NET
  • Setting an XAML Window always on top (but no TopMost property)
  • Correctly use dependency injection
  • Managed code (C#) vs Matlabs and C++ for speed
  • Setting Cursor property has no effect
  • .net Attributes that handle exceptions - usage on a property accessor
  • Controlling images of nodes in a TreeView
  • How to return Task<IEnumerable<T>>?
  • Create a overlay screen while a game/program is running?
  • How to deal with JavaScript when trying to fetch web page in C#.NET/WPF?
  • how to read screen resolution - and change this resolution?
  • Converting C# void* to byte[]
  • C# Textbox validation should only accept integer values, but allows letters as well
  • format ugly c# source code
  • Why RSA encryption can return different results with C# and Java?
  • ASP.NET MVC 2 Localization/Globalization stored in the database?
  • LoaderLock was detected, and turning off the warning does not work
  • Problem with usercontrol scaling when added at run-time in .Net, WinForms
  • Good Silverlight Custom ItemsControl Tutorial
  • C# tabcontrol border controls
  • save images in webbrowser control without redownloading them from the internet
  • How can i restart MSDTC service on remote server from client machine
  • Fields of class, are they stored in the stack or heap?
  • setup format for columns of DataTable or GridView
  • c# - wmplib - playing 2 mp3 files but can get them in synic
  • How to delete a file from a SFTP server programmatically using SharpSSH?
  • .NET HttpListener Prefix issue with anything other than localhost
  • .NET: Type.Parse not working?
  • Inheritence in C# question - is overriding internal methods possible?
  • Prevent command window showing when NAnt compiling windows forms application
  • Need to pinvoke a .dll or something to change the mouse cursor in WPF
  • Store more than 24 hours in a DateTime
  • Custom code access permissions
  • How to convert SAML XML token string to either SecurityToken or ClaimsPrincipal instance?
  • DropDownList problem/doubt
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk