Entity Framework Guide
Entity Framework Guide
Object:
What is OOP?
- Equals()
- An approach to designing and building applications that - GetHashCode()
are: - GetType()
o Flexible - ToString()
o Natural
o Well-crafted
o Testable
- By focusing on objects that interact cleanly one with another
The two most basic words in object-oriented programming are object and class. Though they are often
used interchangeably when speaking about object-oriented programming and when building
applications, they are not the same thing.
A class is code. The properties define the data managed by the class. The functions define the actions
or behaviors managed by the class. In object-oriented programming these functions are called methods.
Together the properties and methods of a class define the class members.
The basic purpose of a class is to provide the definition for a particular type of object. An object is an
instance of a class. It is created using the C# new keyword.
1
- Leveraging reuse - Extensive reuse of existing proven classes not only shortens
development time, it also leads to more robust applications
- Encapsulation is a way to hide or encapsulate the data and implementation details within a
class, thus hiding complexity
- In C#, the data is exposed to the application through property getters and setters
- The operations are exposed to the application through methods
- Encapsulation allows the objects in an application to work together without each object
knowing the details of the implementation. Encapsulation hides both the data and
implementation within the class.
- This data hiding has many benefits.
o It protects the data. No other code can modify the underlying data except through
the getters and setters. This prevents an object's values from being corrupted by
other objects.
o Allows for authorization before getting the data. You can add code to the getter to
perform operations before the data is provided. For example, you could check to
ensure the user has access to the specific data element.
o Allows for validation before setting the data. You can add code to the setter to
perform operations before the data is set
- Implementation hiding
o Helps manage complexity, by breaking it down into manageable units
o Only the class needs to understand the implementation
o Implementation can be changed without impacting the application
2
Static modifier
Overloading
- The term overloading is used to describe methods that have the same name, but different
parameters
Contract
- All of the properties and methods defined using the public access modifier, defined the
class' contract
- The contract is also called the class interface
Constructor
- A constructor is code that is executed each time an instance of the class is created
- It is basically a special kind of method named with the class name
- Constructors are normally defined at the top of the class, above the properties and methods
- If you don't need any other constructors, and you don't need any code in the constructor,
you don't actually have to create a constructor. An implied default constructor will be
defined for you.
3
Coupling
4
Cohesion
- Degree to which members of the class relate to the purpose of the class
Separation of Concerns
Design patterns
- Common practices for defining appropriate classes and their associated relationships
Objects relationships
Collaboration - exists whenever an object from one class uses one or more instances of another class
Composition - exists whenever an object from one class is composed of one more objects from another
class. Composite objects often contain references to their component objects as properties.
5
Inheritance
- Inheritance allows you to build a class that takes on or inherits the members of another
class so you can define a more specific type and it will inherit all of the members of the
classes above it in the hierarchy.
- C# does not allow multiple inheritance, meaning that a class can derive from only a single
class
- Only build an inheritance relationship between your classes if the derived classes have
specialized properties or behaviors that require unique code and you can significantly
leverage code reuse from the base class
6
Techniques for Reuse
7
- Polymorphism is the concept that a single method such as the ToString method, can behave
differently, depending on the type of object that calls it
Abstract classes
- An abstract class cannot be instantiated, meaning you can't use the new keyword to create
one.
- An abstract class is intended only to be used as a base class for other classes and never used
on its own.
Concrete classes
- A concrete class can be instantiated, meaning you can use the new keyword to create one
Sealed classes
- A sealed class is a class that cannot be extended through inheritance, meaning that no other
class can inherit from it.
- Sealing a class prevents extension and more importantly, prevents customization, so if you
want to ensure that no other classes extend or override the functionality of a class, consider
marking it as sealed.
Virtual or Abstract?
- Virtual
o Use the virtual keyword if you want to provide the option for the method to be
overridden.
o When using virtual, the method has its own default implementation, but allows a
derived class to override it.
- Abstract
o Use the abstract keyword if the method must be overridden.
8
o When using abstract the method doesn't have its own implementation, so the
derived classes must implement it.
- So to summarize, a derived class cannot override a base class member unless the base class
member is declared specifically to allow it to be overridden using either the virtual or
abstract keywords. The derived member must then use the override keyword to explicitly
indicate that the method is overridden.
Access modifiers
Static classes
Extension methods
- An extension method allows you to add methods to any existing type including the .NET
types like string, without having the source code of the type, without using inheritance,
without recompiling, without any changes at all to the original type.
- Another benefit of extension methods is discoverability. If another developer on the team is
looking for a method on string that is already implemented, if the developer typed a string
dot, they will see the methods that are available there from your common component. No
need to hunt around for or remember the name of the class in the component. This will help
your reusable code get reused much more often
Interfaces
- Interfaces provide a clean way for your application to interact with other classes,
components, applications, or systems.
- Interfaces are used in many design patterns and interfaces are an integral part of the .NET
framework
- Definition from Wikipedia: In computer science, an interface is a shared boundary across
which two separate components of a computer system exchange information. The exchange
can be between software, computer hardware, peripheral devices, humans, and
combinations of these
9
- Examples of Interfaces in programming:
o UI - the boundary between the user and an application is called the user interface
o Web API - In web development, the boundary between an application that provides
services and the clients requesting those services is called a web API
o Class interfaces - In an object-oriented application, the boundary between each
class and the rest of the application is called a class interface
- Every class has an implicit class interface. That interface is comprised of the public
properties and methods in the class – implicit interface
- In C# there is also the concept of an explicit interface. An explicit interface is a separate
type that you can create using the interface keyword. Like a class, the interface defines a set
of properties and methods
- An interface does not contain any implementation details
10