Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I recently converted an old application that was using XML files as the data store to use SQL instead. To avoid a lot of changes I basically created ActiveRecord style classes that inherited from the original business objects.

For example

SomeClassRecord :SomeClass
//ID Property
//Save method

I then used this new class in place of the other one, because of polymorphism I didn't need to change any methods that took SomeClass as a parameter.

Would this be considered 'Bad'? What would be a better alternative?

share|improve this question
    
What do you mean by "bad?" Does it meet your customer's requirements for functionality and maintainability? –  Robert Harvey Aug 2 '12 at 22:59

2 Answers 2

up vote 1 down vote accepted

I would not say this is a 'Bad' thing to do. But there are alternatives with their own trade-offs.

Assuming that the way the original classes were used was the ActiveRecord pattern, here are some alternatives.

  1. Modify each business object's Save() method to delegate the saving work to a class responsible for dealing with storage, whether it's XML or SQL. Instead of decorating each class, you delegate the saving to a single interface, say, IDataStore. For example:

    public class SomeClass {
        IDataStore dataStore; // how this gets set can vary also
        public void Save() {
            dataStore.Save(this);
        }
    }
    

    Now the change is in the implementation of IDataStore.

  2. Continue using your business objects in the business layer, but when doing data access, map your business objects to data access objects, and use the Repository pattern. For example:

    public class SomeBusinessProcess {
      public void DoSomeWork(SomeClass someClass) {
        // ... do some work on someClass ...
    
        // now save
        SomeClassDAO someClassDao = AutoMapper.Map<SomeClass, SomeClassDAO>(); // can use AutoMapper, for example
        repository.Save(someClassDao);
      }
    

You really can take it as far as you want, based on what your needs are.

share|improve this answer

In no way is this is not a 'bad' thing to do. What you did is akin to the decorator pattern. http://www.dofactory.com/Patterns/PatternDecorator.aspx

In fact I suggest that you do read up on the decorator pattern as its a useful tool to have in your design arsenal.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.