Search code examples
c#interfacefactory-pattern

Roles, Abstract Pattern, Loose Coupling


Let's imagine we got the following:

A) Factory interface such as

public interface IEmployeeFactory
{        
    IEmployee CreateEmployee(Person person, Constants.EmployeeType type, DateTime hiredate);
}

B) Concrete factory such as

public sealed class EmployeeFactory : Interfaces.IEmployeeFactory
{        
    public Interfaces.IEmployee CreateEmployee(Person person, Constants.EmployeeType type, DateTime hiredate)
    {
        switch(type)
        {
        case BusinessObjects.Common.Constants.EmployeeType.MANAGER:
            {
                return new Concrete.Manager(person, hiredate);
            }
        case BusinessObjects.Common.Constants.EmployeeType.SALES:
            {
                return new Concrete.Sales(person, hiredate);
            }
        default:
            {
                throw new ArgumentException("Invalid employee type");
            }
        }
    }
}

C) Employees family: Manager and Sales inherited from an abstract Employee.

More on my simple) architecture

Some client code

public sealed class EmployeeFactoryClient
{
    private Interfaces.IEmployeeFactory factory;
    private IDictionary<String, Interfaces.IEmployee> employees;

    public Interfaces.IEmployee this[String key]
    {
        get { return this.employees[key]; }
        set { this.employees[key] = value; }
    }
    public EmployeeFactoryClient(Interfaces.IEmployeeFactory factory)
    {
        this.factory   = factory;
        this.employees = new Dictionary<String, Interfaces.IEmployee>();
    }
    public void AddEmployee(Person person, Constants.EmployeeType type, DateTime hiredate, String key)
    {            
        this.employees.Add(
            new KeyValuePair<String, Interfaces.IEmployee>(
                key,
                this.factory.CreateEmployee(
                    person,
                    type,
                    hiredate
                )
            )
        );            
    }
    public void RemoveEmployee(String key)
    {
        this.employees.Remove(key);
    }
    public void ListAll()
    {
        foreach(var item in this.employees)
        {
            Console.WriteLine(
                "Employee ID: " + item.Key.ToString() + 
                "; Details: "   + item.Value.ToString()
            );
        }
    }
}
class ApplicationShell
{
    public static void Main()
    {
        var client = new EmployeeFactoryClient(new EmployeeFactory());
        client.AddEmployee(
            new Person {
                FirstName = "Walter",
                LastName  = "Bishop",
                Gender    = BusinessObjects.Common.Constants.SexType.MALE,
                Birthday  = new DateTime()
            },
            BusinessObjects.Common.Constants.EmployeeType.MANAGER,
            new DateTime(),
            "A0001M"
        );
        Console.WriteLine(client["A0001M"].ToString());
        Console.ReadLine();
    }
}

I wanted to ask about some business logic architecture details. Using the architecture I can't design what the Company class should be, don't know how do I integrate it.

I have read that it more appropriate to use Role-based architecture in such cases. Could you provide an example (Company, Employees entities)?


Thanks!


Solution

  • Are you sure you included the override keyword in the ToString() method?