I look at an article by Martin Fowler titled " Role-Based Work . " In it, Fowler discloses three basic strategies for working with roles for a Person in an organization (for example, Employee, Engineer, Manager, Salesman), which include role subtyping, object role, and role relationships.
Being a “working draft”, it was written in 1997 and, of course, was old, it also has some errors that would otherwise not have been there. I am confused by going over to the example of the Role object through which it passes, and has included my C # interpretation of some of its java code below.
I have three questions:
(1) there is a lot of type identification with strings that seem to be replaced by generics, but I can't figure out how to do this. How to implement this code using generics?
(2) The JobRole is in the code as the string name for the type, but it is not defined specifically with the rest of the code. I can’t say whether this is the base class for PersonRole or not. What is the definition of JobRole? Is unit test similar to a valid example of using a template?
(3) Does anyone have links to a later implementation and an example of using a Role object?
Cheers,
Berryl
public class PersonWithRoles : Person
{
private readonly IList<PersonRole> _roles = new List<PersonRole>();
public static PersonWithRoles CreatePersonWithRoles(string identifierName) {
...
}
public void AddRole(PersonRole role) { _roles.Add(role); }
public PersonRole RoleOf(string typeName) { return _roles.FirstOrDefault(x => x.HasType(typeName)); }
}
public class PersonRole
{
public virtual bool HasType(string typeName) { return false; }
}
public class Salesman : PersonRole
{
public override bool HasType(string typeName)
{
if (typeName.Equals("Salesman", StringComparison.InvariantCultureIgnoreCase)) return true;
if (typeName.Equals("JobRole", StringComparison.InvariantCultureIgnoreCase)) return true;
return base.HasType(typeName);
}
public int NumberOfSales { get; set; }
}
[TestFixture]
public class RoleUsageTests
{
[Test]
public void Test() {
var p = PersonWithRoles.CreatePersonWithRoles("Ted");
var s = new Salesman();
p.AddRole(s);
var tedSales = (Salesman) p.RoleOf("Salesman");
tedSales.NumberOfSales = 50;
}
}
source
share