0

Is it considered a good practice to pass the model class from e.g. the DAL to a threaded service class as 'information model'?

Example:

public class CarModel {

    public string Name                 { get; set; }
    public int    AmountOfWheels       { get; set; }
    public Engine EngineDescription    { get; set; }

}

public class Car {

    public CarModel CarModel        { get; set; }

    public Car(CarModel model) {
        this.CarModel = model;

        Thread.Start(BrummBrumm);
    }

    private void BrummBrumm() {
        // start the car
    }

}

This example is made under the assumption that CarModel is a entity (e.g. to use with Entity Framework or any other repository/DAL) or a model class to use with UI, WebApi, WCF.. and Car is a class that resides as implementation in e.g. a Windows service.

Edit further code

public class CarManager {

    public List<Car> Cars = new List<Car>();

    public void Add(CarModel model) {
        this.Cars.Add(new Car(model));
    }

    public void Remove(int id) {
        ...
    }

}

... then what's with the example above? What if I don't just have cars, but also motorcycles? Wouldn't the example above create a lot of boilerplate code?

Acrotygma
  • 2,531
  • 3
  • 27
  • 54
  • A strategy pattern might help in this case: http://stackoverflow.com/questions/3449634/how-to-use-the-strategy-pattern-with-c – Keith Payne Jan 26 '14 at 22:00

1 Answers1

0

Best practice is to use DTOs while transfering data from/to services. There are also tools to reduce code required to map DTO's from/to business objects. Personally I use Automapper.

There's a good article just in case you want to read up on DTO's - Pros and Cons of Data Transfer Objects

However if your solution is small and is not supposed to grow and your services are not publicly exposed you are good to go with just your business objects to avoid overdesign.

Hope it helps!

Maksym Strukov
  • 2,679
  • 1
  • 13
  • 17
  • @Acrotygma hi there, you didn't make any comments to my answer, just wonder if it was helpful to you? If not is there is anything I can do to help you with your question? – Maksym Strukov Apr 18 '14 at 11:46