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?
2 Answers 2
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.
-
1\$\begingroup\$ Very good answer (as usual)! One little question: am I wrong or is in the last example CategoryID still
public
? \$\endgroup\$Abbas– Abbas2015年09月29日 09:30:11 +00:00Commented Sep 29, 2015 at 9:30 -
\$\begingroup\$ @Abbas no, the setter is
protected
see the second last code block. \$\endgroup\$Heslacher– Heslacher2015年09月29日 09:38:28 +00:00Commented 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\$Abbas– Abbas2015年09月29日 09:47:14 +00:00Commented Sep 29, 2015 at 9:47
-
\$\begingroup\$ Why a base class and not an interface? \$\endgroup\$itsbruce– itsbruce2015年09月30日 10:10:20 +00:00Commented 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\$Heslacher– Heslacher2015年09月30日 10:17:11 +00:00Commented Sep 30, 2015 at 10:17
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 namespaceConsole
is a classWrite
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.
Om_
in the title of the classes, doesn't server any purpose in my opinion. \$\endgroup\$IsActive
toWasActive
? Also, while it's clear that any property ofOm_Category
may be changed, can we assume that the properties of anOm_CategoryHistory
instance will never be changed after creation? (Wouldn't be much of a history if it could be arbitrarily modified) \$\endgroup\$