Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have simple problem.
There is one class or dll named Employee.
Now all the other class or dlls should not create instance of employee class.
But they can use employee class without creating instance.
OR
I want to create a method in Employee class which will be used to pass the instance of employee class.
Is there any standard way to do this? OR What is the best way to do it?

Cannot use static class it will prevent any instance. But I want to create instance.

If you didn't understand. Please comment below I will happy to provide additional details.

Thanks.
Posted
Updated 4-Dec-14 7:01am
v6
Comments
ZurdoDev 4-Dec-14 12:48pm    
Sounds like you just want a static class.
sushil_gupta 4-Dec-14 12:50pm    
I want to create instance. If I create static class there won't be any instance.
PIEBALDconsult 4-Dec-14 12:52pm    
Unclear. There is likely no reason to enforce the "should not create instance of employee class" at all, just don't do it.
Or, is it a matter of maybe Employee should be abstract? Or have a factory method?
sushil_gupta 4-Dec-14 12:54pm    
I meant forcing user not to create instance outside of class. Inside or in same dll we can do it.
PIEBALDconsult 4-Dec-14 13:02pm    
Yes, but do you absolutely positively have to force it?
Consider making the constructor internal rather than public.

So,

There is design pattern called Factory method. You can use this in combination with private contructor. This way you can create objects on your conditions. Block some fields for editing etc...

Here you have some naive implementation which can give you overview of this pattern:
C#
public enum EmployeeType
{
    Regular,
    Manager,
}

public class Employee
{
    #region Public members
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Employee Manager { get; private set; }

    public EmployeeType EmployeeType
    {
        get;
        private set;
    }
    #endregion

    #region Constructors
    /// <summary>
    /// Private constructor prevents to create instance of this class from outside the class
    /// </summary>
    private Employee(EmployeeType employeeType, Employee manager = null)
    {
        EmployeeType = employeeType;
        Manager = manager;
    }
    #endregion

    #region Factory methods
    /// <summary>
    /// Factory method to create Regular employe
    /// </summary>
    /// <param name="manager"></param>
    /// <returns></returns>
    public static Employee CreateRegularEmployee(Employee manager)
    {
        return new Employee(EmployeeType.Regular, manager);
    }

    /// <summary>
    /// Factory method to create Manager
    /// </summary>
    /// <returns></returns>
    public static Employee CreateManager()
    {
        return new Employee(EmployeeType.Manager);
    }
    #endregion
}


Only way to create instance of Employee class is to use its static methods:

C#
var manager = Employee.CreateManager();
manager.FirstName = "Jonh";
manager.LastName = "Smith";

var regular = Employee.CreateRegularEmployee(manager);
regular.FirstName = "Adam";
regular.LastName = "Hyde";


If you try to create instance of Employee class in normal way:
C#
var regular2 = new Employee();

it won't compile and you will get an error:
Error	1	'FactoryMethod.Employee.Employee(FactoryMethod.EmployeeType, FactoryMethod.Employee)' is inaccessible due to its protection level	F:\Projekty\net\WinForms\FactoryMethod\FactoryMethod\Program.cs	21	28	FactoryMethod


There are articles on CP:
Implementing Factory Method in C#[^]
Factory Method Pattern[^]

[Update]
I've just tested another solution. You an place this class in separate DLL and change constructor's protection level from Private to Internal (you will have public class and internal constructor). Then you can create instance of Employee class from inside of DLL but not from outside. You can still use Factory Method pattern to instantiate Employee class or you can use Factory pattern which is similar to Factory Method by you need to create another class to create Employee class i.e. EmployeeFactory like this:

C#
public static class EmployeeFactory
{
    /// <summary>
    /// Factory method to create Regular employe
    /// </summary>
    /// <param name="manager"></param>
    /// <returns></returns>
    public static Employee CreateRegularEmployee(Employee manager)
    {
        return new Employee(EmployeeType.Regular, manager);
    }

    /// <summary>
    /// Factory method to create Manager
    /// </summary>
    /// <returns></returns>
    public static Employee CreateManager()
    {
        return new Employee(EmployeeType.Manager);
    }
}


And use it like this:
C#
var manager = EmployeeFactory.CreateManager();
manager.FirstName = "Jonh";
manager.LastName = "Smith";

var regular = EmployeeFactory.CreateRegularEmployee(manager);
regular.FirstName = "Adam";
regular.LastName = "Hyde";


If you try to create instance of Employee class in normal way:
C#
var employee = new Employee();


It won't compile and you will get an error:
Error	1	The type 'FactoryMethod.Employee' has no constructors defined	F:\Projekty\net\WinForms\FactoryMethod\FactoryMethod\Program.cs	21	28	FactoryMethod


I hope it help you.
 
Share this answer
 
v3
Comments
sushil_gupta 4-Dec-14 15:50pm    
Yeah, you are right. The solution you have updated ( Internal construtor and Public class will do the funtionality I am looking for. I figured it out and tested it. simultaneously while reading answers. But Now you have written solution before me so you deserve to be choosen best.
Marcin Kozub 4-Dec-14 15:55pm    
I'm glad I could help. But big thanks should be directed to PIEBALDConsult for mentioning about Factory Method in comment (which I didn't noticed before posting my solution).
BillWoodruff 5-Dec-14 1:20am    
+ Excellent.
Marcin Kozub 5-Dec-14 1:56am    
Thx Bill! :)
 
Share this answer
 
Comments
sushil_gupta 4-Dec-14 13:00pm    
Static class we cannot create instance. But I want instance to be created. Not outside the class but inside the dll.
Member 10711013 4-Dec-14 13:07pm    
understand . . .
 
Share this answer
 
Comments
sushil_gupta 4-Dec-14 14:17pm    
I checked your solution. But it is Singleton. Only one instance. I want multiple instances.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900