Index
- Introduction
- Applying encapsulation
- Properties
- Use explicit interface implementations
- References
Introduction
This post is part of a series of post to help you prepare the MCSD certification, particularly the certification exam 70-483, based on the book:You will find all the code available in the following GitHub repository. Lets talk a little bit about threads and how they work using a .Net development environment.
Applying encapsulation
Encapsulation is one of the pillars of Object-oriented programming languages (with inheritance and polymorphism) and gives you the ability to hide specific content within a class to make better software. A class must be built for an specific purpose and only allow access to some particular bits of the class. You don't need to understand how it works just know how to use it. See the following access modifiers from less to more restricted:
- public: no restriction to access. "Private" is the most restricted access modifier, only accessible by members within the same type. "Protected" brings the option of share data between objects with a inheritance relation.
- internal: only access to classes in the same assembly. "Internal" is more restricted than "Public" but less than "Private" and is useful when you have a helper class or an implementation detail you just want to share within the assembly you are building. There can be cases when you want to expose internal types or type members to another assembly, you can use the attribute: InternalsVisibleToAttribute. For example, when you write a unit test for a particular piece of code which has been decorated with Internal you need to specify this attribute inside the AssemblyInfo.cs stored in the properties folder of your project: [assembly:InternalsVisibleTo("ClassA")]
- protected: only access from the containing class and derived classes
- protected internal: only access from the current assembly or derived classes
- private: only access from the containing class.
The following list shows which access modifiers can be used in specific situations:
- enum;
- default (if nothing is declared): public
- allowed: none
- class:
- default: private
- allowed: public, protected, internal, private, protected internal
- interface:
- default: public
- allowed: none
- struct:
- default: private
- allowed: public, internal, private.
Keep in mind the access modifier for enclosing types. For example, a public method within an internal class has an accessibility of internal. There are some exceptions to this like when an internal class implements a public interface or when a class overrides a public virtual member of a base class. As good practice, use always the most restrictive access modifier because we want to hide as much information as possible.
Properties
It's always a good practice to use properties in our code because they give you a good view of what's going in on in your data when someone pull (get) or push (set) information to them. The default implementation in C# comes with a shorthand version (public int Value { get; set; }). You can define different access modifiers for get and set.
A single get accessor can come in handy when you want to create a true read-only property or with a property which calculates its value on the fly. A property with only a set accessor is less common, but maybe you can use it as a fire-and-forget where the users sets the value and never checks it.
Use explicit interface implementations
Interfaces are useful with encapsulation. Explicit interface implementation means that an interface type element can be accessed only when using the interface directly. For example, in Entity Framework you use a class called DbContext which is a more handy implementation of the ObjectContext class. Although DbContext implements IObjectContextAdapter which contains an ObjectContext property, you won't access this property from your DbContext directly. You're forced to cast to the interface first as shown below:
This is possibile because DbContext implements the interface IObjectContextAdapter explicitly. Explicit interface implementation means that an interface type element can be accessed only when using the interface directly. You can create one explicit interface implementation by adding the interface name and a period to the implementation.
Only when you cast Implementation to IInterfaceA you have access to MyMethod, this can be used to hide members of a class to outside users. There's another situation when you need this approach and it happens when you implement two interfaces with the same method names.
0 comments:
Post a Comment