Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

I am working on a small demo application in which I created few simple domain objects. I am having hardtime understanding the dependency hierarchy. Take a look at the simple example. I have a Person class and an Account class. Here is the implementation of the Person class.

 public class Person : BusinessBase
    {
       
        private string _firstName;
        private string _lastName;
        private string _middleName;
        private Account _account;

        [MustValidate]
        public Account Account
        {
            get { return _account; }
            set { _account = value; }
        }

         public void AddAccount(Account account)
        {
            if (account == null) return;

            _account = account;
            _account.Person = this;
        }

As, you can see from the above class a Person can have a single Account object. A person can add an account using the AddAccount method. Here is the Account class.

 public class Account : BusinessBase
    {
        private string _accountNo;
        private Person _person;

        public Account(Person person)
        {           
            _person = person;
            _person.Account = this;
        }

        [NotNullOrEmpty("Account no cannot be null or empty")]
        public string AccountNo
        {
            get { return _accountNo; }
            set { _accountNo = value; }
        }

       
        public Person Person
        {
            get { return _person; }
            set { _person = value; }
        }
    }

There is only one constructor for the Account object which takes the Person as a parameter. This means that an Account cannot be created without a Person object. Not, only that when a person is passed to an Account constructor the person object account property is set to the account object. This creates a two way relationship between the Person and the Account objects.

But, I can also use person.AddAccount to add a new account. I think I am providing too many ways to add an account to the person object. I can make a default parameterless constructor for the Account object and use person.AddAccount to add the account for the person. But then what about persisting the account object in the database. Whose responsibility is that?

Should I save the account when I call person.Save() or should I save using account.Save(). If I am using account.Save() then I will keep the dependency constructor and remove the AddAccount method from the Person's object.

Also, for validation I have used attributes. So, this means when I say person.IsValid(_brokenRules). It will validate the person object, account object (if exists). But if I say account.IsValid(_brokenRules) it will only validate the account and not the person. The two way validation dependency was causing infinite loops!

I can also have account.AddMember(person);

Off course, if I was using some OR mapper all this would have been done automatically! But if is nice to think about what happens behind the scenes.

Posted on Thursday, March 27, 2008 11:51 AM | Back to top


Comments on this post: Domain Objects and Depedencies

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net