Business Object Advice, Nested Classes

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;
}
}
}
 
F

Frans Bouma [C# MVP]

miked said:
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.

I think you shouldn't use the design you have now. Nested classes
should be used in situations where you need a class type inside another
class, like a temp binding class in a form, which has no meaning
outside the containing class. Your usage of nested classes isn't doing
that.
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();

Excuse me if I miss something, but what do reader/writer thingies in a
readonly class?
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.

Isn't that because you're things way too difficult? I mean, the
load/save logic is INSIDE person, so that's all private to the class.
If person is readonly, you can only LOAD it. If it's not readonly, you
have no problems.

I was trying to rewrite your code below, but after a while I ended up
with a simple person class which had a load/save routine and a name
getter, as it's a readonly class.

So I think there's something mixed up in your problem: either it's not
a readonly class, and then there's no problem, or it is a readonly
class, and you can simply implement a load routine and a Name getter
and that's it.


FB


--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top