Hi everyone:
I am developing a library that will provide persistence methods for business objects.
The Persistence class is generic because it needs to know about the type of object it persists. (The type parameter to the class is the type of object to be persisted.)
However, there is no need for the clients of the library to instantiate an instance of the Persistence class each time an object needs to be loaded or saved. So, I want to make the Persistence class a Singleton. (I can't make it a static class, because the class does keep state, namely info about the type to be persisted)
However, when providing the static public read-only accessor to get to the only instance object, the CA1000 error occurs.
I am not sure now if my design is actually flawed, or if this rule should be disregarded in the case of a generic Singleton.
Thanks for any help,
SA.

CA1000 and Generic Singleton classes
anasilk
David:
Thanks for your reply. I understand why the rule is fired.
I am not sure how to work around this in my scenario though Can you provide any pointers
Thanks,
SA.
AnnyJacky
Jeffrey:
Thank you for your reply.
I will have to ignore the rule in this case, because this project is a library, which can take any arbitrary object (that inherits from a specific base object) and persist it.
SA.
double eagle
Hello Jeffrey
I'm having the same problem here, only with the difference that the generic class in this case is abstract, and the type param is used for returning the instance of the created type.
For example:
public abstract class X<T> where T : X
{
public static T Instance { get; }
}
and the inherited class:
public class Y : X<Y>
{
public SomeMethod() {}
}
It looks a little weird, but it works :) Anywhere in the code you can now call Y.Instance.SomeMethod() (but SomeMethod is not in X) without having to write the singleton pattern again... Also in this case a type param is not needed when using the static Instance property... Let me know what you think about this please.
Best regards,
Jeroen.
cornholios40
The following code is the assumption I've made about your class:
public class Persistance<T>
{
public static Persistance<T> Instance
{
get { [..] }
}
}
As stated in the documentation for this rule; the above class does not allow type inferencing, that is, the type parameter always needs to be specified and cannot be automatically determined by the compiler, ie
Console.WriteLine(Persistance<String>.Instance);
This makes it harder for new users to using the class as they don't understand specifying the String type in the above code sample. This is the same reason behind the GenericMethodsShouldProvideTypeParameter rule.
Regards
David
t.g.
Hi Dave,
one solution, which might or might not work in your case, is to create instances per type parameter..
e.g.
public class Persistence<T>
{
public static Persistence<String> StringInstance
{
get { ...}
}
}
Another option is to ignore this rule. The FxCop rule is there to warn you that not everyone will understand the generic pattern. But if, you know that the targeted developers for your library are experienced enough to understand the pattern, you can safely ignore it.
Regards,
Jeffrey