Groups > Asp .Net > Advanced ASP.NET Architecture > Re: OOP Class Architecture question...




OOP Class Architecture question...

OOP Class Architecture question...
Mon, 31 Mar 2008 05:09:41 +000
If I were to try to mimic the architecture that Microsoft is using for their
DataSet class to do a few classes for myself, how would I accomplish this?  I'd
like to mimic the following...

Dim ds as New Data.Dataset()

ds.Container.Add(...)

Notice above that I create a new instance of  Dataset and then I can select the
Container property and then I can select the Add method to add a new Container
to the Dataset instance that I named "ds" without doing anything extra
or special.  How does Microsoft layout their Classes for to accomplish this? 
Container is obviously another Class that has a Shared Sub of "Add"
that can just be called without creating a New instance of it.  I'd like the do
the same with the following:

Dim t as New Transaction()

t.Account.Add("Username","Password")

Obviously I need a Transaction class

Public Class Transaction
   ...
End Transaction 

With either a property that calls the Account class or there's a sub class of
Account inside the Transaction class - I dunno.  This is where it all gets a
little hairy for me and I feel like I've tried everything at this point so I
must be missing a very crucial piece to this puzzle.

Thanks in advance!

Rich
Post Reply
Re: OOP Class Architecture question...
Mon, 31 Mar 2008 07:46:41 +000
I am not 100% sure how the DataSet Container property works... but if you want
to have a property (Account) that allows you to add multiple accounts to it you
can try doing this:

Specify the Account property as a collection of Account objects

public List<Account> Accounts{ get; }

You can now use the following code to add new accounts to it:

t.Account.Add(new Account("Username","Password"));

I am using here a generic List as collection. This allows to add an object, that
is why I have the "new Account" as parameter. If you want to have it
like you have, you can create your custom collection and override the Add method
so that it takes only those two string parrameters. The method will then create
an Account object and add it to it's inner collection.

Here is an article about custom collection. I would use the CollectionBase class
and override the Add method like this:
http://www.codeproject.com/KB/tabs/propertygridcollection.aspx

public void Add(string un, string an)
{
    Account account = new Account(un, sn);
    List.Add(account);
}


Hope it helps you out a little.
Post Reply
Re: OOP Class Architecture question...
Mon, 31 Mar 2008 08:13:00 +000
I'd really rather try to figure out how to do it as I see it most commonly done.
 It's just weird because this scenario is SOOooo common within the .NET
framework and yet no one I know or have asked knows how to replicate it with
custom classes.
Post Reply
Re: OOP Class Architecture question...
Mon, 31 Mar 2008 10:06:15 +000
Well, my code seems to do exactly what you want... Here it is:

Account object class:

public class Account
    {
        public string Username { get; set; }
        public string Password { get; set; }

        public Account(string username, string password)
        {
            this.Username = username;
            this.Password = password;
        }
    }

The collection class:

public class AccountCollection : CollectionBase
    {
        public void Add(string username, string password)
        {
            Account account = new Account(username, password);
            List.Add(account);
        }
    }

 The Transaction class:

public class Transaction
    {
        public AccountCollection Account { get; set; }

        public Transaction() 
        {
            this.Account = new AccountCollection();
        }
    }

 (not, you need to initialize the collection in the constructor)

The final call:

class Program
    {
        static void Main(string[] args)
        {
            Transaction t = new Transaction();
            t.Account.Add("Username", "password");
            t.Account.Add("UN2", "PW2");

            foreach (Account acc in t.Account)
                Console.WriteLine("UserName:  -- Password: ",
acc.Username, acc.Password);
        }
    } If this is not it, than I must have misunderstood your question. Sorry for
that :D
Post Reply
about | contact