G
Guest
I am architecting in a read only class for use in mapping data to a business
object. The object makes strong use of nested classes and their ability to
access protected fields. The downside is when a nested class inherits from
it’s parent class you get this infinite class chain in intellisense when
consuming the class. To get around this I created two child classes Reader
and Writer which require a base Person object.
When consuming the class you do:
Person.Reader person = Person.Reader.Load(“mikeâ€); // Load a read only object
String name = person.Name;
Person.Writer person = Person.Writer.Load(“mikeâ€); // Load a writable object
person.Name = “Mikeâ€;
person.Save();
The biggest problem I have is trying to use nested classes to provide the
necessary protected access needed by the nested support classes like data
adapters. Some of the child classes such as the Writer should really inherit
from the Reader, which should just be the person, but when I do this I get
Person.Writer.Writer.Writer.Writer which is confusing when consuming. The
code below shows a workaround passing in the base Person to the Writer and
The Reader which I don’t like.
public class Person
{
protected string _name;
public class Reader
{
Reader(Person person)
{
this._person = person;
}
private Person _person;
public string Name
{
get { return this._person._name; }
}
public static Person.Reader Load(string name)
{
return new Reader(Adapter.Load(name));
}
}
public class Writer
{
Writer(Person person)
{
this._person = person;
}
private Person _person;
public string Name
{
get { return this._person._name; }
set { this._person._name = value; }
}
public static Person.Writer Load(string name)
{
return new Writer(Adapter.Load(name));
}
}
private class Adapter
{
public static Person Load(string name)
{
Person p = new Person();
p._name = name;
return p;
}
}
}
object. The object makes strong use of nested classes and their ability to
access protected fields. The downside is when a nested class inherits from
it’s parent class you get this infinite class chain in intellisense when
consuming the class. To get around this I created two child classes Reader
and Writer which require a base Person object.
When consuming the class you do:
Person.Reader person = Person.Reader.Load(“mikeâ€); // Load a read only object
String name = person.Name;
Person.Writer person = Person.Writer.Load(“mikeâ€); // Load a writable object
person.Name = “Mikeâ€;
person.Save();
The biggest problem I have is trying to use nested classes to provide the
necessary protected access needed by the nested support classes like data
adapters. Some of the child classes such as the Writer should really inherit
from the Reader, which should just be the person, but when I do this I get
Person.Writer.Writer.Writer.Writer which is confusing when consuming. The
code below shows a workaround passing in the base Person to the Writer and
The Reader which I don’t like.
public class Person
{
protected string _name;
public class Reader
{
Reader(Person person)
{
this._person = person;
}
private Person _person;
public string Name
{
get { return this._person._name; }
}
public static Person.Reader Load(string name)
{
return new Reader(Adapter.Load(name));
}
}
public class Writer
{
Writer(Person person)
{
this._person = person;
}
private Person _person;
public string Name
{
get { return this._person._name; }
set { this._person._name = value; }
}
public static Person.Writer Load(string name)
{
return new Writer(Adapter.Load(name));
}
}
private class Adapter
{
public static Person Load(string name)
{
Person p = new Person();
p._name = name;
return p;
}
}
}