Zo Has
Zo Has

Reputation: 13018

Base class usage in c #?

Good day. I am just wondering how to create a base class and use it for my business objects. The base class would have some attributes like IsNew, IsDirty etc.

  1. What I want to know is how to inherit this base class ?
  2. Also what does the 'base' syntax means ? Is it always required to use 'base' to access the base class ?
  3. How many base classes can I have ?
  4. Can you show me a little sample

I did google but it was sort of confusing. Here I would get a quick answer. Thank you.

Upvotes: 0

Views: 327

Answers (2)

Sergio Acosta
Sergio Acosta

Reputation: 11440

I would suggest reading how others have done it. One of the most popular business objects frameworks for .NET is CSLA:

http://en.wikipedia.org/wiki/Component-based_Scalable_Logical_Architecture

Basically, those frameworks give you an already made base class that handles all the common work (the IsDirty property, an identity field, etc) and you inherit all your business objects from it.

Here are some blog posts that shows how the CSLA base classes are designed, and some variants:

http://pavelsem.blogspot.com/2009/05/cslanet-introduction-first-business.html http://madskristensen.net/post/A-smart-base-class-for-business-objects.aspx

You might even want to use some kind of framework and save a bunch of effort if you are not doing this just for learning purposes.

The author of the CSLA framework has several books on how the framework is designed and how to use it. They are very easy to follow and worth buying, in my opinion:

Expert C# 2008 Business Objects (link to Amazon)

Upvotes: 1

Jagmag
Jagmag

Reputation: 10356

Any class which is not sealed can be a base class.

To inherit from a class, the syntax would be

class ChildClass : ParentClass

in C#, the base keyword is used to refer to the parent class from within the child class.

Since there is no multiple inheritance i.e a class can have only one immediate parent class, base refers to that parent

I dont think it is always required to use base.XYX to use the base class. This kind of syntax is commonly used for constructors to call the base constructor or to invoke base members which might be over-ridden More details on base here

Upvotes: 1

Related Questions