Where to place the code?

I had a theoretical discussion with a development team lead yesterday that had some practical implications.  He has a class like so:

    public class Person

    {

        public int PersonId { get; set; }

        public string PersonName { get; set; }

        public string LastUpdateUser { get; set; }

        public DateTime LastUpdateDate { get; set; }

 

    }

And a Factory (Builder, really) with the usual get and update interface like so:

    public class PersonFactory

    {

        public static Person GetPerson()

        {

            //Implementation

        }

 

        public static void UpdatePerson(Person person)

        {

            //Implementation

        }

    }

He then had a webpage with an ObjectDataSource and DetailsView. 

The crux of the discussion surrounding the LastUpdateId and LastUpdateTime and where to populate that value.  On one hand, the business layer is responsible for controlling the data so  it makes sense for the lastUpdateDate to be popualted in the Update Method of the Factory like so:

person.LastUpdateDate = DateTime.Now;

However, how do you update the LastUpdateUser in the business layer?   You would need to change the interface of the Update method to be something like this:

        public static void UpdatePerson(Person person, string lastUpdateUser)

        {

            //Implementation

        }

Which smells like Interface polution and certainly more regid than I would want.

On the other hand, you can populate the Person object in the UI via the code behind fairly easily:

protected void ObjectDataSource1_Updating(object sender,

            ObjectDataSourceMethodEventArgs e)

        {

            Person person = e.InputParameters[0] as Person;

            person.LastUpdateId = User.Identity.Name;

            person.LastUpdateDate = DateTime.Now;

 

        }

And I love easy.  In addition, just because you don’t code it doesn’t mean it doesn’t exist.  In this case, the Details View and ODS are responsible for hydrating the values of the Person object from the values in the controls (persisted across sessions by the view state) so it actually is more consistent to populate the values in the code-behind on the UI than in the data layer.

The point is, the code has to be written somewhere so why not put it in a place that is both consistant and keeps your Business Layer’s API clean?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: