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:
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
private Employee(EmployeeType employeeType, Employee manager = null)
{
EmployeeType = employeeType;
Manager = manager;
}
#endregion
#region Factory methods
public static Employee CreateRegularEmployee(Employee manager)
{
return new Employee(EmployeeType.Regular, manager);
}
public static Employee CreateManager()
{
return new Employee(EmployeeType.Manager);
}
#endregion
}
Only way to create instance of Employee class is to use its static methods:
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:
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:
public static class EmployeeFactory
{
public static Employee CreateRegularEmployee(Employee manager)
{
return new Employee(EmployeeType.Regular, manager);
}
public static Employee CreateManager()
{
return new Employee(EmployeeType.Manager);
}
}
And use it like this:
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:
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.