Prayer Request

by Erik Lane 26. July 2007 01:21

Aaron has requested to pass along a prayer request for one of the contributors to A Complete 180, Stefan Tarapchak.  Please lift him and his family up in prayer.

 

Tags:
Categories: Faith and Family

How much should an object know about itself?

by Erik Lane 26. July 2007 00:27

I understand and agree with the Single Responsibility Principle.  But have we gone too far the other way where an object doesn't do much except for carry around data and doesn't know what to do with its own data?  Say you have a Person object that holds all of the data for a person - what do you do when you want to update that person in the data store?  More times than not you will pass your Person object to another object that knows how to update the person.

Example:

IPersonUpdaterProvider provider = new PersonUpdaterProvider();
provider.UpdatePerson(person);

Whatever happened to the Person object knowing how to update itself?  Internal to the person you may have your PersonUpdaterProvider that is read from a config file or use the provider given as a parameter etc..but the code accessing and needing to update the person only needs to call a version of Person.Update().

Example:

Person customer = new Person();
 //...update some stuff
person.Update();
Person customer = new Person(customProvider);
 //...update some stuff
person.Update();

The functionality is all the same and the flexibility is still there.  Internally, the person doesn't know anymore than to call the UpdatePerson() method of the provider and passes itself.  I think we're OK from a single responsibility perspective but we've also added in Tell, Don't ask and a dash of Encapsulation.  In my example, the calling code doesn't need to know about how the person gets updated (unless they really have a need to know).  The person knows what to do - so why can't we leave it to the person?  This helps keep things clear when a developer is working with the person object.  They can see an update method on the object and see what they need to use to get the person's data updated.  No need to ask around to find out what provider/class is used to get a person updated in the data store.

I look at this and I can see that maybe we have the PersonUpdaterProvider created for each instance of a person when they could all use the same one.  Mark the default provider inside the Person object with static and now only one default provider will get created and they can all use it.  You can have an instance variable to store the custom provider so it can be used if needed without forcing all of the other person objects to use it too.

So it might look something like this.

namespace Eriks
{
    public class Person
    {
        private static readonly IPersonUpdaterProvider _provder = 
new DefaultPersonUpdaterProvider(); private IPersonUpdaterProvider _customProvider; public Person() { } public Person(IPersonUpdaterProvider customProvider) { _customProvider = customProvider; } public void Update() { if (_customProvider != null) { _customProvider.Update(this); } else { _provder.Update(this); } } } internal class DefaultPersonUpdaterProvider : IPersonUpdaterProvider { public void Update(Person person) { // update the data the default way... } } public interface IPersonUpdaterProvider { void Update(Person person); } internal class CustomUpdaterProvider : IPersonUpdaterProvider { public void Update(Person person) { // update the dat in a custom way... } } }

Example Usage:

namespace Eriks
{
    public class PersonTest
    {
        public void DoSomething()
        {
            Person person = new Person();
            person.Update();
            Person person2 = new Person(new CustomUpdaterProvider());
            person2.Update();
        }
    }
}

Giddy Up!

Tags:

Kids are so funny!

by Erik Lane 19. July 2007 03:35
These are not my kiddos but it's still so funny I had to share.
Tags:

Collection Best Practices from MSDN Magazine

by Erik Lane 19. July 2007 02:19

I get MSDN Mag every month and it has been a while since I've read an article that really catches my attention.  The new stuff is interesting but I am reading them for informational purposes only because I'm not currently using them.

This month the CLR Inside Out column has a great article on collection best practices.  This is a topic that most developers can use immediately.  Even if you know this stuff it doesn't hurt to get a refresher every now and then.  Plus, it can reaffirm what you already knew or remind you of something you may have forgotten about.

  • When to Create a Custom Collection
  • Existing Collections
  • Generic or Non-Generic
  • Performance Implications
  • Collection Interfaces
  • A Family Tree - Good tips on working with collections and their relationships.

Giddy Up!

Tags:

Set Firefox as default Browser in Visual Studio

by Erik Lane 19. July 2007 01:56

I guess I have to be one of the last ones to figure this out but  you can get VS.NET to open up Firefox by default and even when in debug mode.  With Firebug installed, running a debug site in FF just makes working on the styles/layouts so much easier.  Now I don't have to attach to process, etc..

You can set this up in two different ways.

1.  File -> Browse With... -> Click Add
2.  Browse With... menu should be on your context menu when you right-click an ASPX page in your solution window.

For me, Browse With... wasn't in my file menu so I had to go to Tools -> Customize -> File and drag it to the file menu.

Giddy Up!

Tags: