0

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?

asked Aug 2, 2012 at 17:53
1
  • What do you mean by "bad?" Does it meet your customer's requirements for functionality and maintainability? Commented Aug 2, 2012 at 22:59

2 Answers 2

1

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.

answered Sep 11, 2012 at 19:49
1

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.

answered Aug 3, 2012 at 6:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.