I'm trying to access a method that is marked as internal in the parent class (in its own assembly) in an object that inherits from the same parent.
Let me explain what I'm trying to do...
I want to create Service classes that return IEnumberable with an underlying List to non-Service classes (e.g. the UI) and optionally return an IEnumerable with an underlying IQueryable to other services.
I wrote some sample code to demonstrate what I'm trying to accomplish, shown below. The example is not real life, so please remember that when commenting.
All services would inherit from something like this (only relevant code shown):
public class ServiceBase<T>
{
protected readonly ObjectContext _context;
protected string _setName = String.Empty;
public ServiceBase(ObjectContext context)
{
_context = context;
}
public IEnumerable<T> GetAll()
{
return GetAll(false);
}
//These are not the correct access modifiers.. I want something
//that is accessible to children classes AND between descendant classes
internal protected IEnumerable<T> GetAll(bool returnQueryable)
{
var query = _context.CreateQuery<T>(GetSetName());
if(returnQueryable)
{
return query;
}
else
{
return query.ToList();
}
}
private string GetSetName()
{
//Some code...
return _setName;
}
}
Inherited services would look like this:
public class EmployeeService : ServiceBase<Employees>
{
public EmployeeService(ObjectContext context)
: base(context)
{
}
}
public class DepartmentService : ServiceBase<Departments>
{
private readonly EmployeeService _employeeService;
public DepartmentService(ObjectContext context, EmployeeService employeeService) : base(context)
{
_employeeService = employeeService;
}
public IList<Departments> DoSomethingWithEmployees(string lastName)
{
//won't work because method with this signature is not visible to this class
var emps = _employeeService.GetAll(true);
//more code...
}
}
Because the parent class lives is reusable, it would live in a different assembly than the child services. With GetAll(bool returnQueryable) being marked internal, the children would not be able to see each other's GetAll(bool) method, just the public GetAll() method.
I know that I can add a new internal GetAll method to each service (or perhaps an intermediary parent class within the same assembly) so that each child service within the assembly can see each other's method; but it seems unnecessary since the functionality is already available in the parent class.
For example:
internal IEnumerable<Employees> GetAll(bool returnIQueryable)
{
return base.GetAll(returnIQueryable);
}
Essentially what I want is for services to be able to access other service methods as IQueryable so that they can further refine the uncommitted results, while everyone else gets plain old lists.
Any ideas?
EDIT
You know what, I had some fun playing a little code golf with this... but ultimately I wouldn't be able to use this scheme anyway because I pass interfaces around, not classes.
So in my example GetAll(bool returnIQueryable) would not be in the interface, meaning I'd have to do casting, which goes against what I'm trying to accomplish.
I'm not sure if I had a brain fart or if I was just too excited trying to get something that I thought was neat to work. Either way, thanks for the responses.