By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,749 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,749 IT Pros & Developers. It's quick & easy.

Business Object Advice, Nested Classes

P: n/a
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;
}
}
}
Jun 3 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
miked wrote:
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#)
------------------------------------------------------------------------
Jun 4 '06 #2

P: n/a
You code does not show any inheritance though. Should the inheritance syntex
like:

public class Reader: Person

if you are doing inheritance?

But do read more on Interface, Abstract class, Virtual method before you do
further decision.

Have a read of the C# specification:
http://msdn.microsoft.com/netframewo...r/default.aspx

chanmm

"miked" <mi***@discussions.microsoft.com> wrote in message
news:0B**********************************@microsof t.com...
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;
}
}
}

Jun 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.