How to create a factory class that prevents direct instantiation of a class ?
say, i have class Comment that should only be instantiated from a class CommentFactory. I don't want Comment to be able to be instantiated directly.
I figured I could do this :
public partial class Comment
{
private Comment(){}
public Comment CreateComment(User user,string str)
{
Comment cmt=new Comment();
user.AddComment(cmt);
cmt.User=user;
return cmt;
}
}
is this code clean ?
now, the problem i'm facing is that the Comment is a partial class with partial implementation done by Linq to SQL. and Linq to SQL creates a public constructor of this class, so how can i circumvent this ? is there any 'trick' ?