views:

124

answers:

3

I am still fairly new to C# and I am trying to decide the best way to structure a new program. Here is what I want to do and I would like feed back on my idea.

  • Presentation Layer
  • Business Layer (Separate Class Library)
  • Data Layer (Separate Class Library)
  • Model Layer (Separate Class Library)

What I am struggling with is if it is ok to have the classes in the Data Layer and Business Layer inherit from the types I define in Model Layer. This way I can extended the types as needed in my Business Layer with any new properties I see fit. I might not use every property from the Model type in my Business Layer class but is that really a big deal? If this isn't clear enough I can try and put together an example.

A: 

I think an example would be good - I don't think what you're suggesting is necessarily bad, but it depends on what you're trying to achieve.

Why are you inheriting from classes in your model layer? What is the interface and purpose of the specific model classes, and what is the purpose of the business logic classes which are inheriting from your model classes?

Does inheritance really make sense?

Would inheritance violate Liskov's substitution principle?

EDIT:

wpfwannabe, I would be careful about using inheritance just because it seems to be the easy option. I specifically mentioned the Liskov substitution principle because it deals with determining when inheritance is valid.

In addition, you may also want to look at the 'SOLID' design principles for OO development (which includes Liskov's substitution principle):

http://en.wikipedia.org/wiki/Solid_(object-oriented_design)

Phil
In this example my model layer would contain types which are representative of a database table. Simply classes containing properties and constructors. I thought inheriting from a model class in the business layer would be a good idea so I would have all my base properties and I could create new ones as needed.
+5  A: 

The general practice is to use encapsulation, not inheritance, for layer transitions. Consider the following two paradigms (if I understand you correctly)

Model/Data Layer:
    Customer
    Order

Business Layer:
    MyCustomer : Customer
    MyOrder : Order

versus

Model/Data Layer:
    Customer
    Order

Business Layer:
    MyCustomer (encapsulates Data.Customer)
    MyOrder (encapsulates Data.Order)

There are two main issues when going the first (inheritance) route:

  1. When you modify the base (data/model) class, you're forced to change the business class.
  2. Getting object relationships is difficult and generally requires a non-polymorphic approach. I.E., if the model or data layer exposes a collection of Orders on a Customer object, it's difficult and "kludgy" to get your MyCustomer class to expose a collection of MyOrder objects instead.

Utilizing encapsulation deals with both of these issues, and is definitely the route I'd recommend.

Judging by your name, I'm assuming you're looking to write a WPF application. If that's the case, look into the Model-View-ViewModel (MVVM) design pattern.

Adam Robinson
You pretty much hit the nail on the head. Since I am still fairly new to this. I understand the concept of encapsulation in regards to properties but I don't think I am understanding how it applies in this situation. Could you elaborate a little more?
+1 for the core concept of encapsulation across layers.
scope_creep
+1 for MVVM. @wpfwannabe, I struggled for weeks learning MVVM until I watched this video: http://www.lab49.com/files/videos/Jason%20Dolinger%20MVVM.wmvI really suggest you give it a look after you've played with WPF for a while. Good luck!
bufferz
That was a really good video. Cleared somethings up and dirtied some others...
A: 

Whether to put the classes of each layer in a separate library depends on if you would need to write more applications in the future that would need to use these same classes. You might think that you need and start doing right away. From my experience, I start to have better ideas after coding and having at least some kind of proof of concept. Organizing code, such as re-factoring, organizing into libraries, etc at some point during the middle of the project is easy. It's trying to do that right before going live which is risky and not a good idea.

Regarding the remaining parts of your question, I would refer to you what I learned from Martin Fowler as I can't say better than him and if I do, I am just repeating after him:

http://martinfowler.com/eaaCatalog/serviceLayer.html
http://martinfowler.com/eaaCatalog/

Khnle