1
\$\begingroup\$

I have two classes in this project;

Category: This will be responsible to hold the latest information.

public class Om_Category
{
 public Int32 CategoryID { get; set; }
 public String CategoryName { get; set; }
 public String CategorySanitized { get; set; }
 public Boolean IsActive { get; set; }
 public DateTime CreationDate { get; set; }
 public Int32 ModifiedBy { get; set; }
 public Int64 CreatedBy { get; set; }
 public Int64 ModifiedBy { get; set; }
}

Category History: This will hold the history of all modifications done for any category.

public class Om_CategoryHistory
{
 public Int32 CategoryID { get; set; }
 public String CategoryName { get; set; }
 public String CategorySanitized { get; set; }
 public Boolean IsActive { get; set; }
 public DateTime LastModificationDate { get; set; }
 public Int64 LastModifiedBy { get; set; }
}

Is there anything that I can reuse or optimizations I can make?

BCdotWEB
11.4k2 gold badges28 silver badges45 bronze badges
asked Sep 29, 2015 at 5:56
\$\endgroup\$
6
  • \$\begingroup\$ Any specific reason for the Om_ in the title of the classes, doesn't server any purpose in my opinion. \$\endgroup\$ Commented Sep 29, 2015 at 9:36
  • \$\begingroup\$ Om stands for Object Model. So in this scenario it is Om_Category. I saw in many files, people like to say Business Entities. So it becomes BE_Category \$\endgroup\$ Commented Sep 29, 2015 at 9:38
  • \$\begingroup\$ Then can't you create a namespace to separate the entities? \$\endgroup\$ Commented Sep 29, 2015 at 9:48
  • \$\begingroup\$ see my below answer for a bigger explanation. \$\endgroup\$ Commented Sep 29, 2015 at 11:08
  • \$\begingroup\$ Shouldn't you change IsActive to WasActive? Also, while it's clear that any property of Om_Category may be changed, can we assume that the properties of an Om_CategoryHistory instance will never be changed after creation? (Wouldn't be much of a history if it could be arbitrarily modified) \$\endgroup\$ Commented Sep 30, 2015 at 11:00

2 Answers 2

5
\$\begingroup\$

You can extract the common for both properties to a base class which both classes then inherit like so

public class Om_CategoryBase
{
 public Int32 CategoryID { get; set; }
 public String CategoryName { get; set; }
 public String CategorySanitized { get; set; }
 public Boolean IsActive { get; set; }
 public DateTime LastModificationDate { get; set; }
 public Int64 LastModifiedBy { get; set; }
} 
public class Om_Category : Om_CategoryBase
{
 public DateTime CreationDate { get; set; }
 public DateTime ModifiedBy { get; set; }
} 
public class Om_CategoryHistory : Om_CategoryBase
{
} 

but you should maybe come up with a better name.

If the property setters don't have to be public you should consider to make them protected instead.

Based on the comment

Kindly explain with the help of an example for this :If the property setters don't have to be public you should consider to make them protected instead.

Like public and private the protected scope defines in which scope a method or property can be accessed. Protected means it can be accessed by the class itself and the class which is inheriting from this class.

For instance if you would make the setter of CategoryID protected like so

public class Om_CategoryBase
{
 public Int32 CategoryID { get; protected set; }
 public String CategoryName { get; set; }
 public String CategorySanitized { get; set; }
 public Boolean IsActive { get; set; }
 public DateTime LastModificationDate { get; set; }
 public Int64 LastModifiedBy { get; set; }
} 

you can only set this property from within this base class and from inside the derived classes Om_Category and Om_CategoryHistory. If you would try to set this property from another class which doesn't inherit the base class, your IDE would show an error.

So assuming the Om_Category would have a constructor like so

public Om_Category (int catagoryId)
{
 CategoryID = categoryId;
} 

the CategoryID property would be set to the passed in categoryId value.

answered Sep 29, 2015 at 6:04
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Very good answer (as usual)! One little question: am I wrong or is in the last example CategoryID still public? \$\endgroup\$ Commented Sep 29, 2015 at 9:30
  • \$\begingroup\$ @Abbas no, the setter is protected see the second last code block. \$\endgroup\$ Commented Sep 29, 2015 at 9:38
  • \$\begingroup\$ Somehow I knew I had to leave your answer and don't just edit it, thanks for clarifying! \$\endgroup\$ Commented Sep 29, 2015 at 9:47
  • \$\begingroup\$ Why a base class and not an interface? \$\endgroup\$ Commented Sep 30, 2015 at 10:10
  • \$\begingroup\$ @itsbruce because here it wouldn't really help IMHO. The op asks for reuse so its either needs to be base class with inheritance or interface with composition where I choose the former because the op seems to be a beginner. \$\endgroup\$ Commented Sep 30, 2015 at 10:17
3
\$\begingroup\$

The answer of Heslacher covers the most important part. This answer is intended as a reply to the comments, regarding the use of a namespace. Here's the definition of the namespace keyword (from MSDN):

The namespace keyword is used to declare a scope that contains a set of related objects. You can use a namespace to organize code elements and to create globally unique types.

A simple example:

System.Console.Write("Hello world!");

In above code:

  • System is the namespace
  • Console is a class
  • Write is a method

There are many classes in the System namespace. Namespaces can also be nested, for example the System.Data namespace, it contains several classes for the ADO.NET architecture in .NET.

Deeply nested namespaces will result in longer code. For example:

namespace Services
{
 namespace Management
 {
 namespace Data
 {
 class MyClass
 {
 }
 }
 }
}

Everytime you want to instantiate a class in the Data namespace you'd have to write:

Services.Management.Data.MyClass myClass = new Services.Management.Data.MyClass();

You have two options:

  • use the using statement:

    using Services.Management.Data;
    MyClass myClass = new MyClass();
    
  • use an alias:

    using data = Services.Management.Data;
    data.MyClass = new data.MyClass();
    

More reading on namespaces:

Now you know what you can do with namespaces, you can apply it to your code:

namespace ObjectModel
{
 public class Category { }
}
namespace BusinessEntities
{
 public class Category { }
}
var omCat = new ObjectModel.Category();
var beCat = new BusinessEntities.Category();

As I said, your main question is resolved by the answer of Heslacher but I hope this answer is of any help.

answered Sep 29, 2015 at 11:07
\$\endgroup\$

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.