Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory pattern in C#: How to ensure an object instance can only be created by a factory class?

Recently I've been thinking about securing some of my code. I'm curious how one could make sure an object can never be created directly, but only via some method of a factory class. Let us say I have some "business object" class and I want to make sure any instance of this class will have a valid internal state. In order to achieve this I will need to perform some check before creating an object, probably in its constructor. This is all okay until I decide I want to make this check be a part of the business logic. So, how can I arrange for a business object to be creatable only through some method in my business logic class but never directly? The first natural desire to use a good old "friend" keyword of C++ will fall short with C#. So we need other options...

Let's try some example:

public MyBusinessObjectClass {     public string MyProperty { get; private set; }      public MyBusinessObjectClass (string myProperty)     {         MyProperty = myProperty;     } }  public MyBusinessLogicClass {     public MyBusinessObjectClass CreateBusinessObject (string myProperty)     {         // Perform some check on myProperty          if (true /* check is okay */)             return new MyBusinessObjectClass (myProperty);          return null;     } } 

It's all okay until you remember you can still create MyBusinessObjectClass instance directly, without checking the input. I would like to exclude that technical possibility altogether.

So, what does the community think about this?

like image 221
User Avatar asked Feb 05 '09 10:02

User


People also ask

What is factory pattern with example?

Factory pattern removes the instantiation of actual implementation classes from client code. Factory pattern makes our code more robust, less coupled and easy to extend. For example, we can easily change PC class implementation because client program is unaware of this.

What is meant by factory pattern?

A Factory Pattern or Factory Method Pattern says that just define an. interface or abstract class for creating an object but let the. subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.

What is a factory class in C++?

A factory method is a static method of a class that returns an object of that class' type. But unlike a constructor, the actual object it returns might be an instance of a subclass. Another advantage of a factory method is that it can return existing instances multiple times.

What is design pattern and factory pattern?

This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.


2 Answers

You can make the constructor private, and the factory a nested type:

public class BusinessObject {     private BusinessObject(string property)     {     }      public class Factory     {         public static BusinessObject CreateBusinessObject(string property)         {             return new BusinessObject(property);         }     } } 

This works because nested types have access to the private members of their enclosing types. I know it's a bit restrictive, but hopefully it'll help...

like image 76
Jon Skeet Avatar answered Sep 21 '22 23:09

Jon Skeet


Looks like you just want to run some business logic before creating the object - so why dont you just create a static method inside the "BusinessClass" that does all the dirty "myProperty" checking work, and make the constructor private?

public BusinessClass {     public string MyProperty { get; private set; }      private BusinessClass()     {     }      private BusinessClass(string myProperty)     {         MyProperty = myProperty;     }      public static BusinessClass CreateObject(string myProperty)     {         // Perform some check on myProperty          if (/* all ok */)             return new BusinessClass(myProperty);          return null;     } } 

Calling it would be pretty straightforward:

BusinessClass objBusiness = BusinessClass.CreateObject(someProperty); 
like image 24
Ricardo Nolde Avatar answered Sep 21 '22 23:09

Ricardo Nolde