Enterprise Systems
SI-342
Compiled and structured by Daud Anjum
ENTERPRISE SYSTEMS SI-342
Table of Contents
1. Overview of Enterprise Application Development .................................................................................... 5
Microsoft Technology History ........................................................................................................................ 5
Introduction to .NET and its Architecture ...................................................................................................... 7
Concept of MSIL, CLR, CLS, CTS ...................................................................................................................... 9
2. Introduction to .NET Framework ............................................................................................................. 11
Managed and Unmanaged Code ................................................................................................................. 11
.NET Assembly .............................................................................................................................................. 13
Introduction of C# Fundamentals ................................................................................................................ 14
Boxing and Unboxing ................................................................................................................................... 17
Implementing Multi-tier Architecture ......................................................................................................... 18
3. Introduction to [Link] ......................................................................................................................... 20
SQL Injection ................................................................................................................................................ 21
Parameterized Queries ................................................................................................................................ 23
Usage of Data Set ......................................................................................................................................... 25
Data Adapter and Command Builder in Disconnected Model..................................................................... 26
4. Introduction to Delegate .......................................................................................................................... 28
Multicast Delegates...................................................................................................................................... 29
Introduction to Windows Forms, Html ........................................................................................................ 31
5. Introduction to JavaScript ........................................................................................................................ 33
JavaScript and its Data Types ....................................................................................................................... 34
Variables and Functions ............................................................................................................................... 36
Debugging JavaScript Using Firebug ............................................................................................................ 39
6. Introduction to Various Object Models ................................................................................................... 40
Browser’s Object (BOM) .............................................................................................................................. 42
Document Object Model ............................................................................................................................. 44
7. Introduction to jQuery ............................................................................................................................. 46
jQuery Effects ............................................................................................................................................... 48
8. Introducing LINQ ...................................................................................................................................... 51
LINQ to Objects ............................................................................................................................................ 53
LINQ to SQL .................................................................................................................................................. 55
Query Syntax ................................................................................................................................................ 58
By Daud Anjum 2
ENTERPRISE SYSTEMS SI-342
Operations (Projection, Filtering, and Join) using LINQ Queries ................................................................. 60
9. Introduction to [Link] Entity Framework ............................................................................................ 62
The Entity Data Model ................................................................................................................................. 63
CSDL ............................................................................................................................................................. 65
Eager vs Lazy Loading ................................................................................................................................... 67
POCO classes ................................................................................................................................................ 68
DBContext API .............................................................................................................................................. 70
Querying Entity Data Models ....................................................................................................................... 72
10. Introduction to [Link] MVC .............................................................................................................. 75
MVC Application Structure .......................................................................................................................... 76
Controllers Overview ................................................................................................................................... 78
Action Methods............................................................................................................................................ 80
Parameterized Action Methods ................................................................................................................... 83
Introduction to Razor Syntax........................................................................................................................ 85
Code Expressions ......................................................................................................................................... 88
Code Blocks .................................................................................................................................................. 89
Implicit vs Explicit Code Expression ............................................................................................................. 91
Data Annotations ......................................................................................................................................... 92
Client and Server-Side Validation ................................................................................................................. 94
Validation and Model Binding ...................................................................................................................... 96
Validation and Model State .......................................................................................................................... 99
MVC Membership ...................................................................................................................................... 100
Authorization and Security ........................................................................................................................ 103
11. Introduction to Service Oriented Architecture .................................................................................. 105
SOAP ........................................................................................................................................................... 106
WSDL .......................................................................................................................................................... 108
Service Contract ......................................................................................................................................... 110
Data Contract ............................................................................................................................................. 112
XML ............................................................................................................................................................ 114
WCF Bindings ............................................................................................................................................. 116
ABC of WCF ................................................................................................................................................ 119
RESTful Services ......................................................................................................................................... 121
By Daud Anjum 3
ENTERPRISE SYSTEMS SI-342
Consuming REST services (CRUD operations) using jQuery AJAX and JSON ............................................. 123
Introduction of Web API ............................................................................................................................ 125
Example of Web API using CRUD ............................................................................................................... 127
MVC Routing .............................................................................................................................................. 129
By Daud Anjum 4
ENTERPRISE SYSTEMS SI-342
1. Overview of Enterprise Application Development
Enterprise application development refers to the process of designing, building, deploying, and
maintaining software systems that support the operations, processes, and goals of large
organizations. These applications are typically complex, scalable, distributed, and mission-critical.
They are designed to handle large volumes of data and users, integrate with various business
systems, and ensure high levels of performance, reliability, and security.
At the core of enterprise application development is the need to automate and streamline business
processes across departments such as finance, human resources, supply chain, customer
relationship management (CRM), and enterprise resource planning (ERP). These applications
must often interact with legacy systems, databases, and external services, requiring robust
integration capabilities. Developers use various architectural patterns like multi-tier architecture,
service-oriented architecture (SOA), and microservices to ensure scalability, modularity, and ease
of maintenance.
The development process usually involves multiple stages, including requirement analysis, system
design, coding, testing, deployment, and ongoing maintenance. Modern enterprise applications
are often build using frameworks and platforms such as Java EE, .NET, Spring Boot, and others,
which provide tools and libraries to support enterprise-level features like transaction management,
security, and scalability.
Enterprise applications also require careful attention to user interface design, especially with the
growing demand for web-based and mobile-accessible systems. In addition, they must comply
with industry standards and regulations, particularly in sectors like finance, healthcare, and
government. As businesses increasingly adopt cloud computing, enterprise application
development is shifting towards cloud-native architectures, which offer greater flexibility,
scalability and cost-efficiency.
Microsoft Technology History
Microsoft has played a pivotal role in the evolution of enterprise application development (EAD)
within the broader scope of enterprise systems (ES). Over the decades, Microsoft technologies
have shaped the way organizations build, deploy, and manage business-critical applications. From
early desktop software to modern cloud platforms, Microsoft has continuously expanded its
ecosystem to meet the growing needs of enterprise environments.
1. Early Days: Desktop to Client-Server Era
In the 1980s and early 1990s, Microsoft gained prominence with its Windows operating
systems and Office suite, which became standard tools in business environments. During this
period, most enterprise applications were desktop-based or followed the client-server model.
Technologies like Microsoft Access and Visual Basic enabled developers to build simple
business applications with graphical user interfaces.
By Daud Anjum 5
ENTERPRISE SYSTEMS SI-342
With the introduction of Windows NT and later Windows 2000, Microsoft began targeting
enterprise-grade operating systems, providing better security, networking, and administrative
capabilities. The Active Directory service (introduced in Windows 2000) became crucial for
identity and access management across enterprises.
2. .NET Framework Era (2002-Present)
A major turning point came in 2002 with the release of the .NET Framework. This software
framework provided a consistent object-oriented programming environment and supported
multiple programming languages, most notably C# and [Link]. The .NET Framework was
designed to enable rapid development of enterprise-grade applications on windows.
Key components included:
• [Link] for web development
• [Link] for data access
• Windows Forms and later WPF (Windows Presentation Foundation) for desktop
applications
• Windows Communication Foundation (WCF) for service-oriented architecture (SOA) and
distributed computing
The Visual Studio IDE, Microsoft’s flagship development environment, integrated tightly with
the .NET Framework, offering features like IntelliSense, debugging, and performance profiling
– boosting developer productivity significantly.
3. Enterprise Tools and Servers
To support back-end infrastructure, Microsoft introduced several enterprise-grade server
products:
• SQL Server for relational database management
• Exchange Server for email and calendaring
• SharePoint Server for collaboration and document management
• BizTalk Server for business process automation and system integration
These products, combines with Active Directory and Windows Server, formed a complete
enterprise ecosystem that many organizations adopted.
4. .NET Core and Cross-Platform Development
With the rise of open source and cross-platform needs, Microsoft released .NET Core in 2016
– a lightweight, modular, and cross-platform framework. Unlike the original .NET Framework,
which was Windows-only, .NET Core could run on Linux and macOS, making it more
appealing for cloud and container-based development.
[Link] evolved in [Link] Core, enabling high-performance, cross-platform web
applications. This shift also marked Microsoft’s transformation into a more open-source
friendly company, with .NET Core being hosted on GitHub.
By Daud Anjum 6
ENTERPRISE SYSTEMS SI-342
5. Microsoft Azure and Cloud-Native Development
Microsoft entered the cloud computing space with Azure in 2010, which has since become one
of the leading cloud platforms. Azure supports Infrastructure as a Service (IaaS), Platform as
a Service (PaaS), and Software as a Service (SaaS) offering tailored for EAD.
Key Azure services for EAD include:
• Azure App Services for hosting web apps
• Azure SQL Database and Cosmos DB for managed databases
• Azure Functions for serverless computing
• Azure DevOps for CI/CD and project management
• Azure Active Directory for cloud-based identity management
Azure has enabled businesses to adopt cloud-native architectures, improve scalability, and
reduce infrastructure costs. Microsoft also supports containerization with Azure Kubernetes
Service (AKS) and integrates seamlessly with tools like Docker.
6. Unified Development with .NET 5 and Later
In 2020, Microsoft released .NET 5, a unified platform intended to replace .NET Framework,
.NET Core, and Xamarin. It was followed by .NET 6 and .NET 7, which provide long-term
support (LTS) and new features for performance, security, and productivity. This unification
simplifies enterprise development by offering a single platform for building web, desktop,
mobile, cloud, and IoT applications.
7. Modern Tools and Low-Code Platforms
Microsoft also expanded into low-code development with Power Platform, which includes:
• Power Apps for building custom applications quickly
• Power Automate for workflow automation
• Power BI for data visualization and business intelligence
These tools allow enterprises to empower non-developer users (citizen developers) to
contribute to application development, reducing depending on IT departments and
accelerating digital transformation.
Introduction to .NET and its Architecture
The .NET platform, developed by Microsoft, is a powerful, flexible, and scalable framework
designed to support the development and execution of a wide range of applications, especially
enterprise-level solutions. Introduced in the early 2000s, .NET has become a core part of EAD by
providing a unified environment for building web, desktop, mobile, cloud, and IoT applications.
Its consistent programming model, multi-language support, and extensive libraries make it a
popular choice for businesses aiming to develop secure, reliable, and maintainable software
systems.
By Daud Anjum 7
ENTERPRISE SYSTEMS SI-342
What is .NET?
.NET is a free, open-source, cross-platform development platform for building many types of
applications. It supports multiple programming languages, and allows developers to create
applications that run on Windows, macOS, and Linux. Originally launched as the .NET
Framework, the platform has evolved into .NET Core and not simply .NET (staring from .NET
5), which provides a single unified platform for all types of applications.
.NET includes a vast class library known as the Base Class Library (BCL), which provides
commonly used functionality such as file I/O, data access, cryptography, XML manipulation, and
networking. It also includes support for building RESTful APIs, desktop UIs, cloud services, and
rea-time applications.
.NET Architecture Overview
The architecture of .NET is modular, layered, and designed to ensure performance, portability,
security, and scalability. The key components of .NET architecture include:
1. Common Language Runtime (CLR)
The CLR is the execution engine of .NET. It handles program execution and provides services
such as memory management, garbage collection, security enforcement, exception handling,
and thread management. It ensures that applications run safely and efficiently by managing
runtime behaviour.
2. Base Class Library (BCL)
The BCL is a rich set of reusable classes, interfaces, and value types that developers use to
build applications. It includes core functionality such as file handling, database access (via
[Link] and Entity Framework), XML parsing, collections, and more. This reduces the
need for writing low-level code from scratch.
3. Languages and Compilers
.NET supports multiple languages that compile to a common intermediate language (CIL),
also known as Microsoft Intermediate Language (MSIL). Popular .NET languages include:
• C# – the most commonly used language for .NET development.
• [Link] – more user-friendly, often used in legacy systems.
• F# – functional-first language for mathematical and analytical applications.
These compilers translate high-level code into CIL, which is then compiled into native
machine code by the Just-In-Time (JIT) compiler during execution.
4. Common Type System (CTS)
The CTS defines how types are declared and used in .NET, ensuring that objects written in
different .NET languages can interact with each other. For example, a class written in C# can
inherit from a class written in [Link].
5. Common Language Specification (CLS)
The CLS is a set of rules and standards that all .NET languages must follow to ensure
interoperability. It ensures that features used in one language are available to other languages
that target the .NET runtime.
By Daud Anjum 8
ENTERPRISE SYSTEMS SI-342
6. Application Models
.NET provides different application models depending on the type of application:
• [Link] Core for web applications and APIs
• Windows Forms and WPF for Windows desktop applications
• Blazor for interactive web UIs using C#
• Xamarin / .NET MAUI for cross-platform mobile apps
• Azure Functions for serverless computing
7. Entity Framework (EF)
EF is an Object-Relational Mapping (ORM) framework that simplifies database interactions.
It allows developers to work with databases using C# or [Link] objects instead of SQL
queries. This is particularly useful in enterprise applications where data access is a major
concern.
8. NuGet Package Manager
NuGet is the official package manager for .NET, allowing developers to easily add libraries,
tools, and frameworks to their projects. It supports both Microsoft and third-party packages,
helping enterprises extend functionality quickly and reliably.
Advantages of .NET in ES
• Cross-platform development with .NET allows organizations to deploy applications on
different operating systems using a single codebase.
• Strong tooling support via Visual Studio and Visual Studio Code makes development more
productive.
• Security features such as code access security, role-based authentication, and built-in
encryption help protect enterprise data.
• Integration capabilities with Azure, Microsoft SQL Server, Active Directory, and other
enterprise tools streamline enterprise system development.
• Performance and scalability of .NET Core and later versions enable applications to handle
high workloads efficiently.
Concept of MSIL, CLR, CLS, CTS
When developing enterprise applications with the .NET Framework, several core concepts work
together to ensure cross-language compatibility, performance, security, and maintainability. These
include MSIL (Microsoft Intermediate Language), CLR (Common Language Runtime), CLS
(Common Language Specification), and CTS (Common Type System). Understanding these
concepts is essential for developers working in EAD as they provide the foundation for building
reliable and scalable applications across different programming languages in the .NET ecosystem.
By Daud Anjum 9
ENTERPRISE SYSTEMS SI-342
1. Microsoft Intermediate Language (MSIL)
MSIL, also known as CIL (Common Intermediate Language), is a low-level, CPU-
independent set of instructions that is generated when a .NET-compatible language (such as
C#, [Link], or F#) is compiled. Instead of converting source code directly to machine code,
.NET compilers first translate the code into MSIL.
This intermediate language includes instructions for memory management, exception
handling, control flow, arithmetic operations, and more. MSIL is platform-agnostic, which
means the same compiled code can run on any system where the .NET runtime is available.
At runtime, MSIL is converted into native machine code using the Just-In-Time (JIT)
compiler, allowing it to be executed by the host operating system. This two-step compilation
process enables the portability and flexibility of .NET applications, especially useful in
enterprise environments where systems may run on different platforms.
2. Common Language Runtime (CLR)
The Common Language Runtime (CLR) is the execution engine of the .NET framework. It is
responsible for running applications compiled into MSIL by converting them into native
machine code and managing the runtime behaviour of the application.
The CLR provides essential services such as:
• Memory management and garbage collection: Automatically manages memory allocation
and deallocation
• Type safety and security: Prevents unauthorized access to memory and enforces access
rules
• Exception handling: Offers a consistent model for detecting and responding to runtime
errors
• Thread management: Manages concurrent execution of code
• Code verification: Ensures MSIL code conforms to type safety rules before execution
In ES, the CLR ensures applications are robust, secure, and efficient, reducing common issues
like memory leaks or crashes. It also allows cross-language interoperability, meaning that code
written in different .NET languages can work together within a single application.
3. Common Language Specification (CLS)
The Common Language Specification (CLS) is a set of rules and standards that all .NET
languages must follow to ensure interoperability between them. Since .NET supports multiple
languages, the CLS defines a subset of features that are common across all languages in the
.NET family.
For example, some languages support unsigned integers while others do not. CLS rules restrict
the use of such types if developers want their code to be usable by other CLS-compliant
languages. This promotes language interoperability, allowing enterprise teams to use different
programming languages for different parts of an application without breaking compatibility.
In practice, if a library or class is CLS-compliant, it can be safely used in any .NET language,
making CLS essential for reusable enterprise components and framework development.
By Daud Anjum 10
ENTERPRISE SYSTEMS SI-342
4. Common Type System (CTS)
The Common Type System (CTS) defines how data types are declared and used in the .NET
environment. It ensures that objects written in different languages share a common
understanding of data types, which is crucial for language interoperability.
CTS categorized types into:
• Value types: Stored directly in memory (e.g., int, float, bool)
• Reference types: Store a reference to the actual data (e.g., class, interface, array)
For example, an int in C# is understood the same way in [Link] or F#, because it maps to
the same CTS type (System.Int32). This consistency allows objects and methods to be passed
across different languages without type mismatch errors.
CTS also defines rules for type inheritance, type visibility, and access levels. For enterprise
applications, CTS ensures a standardized programming model where developers can write
and consume components across different teams and languages, improving maintainability
and scalability.
2. Introduction to .NET Framework
The .NET Framework, developed by Microsoft, is a comprehensive and consistent programming
platform primarily used for building and running applications on Windows. It is one of the
foundational technologies in enterprise systems, providing the necessary infrastructure for
developing robust, secure, and scalable applications that can handle the demands of modern
business environments.
Introduced in the early 2000s, the .NET Framework was designed to overcome the limitations of
traditional Windows development by offering a unified environment where developers could write
code using multiple languages (like C#, [Link], and F#) and share components seamlessly. It
supports object-oriented programming, simplifies application deployment, and offers built-in
security features, making it ideal for large-scale enterprise solutions.
One of the key strengths of the .NET Framework in enterprise systems is its support for multiple
application models, including Windows Forms, [Link] (for web applications), and WPF
(Windows Presentation Foundation) for advanced desktop UIs. It also integrates well with
Microsoft technologies like SQL Server, Active Directory, and SharePoint, making it a preferred
choice for enterprise-level applications.
Managed and Unmanaged Code
Understanding the difference between managed and unmanaged code is essential. These terms
refer to how the code is executed and whether or not it is managed by the .NET runtime
environment known as the CLR.
By Daud Anjum 11
ENTERPRISE SYSTEMS SI-342
Managed Code
It is the code that is written in a .NET-compatible language such as C#, [Link], or F# and is
executed under the supervision of the CLR. When this code is compiled, it is first converted into
MSIL and then into native machine code by the JIT compiler at runtime. The CLR handles several
critical services for managed code, including:
• Memory management
• Type safety
• Exception handling
• Security enforcement
• Cross-language integration
Because of these features, managed code is generally safer, more robust, and easier to maintain –
especially important in ES where reliability and security are top priorities. The CLR abstracts
many low-level programming tasks, which allows developers to focus more on business logic and
less on system-level operations.
For example, a web application built using [Link] is considered managed code because it runs
inside the CLR environment. It benefits from automatic memory cleanup, strong exception
handling, and secure execution, making it ideal for enterprise-grade solutions.
Unmanaged Code
Unmanaged code refers to programs that are executed directly by the operating system, outside
the control of the CLR. This includes applications written in languages like C or C++, where the
developer is responsible for managing memory allocation and deallocation, error handling, and
system-level interactions manually.
Since unmanaged code does not run within the CLR, it does not benefit from automatic garbage
collection, type checking, or the other runtime services provided by .NET. However, it can offer
greater performance and low-level system access, which may be necessary for certain components
such as hardware drivers, legacy systems, or performance-critical applications.
In ES, it is common to integrate unmanaged code with .NET applications, especially when
interacting with existing C/C++ libraries or legacy codebases. This is done through Interop
Services such as Platform Invocation Services (P/Invoke) or COM Interoperability, allowing
managed .NET applications to call functions in unmanaged libraries.
Importance in ES
In EAD, understanding the distinction between managed and unmanaged code is crucial because
both types often coexist. While managed code is typically preferred for new development due to
its productivity and safety, unmanaged code is still widely used in system-level programming or
for integrating with older systems.
For example, a .NET-based enterprise application may use managed code for its user interface
and business logic, but rely on unmanaged code for specialized tasks like data encryption or
communication with legacy hardware.
By Daud Anjum 12
ENTERPRISE SYSTEMS SI-342
.NET Assembly
A .NET Assembly is a fundamental building block of .NET applications. It is a compiled code
library used for deployment, versioning, and execution in the .NET Framework. Assemblies are
created when source code written in a .NET-supported language (like C# or [Link]) is compiled
by the respective compiler. They contain not just the compiled code but also metadata and other
essential information that the CLR uses to execute and manage the application.
Types of Assemblies
There are two main types of assemblies:
1) Private Assemblies: Used by a single application and stored in that application’s directory.
These are the most common type in enterprise applications.
2) Shared (or Global) Assemblies: Stored in the Global Assembly Cache (GAC) and can be used
by multiple applications. These require a strong name (a unique identity) and are typically used
for shared libraries in large ES.
Contents of an Assembly
An assembly is more than just compiled code. It typically includes:
• MSIL code: The intermediate, CPU-independent code generated during compilation.
• Metadata: Describes the assembly’s types, version, culture, and other identity details. It allows
the CLR to understand the structure and functionality of the assembly.
• Manifest: Contains information about the assembly itself, including version number,
referenced assemblies, and security permissions.
• Resources: Embedded files like images, strings, XML, or localization data.
All of these components are packaged into a single file with a .dll (Dynamic Link Library) or .exe
(Executable) extension.
Role is Enterprise Systems
In ES, assemblies enable modular and scalable application design. Developers can separate an
application into logical components such as data access, business logic, and UI layers, each
compiled into separate assemblies. This separation supports reusability, code sharing, and version
control, which are essential for large teams and long-term projects.
Assemblies also enhance security and deployment. By using strong-named assemblies and storing
them in the GAC, enterprises can enforce versioning policies and prevent unauthorized or
mismatched library usage. This is particularly valuable when multiple applications rely on the
same shared components.
Assembly Versioning
Versioning is built into the .NET assembly system. Each assembly has a version number
([Link]), and the CLR uses this information to bind the correct version of
By Daud Anjum 13
ENTERPRISE SYSTEMS SI-342
an assembly at runtime. In enterprise environments, this allows for side-by-side execution, where
different versions of the same library can run on the same system without conflict.
Introduction of C# Fundamentals
C# (pronounced as “C-sharp”) is a modern, object-oriented programming language developed by
Microsoft as part of its .NET initiative. It is one of the primary languages used in the .NET
Framework and is widely adopted in ES for building robust, secure, and scalable applications. C#
combines the simplicity of earlier languages like C and C++ with the powerful features of modern
languages such as Java and Python, making it both easy to learn and powerful for large-scale
development.
Basic Syntax and Structure
A C# program starts with a 𝑀𝑎𝑖𝑛() method, which acts as the entry point of the application. The
syntax is clean and consistent, using curly braces {} to define blocks of code. For example:
𝑢𝑠𝑖𝑛𝑔 𝑆𝑦𝑠𝑡𝑒𝑚;
𝑐𝑙𝑎𝑠𝑠 𝑃𝑟𝑜𝑔𝑟𝑎𝑚
{
𝑠𝑡𝑎𝑡𝑖𝑐 𝑣𝑜𝑖𝑑 𝑀𝑎𝑖𝑛()
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝐻𝑒𝑙𝑙𝑜, 𝐸𝑛𝑡𝑒𝑟𝑝𝑟𝑖𝑠𝑒 𝑆𝑦𝑠𝑡𝑒𝑚𝑠! ");
}
}
In this example, 𝑢𝑠𝑖𝑛𝑔 𝑆𝑦𝑠𝑡𝑒𝑚; allows access to the .NET System namespace, and
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒() is used to print output to the console. The basic structure of a C# program
includes namespaces, classes, methods, and statements, all of which follow clear and intuitive
rules.
Data Types
C# supports a wide range of data types, including value types like 𝑖𝑛𝑡, 𝑑𝑜𝑢𝑏𝑙𝑒, 𝑏𝑜𝑜𝑙, and
reference types like 𝑠𝑡𝑟𝑖𝑛𝑔, 𝑜𝑏𝑗𝑒𝑐𝑡, and custom classes. Variables are declared with a type and
can be assigned values:
𝑖𝑛𝑡 𝑎𝑔𝑒 = 23;
𝑠𝑡𝑟𝑖𝑛𝑔 𝑛𝑎𝑚𝑒 = "𝐸𝑛𝑡𝑒𝑟𝑝𝑟𝑖𝑠𝑒 𝐷𝑒𝑣𝑒𝑙𝑜𝑝𝑒𝑟";
The type safety of C# ensures that errors are caught at compile time, which is a significant
advantage in ES where data integrity and consistency are critical.
Control Structures
C# includes standard control flow statements such as:
• 𝑖𝑓, 𝑒𝑙𝑠𝑒 𝑖𝑓, 𝑒𝑙𝑠𝑒
By Daud Anjum 14
ENTERPRISE SYSTEMS SI-342
• 𝑠𝑤𝑖𝑡𝑐ℎ
• 𝑓𝑜𝑟, 𝑤ℎ𝑖𝑙𝑒, 𝑑𝑜 − 𝑤ℎ𝑖𝑙𝑒 loops
• 𝑓𝑜𝑟𝑒𝑎𝑐ℎ for iterating over collections
These control structures help manage program logic and decision-making efficiently:
𝑖𝑓 (𝑎𝑔𝑒 > 18)
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝐸𝑙𝑖𝑔𝑖𝑏𝑙𝑒 𝑓𝑜𝑟 𝑎𝑐𝑐𝑒𝑠𝑠. ");
}
Methods are Parameters
Methods help break down code into manageable pieces. C# supports method overloading and
both value and reference parameter passing.
𝑣𝑜𝑖𝑑 𝐺𝑟𝑒𝑒𝑡(𝑠𝑡𝑟𝑖𝑛𝑔 𝑢𝑠𝑒𝑟)
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝑊𝑒𝑙𝑐𝑜𝑚𝑒, " + 𝑢𝑠𝑒𝑟);
}
Object-Oriented Programming (OOP)
C# is fully object-oriented, supporting classes, inheritance, encapsulation, abstraction, and
polymorphism. These principles allow developers to build modular, reusable, and maintainable
code – ideal for complex enterprise applications.
𝑐𝑙𝑎𝑠𝑠 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑛𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
𝑝𝑢𝑏𝑙𝑖𝑐 𝑣𝑜𝑖𝑑 𝐷𝑖𝑠𝑝𝑙𝑎𝑦()
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒: " + 𝑁𝑎𝑚𝑒);
}
}
With object-oriented design, business logic can be cleanly separated into classes, making the code
easier to manage in large-scale systems.
Properties and Auto-Implemented Properties
Properties in C# provide controlled access to class fields. Auto-implemented properties reduce
boilerplate:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
Access Modifiers
C# uses access modifiers to control visibility:
• public – accessible anywhere
By Daud Anjum 15
ENTERPRISE SYSTEMS SI-342
• private – accessible only within the class
• protected – accessible in the class and derived class
• internal – accessible within the same assembly
These are important in enterprise development for creating secure and modular applications.
Static vs Instance Members
Static members belong to the class, not the object, and can be called without creating as instance:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑎𝑡𝑖𝑐 𝑖𝑛𝑡 𝐶𝑜𝑢𝑛𝑡 = 0;
In contrast, instance members require an object to access them.
Arrays and Collections
Arrays store fixed-size sequences of elements:
𝑖𝑛𝑡[ ] 𝑠𝑐𝑜𝑟𝑒𝑠 = { 90, 85, 78 };
Collections such as 𝐿𝑖𝑠𝑡 < 𝑇 > offer dynamic and flexible data handling:
𝐿𝑖𝑠𝑡 < 𝑠𝑡𝑟𝑖𝑛𝑔 > 𝑛𝑎𝑚𝑒𝑠 = 𝑛𝑒𝑤 𝐿𝑖𝑠𝑡 < 𝑠𝑡𝑟𝑖𝑛𝑔 > ();
𝑛𝑎𝑚𝑒𝑠. 𝐴𝑑𝑑("𝐴𝑙𝑖𝑐𝑒");
Interfaces and Abstract Classes
Interfaces define contracts with method signatures only, while abstract classes provide partial
implementation. These are used to design reusable and extensible enterprise applications.
𝑖𝑛𝑡𝑒𝑟𝑓𝑎𝑐𝑒 𝐼𝑅𝑒𝑝𝑜𝑟𝑡
{
𝑣𝑜𝑖𝑑 𝐺𝑒𝑛𝑒𝑟𝑎𝑡𝑒();
}
Exception Handling
C# includes robust exception handling using 𝑡𝑟𝑦, 𝑐𝑎𝑡𝑐ℎ, 𝑓𝑖𝑛𝑎𝑙𝑙𝑦, and 𝑡ℎ𝑟𝑜𝑤 keywords. This is
essential in ES where reliability and error recovery are critical.
𝑡𝑟𝑦
{
𝑖𝑛𝑡 𝑟𝑒𝑠𝑢𝑙𝑡 = 10 / 0;
}
𝑐𝑎𝑡𝑐ℎ (𝐷𝑖𝑣𝑖𝑑𝑒𝐵𝑦𝑍𝑒𝑟𝑜𝐸𝑥𝑐𝑒𝑝𝑡𝑖𝑜𝑛 𝑒𝑥)
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝐸𝑟𝑟𝑜𝑟: " + 𝑒𝑥. 𝑀𝑒𝑠𝑠𝑎𝑔𝑒);
}
Namespaces and Using Directives
Namespaces help organize code logically, and the 𝑢𝑠𝑖𝑛𝑔 directive allows access to those
namespaces:
𝑢𝑠𝑖𝑛𝑔 𝑆𝑦𝑠𝑡𝑒𝑚. 𝐶𝑜𝑙𝑙𝑒𝑐𝑡𝑖𝑜𝑛. 𝐺𝑒𝑛𝑒𝑟𝑖𝑐;
By Daud Anjum 16
ENTERPRISE SYSTEMS SI-342
LINQ Basics
LINQ (Language Integrated Query) is used for querying collections in a readable way:
𝑣𝑎𝑟 ℎ𝑖𝑔ℎ𝑆𝑐𝑜𝑟𝑒𝑠 = 𝑠𝑐𝑜𝑟𝑒𝑠. 𝑊ℎ𝑒𝑟𝑒(𝑠 => 𝑠 > 80). 𝑇𝑜𝐿𝑖𝑠𝑡();
LINQ simplifies data manipulation, which is a common requirement in enterprise applications.
Boxing and Unboxing
In the .NET Framework, Boxing and Unboxing are mechanisms that enable conversion between
value types and reference types. These concepts are central to the type system of .NET and are
part of the Common Type System (CTS). Understanding them is important for working with
objects, collections, and data conversions in enterprise-level .NET applications.
Boxing
It is the process of converting a value types (such as 𝑖𝑛𝑡, 𝑓𝑙𝑜𝑎𝑡, 𝑏𝑜𝑜𝑙, etc.) into a reference types
(specifically an 𝑜𝑏𝑗𝑒𝑐𝑡). Since value types are stored on the stack, and reference types are stored
on the heap, boxing wraps the value types inside a reference type, allowing it to be treated as an
object.
Example:
𝑖𝑛𝑡 𝑛𝑢𝑚𝑏𝑒𝑟 = 42;
𝑜𝑏𝑗𝑒𝑐𝑡 𝑜𝑏𝑗 = 𝑛𝑢𝑚𝑏𝑒𝑟; // 𝐵𝑜𝑥𝑖𝑛𝑔
Here, the integer 𝑛𝑢𝑚𝑏𝑒𝑟 is boxed into an object. The runtime creates a new object on the heap
and copies the value of 𝑛𝑢𝑚𝑏𝑒𝑟 into it. Boxing is implicit, meaning it happens automatically
when a value type is assigned to an object or an interface it implements.
Use Case:
Boxing is commonly used when value types need to be stored in data structures that store objects,
such as 𝐴𝑟𝑟𝑎𝑦𝐿𝑖𝑠𝑡 (non-generic collections).
Unboxing
Unboxing is the reverse process – converting an object (reference type) back into a value type.
Since the value was boxed earlier, unboxing extracts the value from the object and assigns it back
to the original value type.
Example:
𝑜𝑏𝑗𝑒𝑐𝑡 𝑜𝑏𝑗 = 42; // 𝐵𝑜𝑥𝑖𝑛𝑔
𝑖𝑛𝑡 𝑛𝑢𝑚 = (𝑖𝑛𝑡)𝑜𝑏𝑗; // 𝑈𝑛𝑏𝑜𝑥𝑖𝑛𝑔
Unboxing requires explicit casting. If you attempt to unbox an object to the wrong type, a
𝐼𝑛𝑣𝑎𝑙𝑖𝑑𝐶𝑎𝑠𝑡𝐸𝑥𝑐𝑒𝑝𝑡𝑖𝑜𝑛 will be thrown.
Important Points:
• The object must originally be a boxed value of the same value type.
• Unboxing involves both type checking and memory extraction, so it is slightly slower than
direct value-type operations.
By Daud Anjum 17
ENTERPRISE SYSTEMS SI-342
Performance Considerations
While boxing and unboxing provide flexibility, especially when dealing with collections and
object-based APIs, they come with a performance cost due to memory allocation and type
conversion. In large-scale enterprise applications, frequent boxing and unboxing can lead to
increased heap usage and garbage collection pressure, reducing performance.
To address this, generic collections like 𝐿𝑖𝑠𝑡 < 𝑇 > in the 𝑆𝑦𝑠𝑡𝑒𝑚. 𝐶𝑜𝑙𝑙𝑒𝑐𝑡𝑖𝑜𝑛𝑠. 𝐺𝑒𝑛𝑒𝑟𝑖𝑐
namespace were introduced in .NET 2.0. Generics eliminate the need for boxing/unboxing by
allowing you to use value types directly.
Example using Generics (no boxing):
𝐿𝑖𝑠𝑡 < 𝑖𝑛𝑡 > 𝑛𝑢𝑚𝑏𝑒𝑟𝑠 = 𝑛𝑒𝑤 𝐿𝑖𝑠𝑡 < 𝑖𝑛𝑡 > (); // 𝑁𝑜 𝑏𝑜𝑥𝑖𝑛𝑔
𝑛𝑢𝑚𝑏𝑒𝑟𝑠. 𝐴𝑑𝑑(42);
Implementing Multi-tier Architecture
Multi-tier architecture (also known as n-tier architecture) is a software design pattern used in the
development of enterprise applications to promote modularity, scalability, maintainability, and
separation of concerns. In .NET Framework-based systems, this architecture typically divides an
application into logical layers or tiers, where each tier is responsible for a specific task. These
tiers communicate with each other while remaining independent in their internal implementation.
Common Tiers in Multi-Tier Architecture
1) Presentation Tier (UI Layer)
This is the topmost layer that interacts with the user. It includes forms, web pages, or mobile
interfaces that allow users to input and view data. In .NET, this could be implemented using
[Link] Web Forms, MVC, or Windows Forms/WPF.
2) Business Logic Tier (BLL)
Also called the middle layer, it contains all the core processing logic, business rules,
validations, and algorithms. It acts as a bridge between the presentation and data layers. This
layer ensures that data sent to the user or database is valid and secure.
3) Data Access Tier (DAL)
This layer handles direct communication with the database. It performs CRUD operations
(Create, Read, Update, Delete) and maps data between database and business objects. In .NET,
[Link], Entity Framework, or Dapper are commonly used technologies for data access.
4) Database Tier
Sometimes considered separate from DAL, this tier refers to the actual database system like
SQL Server, Oracle, or MySQL. It stores all application data and enforces data integrity at the
storage level.
By Daud Anjum 18
ENTERPRISE SYSTEMS SI-342
Benefits of Multi-Tier Architecture in .NET
• Separation of Concerns: Each layer has a distinct responsibility, making the codebase easier
to manage and debug.
• Scalability: Individual layers can be scaled independently depending on demand. For example,
a web farm can scale the UI layer, while the database can be scaled with replication.
• Reusability: Business logic and data access code can be reused across different front-end
applications (web, desktop, API).
• Maintainability: Changes in one layer (e.g., switching from SQL Server to Oracle) typically
do not affect other layers if interfaces are properly defined.
Implementation in .NET Framework
To implement multi-tier architecture in a .NET application, developers typically follow this
structure:
1) Create Separate Projects for Each Layer
• 𝑀𝑦𝐴𝑝𝑝. 𝑈𝐼 – Presentation Layer
• 𝑀𝑦𝐴𝑝𝑝. 𝐷𝐿𝐿 – Business Logic Layer
• 𝑀𝑦𝐴𝑝𝑝. 𝐷𝐴𝐿 – Data Access Layer
• 𝑀𝑦𝐴𝑝𝑝. 𝑀𝑜𝑑𝑒𝑙𝑠 – Shared data models/entities
This structure is especially useful in large-scale enterprise applications using [Link] MVC
or Web API.
2) Communication Between Layers
• The UI Layer calls public methods in the Business Logic Layer.
• The Business Logic Layer calls methods in the Data Access Layer to retrieve or update
data.
• Data is passed between layers using DTOs (Data Transfer Objects) or entity models.
Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑆𝑒𝑟𝑣𝑖𝑐𝑒
{
𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐷𝐴𝐿 𝑑𝑎𝑙 = 𝑛𝑒𝑤 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐷𝐴𝐿();
𝑝𝑢𝑏𝑙𝑖𝑐 𝐸𝑚𝑝𝑜𝑙𝑦𝑒𝑒 𝐺𝑒𝑡𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒(𝑖𝑛𝑡 𝑖𝑑)
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝑑𝑎𝑙. 𝐺𝑒𝑡𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐵𝑦𝐼𝑑 (𝑖𝑑 );
}
}
Best Practices
• Use interfaces to define contracts between layers (Dependency Injection is commonly used).
• Avoid direct access from the UI to the DAL.
• Handle exceptions and validations within the appropriate layers (business rules in BLL, UI
feedback in Presentation Layer).
By Daud Anjum 19
ENTERPRISE SYSTEMS SI-342
• Follow SOLID principles and design patterns (e.g., Repository, Unit of Work) for better
structure.
3. Introduction to [Link]
[Link] (ActiveX Data Objects for .NET) is a core component of the .NET Framework that
provides a set of classes for working with data sources such as databases, XML files, and in-
memory data. It enables developers to connect to databases, execute commands, retrieve results,
and manipulate data in a disconnected or connected manner.
[Link] plays a crucial role in enterprise system development, especially for building data-
driven applications where efficient, reliable, and secure database access is essential.
Key Features of [Link]
1) Disconnected Architecture
[Link] allows applications to fetch data from the database, work with it locally (in-
memory), and then update the database later. This reduces the load on the database server and
improves scalability.
2) Data Providers
[Link] uses data providers as a bridge between the application and the data source. The
main components of a data provider include:
• Connection – establishes a link to the database
• Command – executes SQL queries or stored procedures
• DataReader – retrieves read-only, forward-only data
• DataAdapter – fills data into DataSet or DataTable and updates the database
• DataSet – an in-memory representation of data (can contain multiple tables)
3) Support for Multiple Databases
[Link] supports various data providers for different databases:
• SqlClient – for Microsoft SQL Server
• OleDb – for Access and other OLE DB-compatible sources
• Odbc – for databases via ODBC drivers
• OracleClient – for Oracle databases (older versions)
Connected vs Disconnected Models
• Connected Model
Uses SqlConnection and SqlDataReader. The connection to the database remains open while
reading data. It’s fast and efficient but ties up server resources.
By Daud Anjum 20
ENTERPRISE SYSTEMS SI-342
𝑆𝑞𝑙𝐶𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛 𝑐𝑜𝑛𝑛 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐶𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛(𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛𝑆𝑡𝑟𝑖𝑛𝑔);
𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑 𝑐𝑚𝑑 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑("𝑆𝐸𝐿𝐸𝐶𝑇 ∗ 𝐹𝑅𝑂𝑀 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠", 𝑐𝑜𝑛𝑛);
𝑐𝑜𝑛𝑛. 𝑂𝑝𝑒𝑛();
𝑆𝑞𝑙𝐷𝑎𝑡𝑎𝑅𝑒𝑎𝑑𝑒𝑟 𝑟𝑒𝑎𝑑𝑒𝑟 = 𝑐𝑚𝑑. 𝐸𝑥𝑒𝑐𝑢𝑡𝑒𝑅𝑒𝑎𝑑𝑒𝑟();
𝑤ℎ𝑖𝑙𝑒 (𝑟𝑒𝑎𝑑𝑒𝑟. 𝑅𝑒𝑎𝑑())
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒(𝑟𝑒𝑎𝑑𝑒𝑟["𝑁𝑎𝑚𝑒"]);
}
𝑐𝑜𝑛𝑛. 𝐶𝑙𝑜𝑠𝑒();
• Disconnected Model
Uses DataSet and DataAdapter. Data is fetched and stored in memory, and the connection is
closed. Changes can later by synchronized with the database.
𝑆𝑞𝑙𝐷𝑎𝑡𝑎𝐴𝑑𝑎𝑝𝑡𝑒𝑟 𝑎𝑑𝑎𝑝𝑡𝑒𝑟 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐷𝑎𝑡𝑎𝐴𝑑𝑎𝑝𝑡𝑒𝑟("𝑆𝐸𝐿𝐸𝐶𝑇 ∗
𝐹𝑅𝑂𝑀 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠", 𝑐𝑜𝑛𝑛);
𝐷𝑎𝑡𝑎𝑆𝑒𝑡 𝑑𝑠 = 𝑛𝑒𝑤 𝐷𝑎𝑡𝑎𝑆𝑒𝑡();
𝑎𝑑𝑎𝑝𝑡𝑒𝑟. 𝐹𝑖𝑙𝑙(𝑑𝑠, "𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠");
Advantages of [Link] in ES
• Efficient Data Management: Enables scalable and fast access with the disconnected model.
• Rich Object Model: Offers powerful classes to represent and manage complex data
relationships.
• Secure and Reliable: Supports parameterized queries to prevent SQL injection and manages
connections efficiently.
• Integration with XML: Supports reading from and writing to XML, making data exchange
across platforms easier.
• Customizable and Extensible: Developers can build custom data providers or use third-party
libraries.
SQL Injection
SQL Injection is a type of security vulnerability that occurs when an attacker is able to insert or
“inject” malicious SQL statements into an input field, which are then executed by the application’s
database engine. This flaw is commonly found in applications that construct SQL queries using
unvalidated user inputs. It is one of the most dangerous and commonly exploited vulnerabilities,
especially in web-based ES.
In [Link] applications, SQL Injection can occur if developers concatenate user inputs directly
into SQL statements without proper validation or parameterization.
How SQL Injection Works
Consider a simple login form where the user enters a username and password. The application
might use the following SQL query:
By Daud Anjum 21
ENTERPRISE SYSTEMS SI-342
𝑠𝑡𝑟𝑖𝑛𝑔 𝑞𝑢𝑒𝑟𝑦 = "𝑆𝐸𝐿𝐸𝐶𝑇 ∗ 𝐹𝑅𝑂𝑀 𝑈𝑠𝑒𝑟𝑠 𝑊𝐻𝐸𝑅𝐸 𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 = ′" + 𝑢𝑠𝑒𝑟𝑛𝑎𝑚𝑒 +
"′ 𝐴𝑁𝐷 𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑 = ′" + 𝑝𝑎𝑠𝑠𝑤𝑜𝑟𝑑 + "′";
If the user enters the following input:
• Username: 𝑎𝑑𝑚𝑖𝑛′ − −
• Password: 𝑎𝑛𝑦𝑡ℎ𝑖𝑛𝑔
The query becomes:
𝑆𝐸𝐿𝐸𝐶𝑇 ∗ 𝐹𝑅𝑂𝑀 𝑈𝑠𝑒𝑟𝑠 𝑊𝐻𝐸𝑅𝐸 𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 = ′𝑎𝑑𝑚𝑖𝑛′ − −′ 𝐴𝑁𝐷 𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑 =
′𝑎𝑛𝑦𝑡ℎ𝑖𝑛𝑔′
The − − signifies a SQL command, so everything after it is ignored. The query essentially
becomes:
𝑆𝐸𝐿𝐸𝐶𝑇 ∗ 𝐹𝑅𝑂𝑀 𝑈𝑠𝑒𝑟𝑠 𝑊𝐻𝐸𝑅𝐸 𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 = ′𝑎𝑑𝑚𝑖𝑛′
This could allow the attacker to bypass authentication, gaining unauthorized access to the system.
Risks and Consequences
SQL Injection can lead to:
1) Unauthorized access to sensitive data (user credentials, financial records)
2) Data modification or deletion
3) Database corruption
4) Privilege escalation (gaining administrative access)
5) Denial of Service (DoS) y exploiting database calls
6) Full system compromise in severe cases
These consequences can be especially devastating in ES, which often manage large volumes of
critical business data.
Preventing SQL Injection in [Link]
[Link] provides safe and effective ways to prevent SQL Injection, primarily through
parameterized queries and stored procedures.
1) Using Parameterized Queries
[Link] supports the use of parameters in SQL commands. Parameters ensure that user input
is treated as data, not executable code.
Example:
𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑 𝑐𝑚𝑑 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑("𝑆𝐸𝐿𝐸𝐶𝑇 ∗
𝐹𝑅𝑂𝑀 𝑈𝑠𝑒𝑟𝑠 𝑊𝐻𝐸𝑅𝐸 𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 = @𝑢𝑠𝑒𝑟𝑛𝑎𝑚𝑒 𝐴𝑁𝐷 𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑 =
@𝑝𝑎𝑠𝑠𝑤𝑜𝑟𝑑", 𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛);
𝑐𝑚𝑑. 𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑠. 𝐴𝑑𝑑𝑊𝑖𝑡ℎ𝑉𝑎𝑙𝑢𝑒("@𝑢𝑠𝑒𝑟𝑛𝑎𝑚𝑒", 𝑢𝑠𝑒𝑟𝑛𝑎𝑚𝑒);
𝑐𝑚𝑑. 𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑠. 𝐴𝑑𝑑𝑊𝑖𝑡ℎ𝑉𝑎𝑙𝑢𝑒("@𝑝𝑎𝑠𝑠𝑤𝑜𝑟𝑑", 𝑝𝑎𝑠𝑠𝑤𝑜𝑟𝑑);
This ensures that even if a user tries to inject SQL commands, they will be treated as string
values, not code.
2) Using Stored Procedures
Stored procedures are precompiled SQL code stored in the database. They also support
parameters and can reduce the risk of injection.
By Daud Anjum 22
ENTERPRISE SYSTEMS SI-342
Example:
𝐶𝑅𝐸𝐴𝑇𝐸 𝑃𝑅𝑂𝐶𝐸𝐷𝑈𝑅𝐸 𝑉𝑎𝑙𝑖𝑑𝑎𝑡𝑒𝑈𝑠𝑒𝑟
@𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 𝑁𝑉𝐴𝑅𝐶𝐻𝐴𝑅 (50),
@𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑 𝑁𝑉𝐴𝑅𝐶𝐻𝐴𝑅(50)
𝐴𝑆
𝐵𝐸𝐺𝐼𝑁
𝑆𝐸𝐿𝐸𝐶𝑇 ∗ 𝐹𝑅𝑂𝑀 𝑈𝑠𝑒𝑟𝑠 𝑊𝐻𝐸𝑅𝐸 𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 = @𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 𝐴𝑁𝐷 𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑 =
@𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑
𝐸𝑁𝐷
Calling in [Link]:
𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑 𝑐𝑚𝑑 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑("𝑉𝑎𝑙𝑖𝑑𝑎𝑡𝑒𝑈𝑠𝑒𝑟", 𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛);
𝑐𝑚𝑑. 𝐶𝑜𝑚𝑚𝑎𝑛𝑑𝑇𝑦𝑝𝑒 = 𝐶𝑜𝑚𝑚𝑎𝑛𝑑𝑇𝑦𝑝𝑒. 𝑆𝑡𝑜𝑟𝑒𝑑𝑃𝑟𝑜𝑐𝑒𝑑𝑢𝑟𝑒;
𝑐𝑚𝑑. 𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑠. 𝐴𝑑𝑑𝑊𝑖𝑡ℎ𝑉𝑎𝑙𝑢𝑒("@𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒", 𝑢𝑠𝑒𝑟𝑛𝑎𝑚𝑒);
𝑐𝑚𝑑. 𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑠. 𝐴𝑑𝑑𝑊𝑖𝑡ℎ𝑉𝑎𝑙𝑢𝑒("@𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑", 𝑝𝑎𝑠𝑠𝑤𝑜𝑟𝑑);
Additional Best Practices
• Input Validation: Sanitize and validate all user inputs to restrict unexpected data formats.
• Least Privilege Principle: Use database accounts with the minimum required privileges.
• Avoid Dynamic SQL: Don’t construct SQL queries using string concatenation.
• Use ORM Tools: Tools like Entity Framework reduce direct SQL use and help prevent
injection.
• Regular Security Testing: Perform code reviews and penetration testing to identify
vulnerabilities.
Parameterized Queries
Parameterized queries are a key feature in [Link] used to safely execute SQL commands with
dynamic input values. Instead of directly embedding user input into SQL statements (which can
be dangerous and lead to SQL Injection), parameterized queries treat user input as parameters,
thereby ensuring that the data is passed securely and efficiently. This is essential for building
robust and secure ES where data integrity and protection are critical.
Why Use Parameterized Queries?
In traditional SQL queries, values are often concatenated directly into the query string:
𝑠𝑡𝑟𝑖𝑛𝑔 𝑞𝑢𝑒𝑟𝑦 = "𝑆𝐸𝐿𝐸𝐶𝑇 ∗ 𝐹𝑅𝑂𝑀 𝑈𝑠𝑒𝑟𝑠 𝑊𝐻𝐸𝑅𝐸 𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 = ′" + 𝑢𝑠𝑒𝑟𝑛𝑎𝑚𝑒 +
"′ 𝐴𝑁𝐷 𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑 = ′" + 𝑝𝑎𝑠𝑠𝑤𝑜𝑟𝑑 + "′";
This approach is vulnerable to SQL Injection. To prevent this, [Link] allows the use of
placeholders in SQL statements, which are then safely substituted with actual values using
parameters.
Parameterized queries offer several benefits:
• Security: Prevent SQL injection attacks
• Efficiency: Improve performance due to query plan reuse by the database engine
By Daud Anjum 23
ENTERPRISE SYSTEMS SI-342
• Readability and Maintainability: Easier to manage complex queries
• Data Type Safety: Parameters automatically handle type conversions
Syntax of Parameterized Queries
To implement parameterized queries in [Link], follow these steps:
1) Create a SQL command with placeholders:
𝑠𝑡𝑟𝑖𝑛𝑔 𝑞𝑢𝑒𝑟𝑦 = "𝑆𝐸𝐿𝐸𝐶𝑇 ∗ 𝐹𝑅𝑂𝑀 𝑈𝑠𝑒𝑟𝑠 𝑊𝐻𝐸𝑅𝐸 𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 =
@𝑢𝑠𝑒𝑟𝑛𝑎𝑚𝑒 𝐴𝑁𝐷 𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑 = @𝑝𝑎𝑠𝑠𝑤𝑜𝑟𝑑";
2) Create a SqlCommand object and assign parameters:
𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑 𝑐𝑚𝑑 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑(𝑞𝑢𝑒𝑟𝑦, 𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛);
𝑐𝑚𝑑. 𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑠. 𝐴𝑑𝑑𝑊𝑖𝑡ℎ𝑉𝑎𝑙𝑢𝑒("@𝑢𝑠𝑒𝑟𝑛𝑎𝑚𝑒", 𝑢𝑠𝑒𝑟𝑛𝑎𝑚𝑒);
𝑐𝑚𝑑. 𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑠. 𝐴𝑑𝑑𝑊𝑖𝑡ℎ𝑉𝑎𝑙𝑢𝑒("@𝑝𝑎𝑠𝑠𝑤𝑜𝑟𝑑", 𝑝𝑎𝑠𝑠𝑤𝑜𝑟𝑑);
3) Execute the command using methods like 𝐸𝑥𝑒𝑐𝑢𝑡𝑒𝑅𝑒𝑎𝑑𝑒𝑟() or 𝐸𝑥𝑒𝑐𝑢𝑡𝑒𝑁𝑜𝑛𝑄𝑢𝑒𝑟𝑦():
𝑆𝑞𝑙𝐷𝑎𝑡𝑎𝑅𝑒𝑎𝑑𝑒𝑟 𝑟𝑒𝑎𝑑𝑒𝑟 = 𝑐𝑚𝑑. 𝐸𝑥𝑒𝑐𝑢𝑡𝑒𝑅𝑒𝑎𝑑𝑒𝑟();
Note that @𝑝𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑁𝑎𝑚𝑒 is used as a placeholder in SQL Server. Other databases like Oracle
or MySQL may use : 𝑝𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑁𝑎𝑚𝑒 or ? depending on the provider.
Example: Secure Login System
𝑠𝑡𝑟𝑖𝑛𝑔 𝑞𝑢𝑒𝑟𝑦 = "𝑆𝐸𝐿𝐸𝐶𝑇 𝐶𝑂𝑈𝑁𝑇(∗) 𝐹𝑅𝑂𝑀 𝑈𝑠𝑒𝑟𝑠 𝑊𝐻𝐸𝑅𝐸 𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 =
@𝑢𝑛𝑎𝑚𝑒 𝐴𝑁𝐷 𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑 = @𝑝𝑤𝑑";
𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑 𝑐𝑚𝑑 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑 (𝑞𝑢𝑒𝑟𝑦, 𝑐𝑜𝑛𝑛);
𝑐𝑚𝑑. 𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑠. 𝐴𝑑𝑑𝑊𝑖𝑡ℎ𝑉𝑎𝑙𝑢𝑒("@𝑢𝑛𝑎𝑚𝑒", 𝑡𝑥𝑡𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒. 𝑇𝑒𝑥𝑡);
𝑐𝑚𝑑. 𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑠. 𝐴𝑑𝑑𝑊𝑖𝑡ℎ𝑉𝑎𝑙𝑢𝑒("@𝑝𝑤𝑑", 𝑡𝑥𝑡𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑. 𝑇𝑒𝑥𝑡);
𝑖𝑛𝑡 𝑢𝑠𝑒𝑟𝐸𝑥𝑖𝑠𝑡𝑠 = (𝑖𝑛𝑡)𝑐𝑚𝑑. 𝐸𝑥𝑒𝑐𝑢𝑡𝑒𝑆𝑐𝑎𝑙𝑎𝑟();
𝑖𝑓 (𝑢𝑠𝑒𝑟𝐸𝑥𝑖𝑠𝑡𝑠 > 0)
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝐿𝑜𝑔𝑖𝑛 𝑠𝑢𝑐𝑐𝑒𝑠𝑠𝑓𝑢𝑙");
}
𝑒𝑙𝑠𝑒
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝐼𝑛𝑣𝑎𝑙𝑖𝑑 𝑐𝑟𝑒𝑑𝑒𝑛𝑡𝑖𝑎𝑙𝑠");
}
This approach prevents attackers from injecting malicious SQL code because the values are treated
strictly as data.
Best Practices When Using Parameterized Queries
• Use Strongly Typed Parameters: Use 𝑆𝑞𝑙𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟 explicitly instead of 𝐴𝑑𝑑𝑊𝑖𝑡ℎ𝑉𝑎𝑙𝑢𝑒()
to avoid potential issues with data types.
• Define Parameter Size and Type: Especially important for large or fixed-length fields like
𝑉𝐴𝑅𝐶𝐻𝐴𝑅(50) or 𝐼𝑁𝑇.
• Avoid Null Value Issues: Always check for null values and handle them correctly.
By Daud Anjum 24
ENTERPRISE SYSTEMS SI-342
• Use Stored Procedures Where Applicable: For even better abstraction and performance,
especially in enterprise environments.
Usage of Data Set
A DataSet in [Link] is a powerful, in-memory data container that represents a disconnected
collection of data. It is designed to hold multiple tables and the relationships between them, much
like a mini relational database. DataSets are especially useful in enterprise applications that need
to manipulate data offline, handle complex data structures, or perform batch updates.
The DataSet is a central feature of the disconnected data architecture of [Link], allowing
developers to fetch data once from the database and work with it in memory without maintaining
an open database connection.
Key Characteristics of DataSet
• Disconnected Architecture: DataSet allows data to be loaded, manipulated, and updated
without constant connection to the database.
• Multi-table Support: A DataSet can hold multiple 𝐷𝑎𝑡𝑎𝑇𝑎𝑏𝑙𝑒 objects, making it ideal for
representing relational data.
• Data Binding: It is commonly used to bind data to controls like 𝐺𝑟𝑖𝑑𝑉𝑖𝑒𝑤, 𝐷𝑎𝑡𝑎𝐺𝑟𝑖𝑑, or
forms.
• XML Integration: DataSet can read and write XML, making it useful for web services and data
exchange.
Components of DataSet
1) DataTable: Holds rows and columns of data
2) DataRow: Represents a single record in a table
3) DataColumn: Represents a field/column in a table
4) DataRelation: Represents the relationship between two tables
5) Constraints: Rules such as primary keys and foreign keys that maintain data integrity
How to Use a DataSet is [Link]
Step 1: Create a DataSet
𝐷𝑎𝑡𝑎𝑆𝑒𝑡 𝑑𝑠 = 𝑛𝑒𝑤 𝐷𝑎𝑡𝑎𝑆𝑒𝑡();
Step 2: Create a DataAdapter
𝑆𝑞𝑙𝐷𝑎𝑡𝑎𝐴𝑑𝑎𝑝𝑡𝑒𝑟 𝑎𝑑𝑎𝑝𝑡𝑒𝑟 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐷𝑎𝑡𝑎𝐴𝑑𝑎𝑝𝑡𝑒𝑟("𝑆𝐸𝐿𝐸𝐶𝑇 ∗
𝐹𝑅𝑂𝑀 𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟𝑠", 𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛);
Step 3: Fill the DataSet
𝑎𝑑𝑎𝑝𝑡𝑒𝑟. 𝐹𝑖𝑙𝑙(𝑑𝑠, "𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟𝑠");
By Daud Anjum 25
ENTERPRISE SYSTEMS SI-342
Step 4: Access Data
𝑓𝑜𝑟𝑒𝑎𝑐ℎ (𝐷𝑎𝑡𝑎𝑅𝑜𝑤 𝑟𝑜𝑤 𝑖𝑛 𝑑𝑠. 𝑇𝑎𝑏𝑙𝑒𝑠["𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟𝑠"]. 𝑅𝑜𝑤𝑠)
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒(𝑟𝑜𝑤["𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟𝑁𝑎𝑚𝑒"]);
}
You can also manipulate the data:
𝑑𝑠. 𝑇𝑎𝑏𝑙𝑒𝑠["𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟𝑠"]. 𝑅𝑜𝑤𝑠[0]["𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟𝑁𝑎𝑚𝑒"] = "𝑈𝑝𝑑𝑎𝑡𝑒𝑑 𝑁𝑎𝑚𝑒";
Modifying and Updating Data
One of the major uses of a DataSet is to modify data locally and then synchronize it back to the
database using a 𝐷𝑎𝑡𝑎𝐴𝑑𝑎𝑝𝑡𝑒𝑟.
𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑𝐵𝑢𝑖𝑙𝑑𝑒𝑟 𝑏𝑢𝑖𝑙𝑑𝑒𝑟 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑𝐵𝑢𝑖𝑙𝑑𝑒𝑟 (𝑎𝑑𝑎𝑝𝑡𝑒𝑟);
𝑎𝑑𝑎𝑝𝑡𝑒𝑟. 𝑈𝑝𝑑𝑎𝑡𝑒(𝑑𝑠, "𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟𝑠");
The 𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑𝐵𝑢𝑖𝑙𝑑𝑒𝑟 automatically generates 𝐼𝑁𝑆𝐸𝑅𝑇, 𝑈𝑃𝐷𝐴𝑇𝐸, and 𝐷𝐸𝐿𝐸𝑇𝐸
commands needed to update the database based on changes made in the DataSet.
Usage in Enterprise Systems
In enterprise-level applications, DataSets are widely used for:
• Handling complex reporting requirements
• Managing master-detail relationships in user interfaces
• Performing batch updates
• Offline data entry applications
• Exporting and importing data using XML
Example: In an enterprise inventory system, a DataSet might load tables for Products, Categories,
and Suppliers, allowing the application to relate them and present the full dataset to the user
without constantly querying the database.
Data Adapter and Command Builder in Disconnected Model
In the disconnected model of [Link], data is fetched from a data source (like SQL Server) into
an in-memory structure (such as a 𝐷𝑎𝑡𝑎𝑆𝑒𝑡) and then manipulated locally without a constant
connection to the database. Two critical components that make this model work efficiently are the
DataAdapter and the CommandBuilder. These classes work together to retrieve, update, and
manage data between the application and the database.
DataAdapter – The Bridge Between Database and DataSet
The DataAdapter acts as a bridge between a data source and the in-memory DataSet or DataTable.
It is responsible for retrieving data from the database into the DataSet, and optionally sending
updated data back to the database.
By Daud Anjum 26
ENTERPRISE SYSTEMS SI-342
Key Responsibilities of DataAdapter:
• Filling the DataSet/DataTable with data using the 𝐹𝑖𝑙𝑙() method.
• Updating the original database using the 𝑈𝑝𝑑𝑎𝑡𝑒() method after changes have been made in
the DataSet.
Basic Example:
𝑆𝑞𝑙𝐶𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛 𝑐𝑜𝑛𝑛 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐶𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛("𝑦𝑜𝑢𝑟_𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛_𝑠𝑡𝑟𝑖𝑛𝑔");
𝑆𝑞𝑙𝐷𝑎𝑡𝑎𝐴𝑑𝑎𝑝𝑡𝑒𝑟 𝑎𝑑𝑎𝑝𝑡𝑒𝑟 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐷𝑎𝑡𝑎𝐴𝑑𝑎𝑝𝑡𝑒𝑟("𝑆𝐸𝐿𝐸𝐶𝑇 ∗
𝐹𝑅𝑂𝑀 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠", 𝑐𝑜𝑛𝑛);
𝐷𝑎𝑡𝑎𝑆𝑒𝑡 𝑑𝑠 = 𝑛𝑒𝑤 𝐷𝑎𝑡𝑎𝑆𝑒𝑡();
𝑎𝑑𝑎𝑝𝑡𝑒𝑟. 𝐹𝑖𝑙𝑙(𝑑𝑠, "𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠");
At this point, the 𝑑𝑠 object holds a disconnected copy of the 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠 table.
Update Example:
To send updates (like new rows, edits, or deletes) back to the database, we can call:
𝑎𝑑𝑎𝑝𝑡𝑒𝑟. 𝑈𝑝𝑑𝑎𝑡𝑒(𝑑𝑠, "𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠");
But for this to work, the adapter must know the SQL statements for INSERT, UPDATE, and
DELETE operations.
CommandBuilder – Automating SQL Commands
The CommandBuilder is a helper class in [Link] that automatically generates the necessary
INSERT, UPDATE, and DELETE SQL commands for a 𝐷𝑎𝑡𝑎𝐴𝑑𝑎𝑝𝑡𝑒𝑟 based on a single SELECT
query.
This is particularly useful when you don’t want to manually define SQL statements for every
update operation.
Example:
𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑𝐵𝑢𝑖𝑙𝑑𝑒𝑟 𝑏𝑢𝑖𝑙𝑑𝑒𝑟 = 𝑛𝑒𝑤 𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑𝐵𝑢𝑖𝑙𝑑𝑒𝑟(𝑎𝑑𝑎𝑝𝑡𝑒𝑟);
𝑎𝑑𝑎𝑝𝑡𝑒𝑟. 𝑈𝑝𝑑𝑎𝑡𝑒(𝑑𝑠, "𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠");
After you associate the 𝐶𝑜𝑚𝑚𝑎𝑛𝑑𝐵𝑢𝑖𝑙𝑑𝑒𝑟 with the 𝑆𝑞𝑙𝐷𝑎𝑡𝑎𝐴𝑑𝑎𝑝𝑡𝑒𝑟, it automatically generates
the commands needed for updating the database. This saves time and reduces the risk of manual
coding errors.
Working Together in Disconnected Model
1) DataAdapter pulls data from the database using a SELECT query.
2) The data is stored in a DataSet or DataTable, where it can be viewed or edited offline.
3) When the user saves changes, the CommandBuilder auto-generates SQL statements (if not
provided manually).
4) The 𝐷𝑎𝑡𝑎𝐴𝑑𝑎𝑝𝑡𝑒𝑟. 𝑈𝑝𝑑𝑎𝑡𝑒() method is called to push changes back to the database.
This model is ideal for:
• Enterprise systems with many users editing data at the same time.
• Mobile or offline applications.
• Reporting and batch data processing.
By Daud Anjum 27
ENTERPRISE SYSTEMS SI-342
4. Introduction to Delegate
In C#, a delegate is a type-safe function pointer that allows methods to be passed as parameters,
delegates are used to encapsulate a method reference in a way that is flexible, secure, and object-
oriented. They are especially important in event-driven programming, such as GUI development
and enterprise-level systems, where dynamic method invocation if frequently required.
Delegates are a key part of the .NET framework and play a central role in the design of event
handling, asynchronous programming, and callback mechanisms,
Basic Concept of a Delegate
A delegate defines the signature (return type and parameters) of the method it can point to. Once
defined, the delegate can reference any method that matches that signature.
Syntax Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑑𝑒𝑙𝑒𝑔𝑎𝑡𝑒 𝑣𝑜𝑖𝑑 𝐷𝑖𝑠𝑝𝑙𝑎𝑦𝑀𝑒𝑠𝑠𝑎𝑔𝑒 (𝑠𝑡𝑟𝑖𝑛𝑔 𝑚𝑒𝑠𝑠𝑎𝑔𝑒);
This defines a delegate named 𝐷𝑖𝑠𝑝𝑙𝑎𝑦𝑀𝑒𝑠𝑠𝑎𝑔𝑒 that can reference any method that takes a
𝑠𝑡𝑟𝑖𝑛𝑔 parameter and returns 𝑣𝑜𝑖𝑑.
Usage Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑣𝑜𝑖𝑑 𝑆ℎ𝑜𝑤(𝑠𝑡𝑟𝑖𝑛𝑔 𝑚𝑠𝑔)
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝑀𝑒𝑠𝑠𝑎𝑔𝑒: " + 𝑚𝑠𝑔);
}
𝐷𝑖𝑠𝑝𝑙𝑎𝑦𝑀𝑒𝑠𝑠𝑎𝑔𝑒 𝑑𝑒𝑙 = 𝑛𝑒𝑤 𝐷𝑖𝑠𝑝𝑙𝑎𝑦𝑀𝑒𝑠𝑠𝑎𝑔𝑒 (𝑆ℎ𝑜𝑤);
𝑑𝑒𝑙("𝐻𝑒𝑙𝑙𝑜, 𝐷𝑒𝑙𝑒𝑔𝑎𝑡𝑒! ");
Types of Delegates
1) Single-cast Delegate: Refers to a single method.
2) Multi-cast Delegate: Refers to multiple methods. When invoked, it calls each method in the
order they were added.
Multicast Example:
𝐷𝑖𝑠𝑝𝑙𝑎𝑦𝑀𝑒𝑠𝑠𝑎𝑔𝑒 𝑑𝑒𝑙1 = 𝑛𝑒𝑤 𝐷𝑖𝑠𝑝𝑙𝑎𝑦𝑀𝑒𝑠𝑠𝑎𝑔𝑒 (𝑀𝑒𝑡ℎ𝑜𝑑1);
𝐷𝑖𝑠𝑝𝑙𝑎𝑦𝑀𝑒𝑠𝑠𝑎𝑔𝑒 𝑑𝑒𝑙2 = 𝑛𝑒𝑤 𝐷𝑖𝑠𝑝𝑙𝑎𝑦𝑀𝑒𝑠𝑠𝑎𝑔𝑒 (𝑀𝑒𝑡ℎ𝑜𝑑2);
𝐷𝑖𝑠𝑝𝑙𝑎𝑦𝑀𝑒𝑠𝑠𝑎𝑔𝑒 𝑐𝑜𝑚𝑏𝑖𝑛𝑒𝑑 = 𝑑𝑒𝑙1 + 𝑑𝑒𝑙2;
𝑐𝑜𝑚𝑏𝑖𝑛𝑒𝑑("𝑇ℎ𝑖𝑠 𝑖𝑠 𝑎 𝑚𝑢𝑙𝑡𝑖𝑐𝑎𝑠𝑡 𝑑𝑒𝑙𝑒𝑔𝑎𝑡𝑒");
Delegates vs Methods
While methods are fixed at compile-time, delegates allow dynamic method calls. This is very
useful in ES where different modules or components might need to call different methods based
on the runtime context or user action.
By Daud Anjum 28
ENTERPRISE SYSTEMS SI-342
Real-World Use in ES
• Event Handling: Delegates are the foundation for events in .NET. In enterprise applications
with GUI or user interactions, events are commonly handled using delegates.
• Callback Methods: When working with asynchronous operations (like file download or
database queries), delegates allow the specification of a callback function to be executed after
completion.
• Custom Logic Injection: Delegates can be passed as parameters to allow dynamic behaviour,
such as passing filtering logic into a method that processes collections.
Built-in Delegate Types
The .NET Framework provides generic delegates to simplify delegate usage:
1) Action< >: Represents a method that returns void
2) Func< >: Represents a method that returns a value
3) Predicate< >: Represents a method that returns a boolean value
Example using Func:
𝐹𝑢𝑛𝑐 < 𝑖𝑛𝑡, 𝑖𝑛𝑡, 𝑖𝑛𝑡 > 𝑎𝑑𝑑 = (𝑎, 𝑏) => 𝑎 + 𝑏;
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒(𝑎𝑑𝑑 (5, 3)); // 𝑂𝑢𝑡𝑝𝑢𝑡: 8
Anonymous Methods and Lambda Expressions
Delegates can also reference anonymous methods and lambda expressions, making them even
more powerful and concise.
𝐷𝑖𝑠𝑝𝑙𝑎𝑦𝑀𝑒𝑠𝑠𝑎𝑔𝑒 𝑚𝑠𝑔 = 𝑑𝑒𝑙𝑒𝑔𝑎𝑡𝑒(𝑠𝑡𝑟𝑖𝑛𝑔 𝑡𝑒𝑥𝑡)
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝐴𝑛𝑜𝑛𝑦𝑚𝑜𝑢𝑠: " + 𝑡𝑒𝑥𝑡);
};
𝐷𝑖𝑠𝑝𝑙𝑎𝑦𝑀𝑒𝑠𝑠𝑎𝑔𝑒 𝑙𝑎𝑚𝑏𝑑𝑎 = (𝑡𝑒𝑥𝑡 ) => 𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝐿𝑎𝑚𝑏𝑑𝑎: " + 𝑡𝑒𝑥𝑡);
This is particularly useful in enterprise applications where minimal, clean, and concise code is
preferred.
Advantages of Using Delegates
• Encapsulation of Methods: You can treat methods as variables.
• Loose Coupling: Helps reduce dependency between components.
• Dynamic Invocation: Enables calling different methods based on runtime logic.
• Essential for Events: Delegates are a core mechanism behind .NET events.
Multicast Delegates
A multicast delegate is a special type of delegate in C# that can hold references to more than one
method. When the delegate is invoked, it calls all the methods in its invocation list in the order
By Daud Anjum 29
ENTERPRISE SYSTEMS SI-342
they were added. This feature is especially useful in event-driven programming and enterprise
applications, where multiple components may need to respond to the same action or trigger.
Multicast delegates are built on the foundation of delegate chaining, which allows a single delegate
instance to encapsulate multiple methods with the same signature.
Concept of Multicast Delegates
Multicast delegates are derived from the base 𝑀𝑢𝑙𝑡𝑖𝑐𝑎𝑠𝑡𝐷𝑒𝑙𝑒𝑔𝑎𝑡𝑒 class in .NET. They work by
combining multiple delegates into a single delegate instance using the + or += operator. Similarly,
the - or -= operator can be used to remove methods from the delegate's invocation list.
Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑑𝑒𝑙𝑒𝑔𝑎𝑡𝑒 𝑣𝑜𝑖𝑑 𝑁𝑜𝑡𝑖𝑓𝑦();
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑁𝑜𝑡𝑖𝑓𝑖𝑐𝑎𝑡𝑖𝑜𝑛𝑆𝑒𝑟𝑣𝑖𝑐𝑒
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝑣𝑜𝑖𝑑 𝐸𝑚𝑎𝑖𝑙()
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝑆𝑒𝑛𝑑𝑖𝑛𝑔 𝐸𝑚𝑎𝑖𝑙 … ");
}
𝑝𝑢𝑏𝑙𝑖𝑐 𝑣𝑜𝑖𝑑 𝑆𝑀𝑆()
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝑆𝑒𝑛𝑑𝑖𝑛𝑔 𝑆𝑀𝑆 … ");
}
𝑝𝑢𝑏𝑙𝑖𝑐 𝑣𝑜𝑖𝑑 𝑃𝑢𝑠ℎ𝑁𝑜𝑡𝑖𝑓𝑖𝑐𝑎𝑡𝑖𝑜𝑛()
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝑆𝑒𝑛𝑑𝑖𝑛𝑔 𝑃𝑢𝑠ℎ 𝑁𝑜𝑡𝑖𝑓𝑖𝑐𝑎𝑡𝑖𝑜𝑛 … ");
}
}
𝑁𝑜𝑡𝑖𝑓𝑖𝑐𝑎𝑡𝑖𝑜𝑛𝑆𝑒𝑟𝑣𝑖𝑐𝑒 𝑠𝑒𝑟𝑣𝑖𝑐𝑒 = 𝑛𝑒𝑤 𝑁𝑜𝑡𝑖𝑓𝑖𝑐𝑎𝑡𝑖𝑜𝑛𝑆𝑒𝑟𝑣𝑖𝑐𝑒();
𝑁𝑜𝑡𝑖𝑓𝑦 𝑛𝑜𝑡𝑖𝑓𝑦𝐷𝑒𝑙𝑒𝑔𝑎𝑡𝑒 = 𝑠𝑒𝑟𝑣𝑖𝑐𝑒. 𝐸𝑚𝑎𝑖𝑙;
𝑛𝑜𝑡𝑖𝑓𝑦𝐷𝑒𝑙𝑒𝑔𝑎𝑡𝑒 += 𝑠𝑒𝑟𝑣𝑖𝑐𝑒. 𝑆𝑀𝑆;
𝑛𝑜𝑡𝑖𝑓𝑦𝐷𝑒𝑙𝑒𝑔𝑎𝑡𝑒 += 𝑠𝑒𝑟𝑣𝑖𝑐𝑒. 𝑃𝑢𝑠ℎ𝑁𝑜𝑡𝑖𝑓𝑖𝑐𝑎𝑡𝑖𝑜𝑛();
𝑛𝑜𝑡𝑖𝑓𝑦𝐷𝑒𝑙𝑒𝑔𝑎𝑡𝑒();
Output:
𝑆𝑒𝑛𝑑𝑖𝑛𝑔 𝐸𝑚𝑎𝑖𝑙 …
𝑆𝑒𝑛𝑑𝑖𝑛𝑔 𝑆𝑀𝑆 …
𝑆𝑒𝑛𝑑𝑖𝑛𝑔 𝑃𝑢𝑠ℎ 𝑁𝑜𝑡𝑖𝑓𝑖𝑐𝑎𝑡𝑖𝑜𝑛 …
Execution Order and Return Values
• The methods in the multicast delegate are called sequentially, in the order they were added.
By Daud Anjum 30
ENTERPRISE SYSTEMS SI-342
• If the delegate has a return type, only the result of the last method in the invocation list is
returned. The previous return values are ignored.
• If any method in the chain throws an exception, it stops the execution and propagates the
exception, unless explicitly handled.
Use in Enterprise Systems
Multicast delegates are especially useful in event broadcasting, where multiple components must
respond to a single event. For example:
1) In an inventory system, updating stock might trigger updates in:
• The sales report
• The supplier alert system
• The audit log
2) In a CRM system, a new lead creation may notify:
• The sales team (via email)
• The marketing system (for follow-up)
• The analytics engine (to update stats)
Instead of tightly coupling these actions, a multicast delegate allows broadcasting the event to all
necessary handlers in a loosely coupled and extensible way.
Removing Methods
You can remove a method from the multicast delegate using the -= operator.
𝑛𝑜𝑡𝑖𝑓𝑦𝐷𝑒𝑙𝑒𝑔𝑎𝑡𝑒 −= 𝑠𝑒𝑟𝑣𝑖𝑐𝑒. 𝑆𝑀𝑆;
𝑛𝑜𝑡𝑖𝑓𝑦𝐷𝑒𝑙𝑒𝑔𝑎𝑡𝑒(); // 𝑁𝑜𝑤 𝑜𝑛𝑙𝑦 𝐸𝑚𝑎𝑖𝑙 𝑎𝑛𝑑 𝑃𝑢𝑠ℎ𝑁𝑜𝑡𝑖𝑓𝑖𝑐𝑎𝑡𝑖𝑜𝑛 𝑎𝑟𝑒 𝑐𝑎𝑙𝑙𝑒𝑑
Multicast Delegates and Events
In .NET, events are based on multicast delegates. When you subscribe multiple methods to an
event using +=, you are effectively using a multicast delegate behind the scenes.
𝑝𝑢𝑏𝑙𝑖𝑐 𝑒𝑣𝑒𝑛𝑡 𝑁𝑜𝑡𝑖𝑓𝑦 𝑂𝑛𝑁𝑜𝑡𝑖𝑓𝑦;
𝑂𝑛𝑁𝑜𝑡𝑖𝑓𝑦 += 𝑠𝑒𝑟𝑣𝑖𝑐𝑒. 𝐸𝑚𝑎𝑖𝑙;
𝑂𝑛𝑁𝑜𝑡𝑖𝑓𝑦 += 𝑠𝑒𝑟𝑣𝑖𝑐𝑒. 𝑆𝑀𝑆;
𝑂𝑛𝑁𝑜𝑡𝑖𝑓𝑦();
This makes multicast delegates a core mechanism in any large-scale application that relies on user
interaction, asynchronous processing, or dynamic workflows.
Introduction to Windows Forms, Html
In Enterprise Systems and Software development using .NET, both Windows Forms (WinForms)
and HTML play significant roles in building user interfaces for applications. Windows Forms is
used for developing desktop applications, while HTML is the foundation of web applications.
By Daud Anjum 31
ENTERPRISE SYSTEMS SI-342
Understanding both is essential, especially when learning how delegates and event-driven
programming work in different UI environments.
Windows Forms
Windows Forms (WinForms) is a Graphical User Interface (GUI) toolkit provided by the .NET
Framework for creating desktop-based applications on Windows operating systems. It allows
developers to design form-based applications with buttons, textboxes, labels, menus, and other
controls.
Windows Forms is event-driven, meaning it relies heavily on delegates and events. Each UI
element (like a button) can raise events (like 𝐶𝑙𝑖𝑐𝑘), which are handled by event handlers using
delegates.
Key Features:
• Rich set of built-in controls (TextBox, Button, DataGridView, etc.)
• Drag-and-drop interface design using Visual Studio
• Full support for custom controls and user-defined events
• Tight integration with .NET features like [Link], delegates, and events
Example of Delegate Usage in Windows Forms:
𝑝𝑟𝑖𝑣𝑎𝑡𝑒 𝑣𝑜𝑖𝑑 𝑏𝑡𝑛𝐶𝑙𝑖𝑐𝑘𝑀𝑒_𝐶𝑙𝑖𝑐𝑘(𝑜𝑏𝑗𝑒𝑐𝑡 𝑠𝑒𝑛𝑑𝑒𝑟, 𝐸𝑣𝑒𝑛𝑡𝐴𝑟𝑔𝑠 𝑒)
{
𝑀𝑒𝑠𝑠𝑎𝑔𝑒𝐵𝑜𝑥. 𝑆ℎ𝑜𝑤("𝐵𝑢𝑡𝑡𝑜𝑛 𝑤𝑎𝑠 𝑐𝑙𝑖𝑐𝑘𝑒𝑑! ");
}
Here, the 𝐶𝑙𝑖𝑐𝑘 event of the button is wired to a method using a delegate behind the scenes. This
demonstrates the practical use of delegates in real-world Windows desktop applications.
HTML (HyperText Markup Language)
HTML (HyperText Markup Language) is the standard markup language used to create web pages.
Unlike Windows Forms, HTML is used in the web development domain, forming the backbone
of web interfaces, whether they’re static sites or enterprise-level web applications.
HTML defines the structure and layout of a web page using elements such as:
• <div>, <span>, <p> for layout and content
• <form>, <input>, <button> for user input
• <a>, <img>, <table> for links, images, and data display
HTML works with CSS (for styling) and JavaScript (for interactivity) to build full-featured web
applications.
Role in Enterprise Systems:
In enterprise systems, HTML is often used in:
1) Web portals
2) Enterprise dashboards
3) Customer management systems (CRMs)
By Daud Anjum 32
ENTERPRISE SYSTEMS SI-342
4) E-commerce platforms
These interfaces are built using HTML and connected to a backend (often using technologies like
[Link], PHP, or [Link]), where event-driven programming and delegates are still relevant on
the server side.
Delegate Equivalent in Web Context:
While HTML itself doesn’t support delegates, JavaScript plays a similar role with event listeners:
< 𝑏𝑢𝑡𝑡𝑜𝑛 𝑜𝑛𝑐𝑙𝑖𝑐𝑘="𝑠𝑎𝑦𝐻𝑒𝑙𝑙𝑜()" > 𝐶𝑙𝑖𝑐𝑘 𝑀𝑒 </𝑏𝑢𝑡𝑡𝑜𝑛 >
< 𝑠𝑐𝑟𝑖𝑝𝑡 >
𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑠𝑎𝑦𝐻𝑒𝑙𝑙𝑜() {
𝑎𝑙𝑒𝑟𝑡(′𝐻𝑒𝑙𝑙𝑜 ′ );
}
</𝑠𝑐𝑟𝑖𝑝𝑡 >
Here, 𝑜𝑛𝑐𝑙𝑖𝑐𝑘 acts similarly to a delegate – it references a function (𝑠𝑎𝑦𝐻𝑒𝑙𝑙𝑜) to be executed
when the event occurs.
5. Introduction to JavaScript
JavaScript is a high-level, interpreted programming language that is one of the core technologies
of the World Wide Web, alongside HTML and CSS. Initially created by Brendan Eich in 1995
while working at Netscape, JavaScript was designed to bring interactivity to otherwise static web
pages. Over the years, it has evolved into a powerful and versatile language capable of handling
everything from client-side scripting to server-side programming, game development, mobile app
creation, and more.
Unlike HTML, which structures a web page, and CSS, which styles it, JavaScript adds dynamic
behaviour and logic to websites. It allows developers to respond to user actions (such as clicks,
keyboard input, or form submissions), update content dynamically without refreshing the page
(via AJAX and APIs), validate input data, animate elements, and much more. Because of its
integration with web browsers, JavaScript is executed directly in the client’s browser, enabling
real-time interaction.
Originally developed under the name LiveScript, JavaScript was later renamed to take advantage
of the growing popularity of Java at the time, though the two languages are fundamentally
different. JavaScript follows the ECMAScript specification, a standard maintained by the ECMA
International organization. As of today, modern JavaScript (ES6 and beyond) includes advanced
features like classes, modules, promises, async/await, destructuring, and arrow functions, which
make it more robust and scalable for large applications.
JavaScript is a prototype-based, multi-paradigm language that supports object-oriented functional,
and imperative programming styles. This flexibility allows developers to approach problems using
the methodology that best fits their needs. It is dynamically typed, meaning variables do not
require explicit type declarations, and supports features like first-class functions, closures, and
lexical scoping, which enable highly modular and reuseable code.
By Daud Anjum 33
ENTERPRISE SYSTEMS SI-342
In recent years, JavaScript has expanded far beyond the browser thanks to environments like
[Link], which allows developers to write server-side applications using JavaScript. This has
paved the way for full-stack JavaScript development, where both the front end and back end of an
application are built using the same language. JavaScript is also at the heart of modern front-end
frameworks and libraries like React, Angular, and [Link], which are widely used in enterprise-
level applications.
JavaScript and its Data Types
JavaScript (JS), being a dynamically type and loosely typed language, allows, variables to hold
different types of data at different times during execution, this flexibility makes it easier to write
code quickly, but it also places a greater responsibility on the developer to manage data types
properly. In the context of ES, where data integrity and precision are crucial, understanding JS’s
data types is essential for building robust and scalable web applications.
JavaScript Syntax Overview
Before diving into data types, let’s briefly look at the syntax used to declare variables in JS:
𝑣𝑎𝑟 𝑛𝑎𝑚𝑒 = Alice; // 𝐸𝑆5 𝑤𝑎𝑦 (𝑜𝑙𝑑𝑒𝑟)
𝑙𝑒𝑡 𝑎𝑔𝑒 = 25; // 𝐸𝑆6 𝑏𝑙𝑜𝑐𝑘 − 𝑠𝑐𝑜𝑝𝑒𝑑 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒
𝑐𝑜𝑛𝑠𝑡 𝑃𝐼 = 3.14; // 𝐸𝑆6 𝑐𝑜𝑛𝑠𝑡𝑎𝑛𝑡, 𝑐𝑎𝑛𝑛𝑜𝑡 𝑏𝑒 𝑟𝑒𝑎𝑠𝑠𝑖𝑔𝑛𝑒𝑑
JavaScript Data Types
JS has two broad categories of data types:
1) Primitive Data Types
Primitive types are immutable and store simple data. They include:
• String
• Number
• Boolean
• Undefined
• Null
• BigInt (ES2020)
• Symbol (ES6)
a. String
A string represents a sequence of characters.
𝑙𝑒𝑡 𝑚𝑒𝑠𝑠𝑎𝑔𝑒 = "𝐻𝑒𝑙𝑙𝑜, 𝑊𝑜𝑟𝑙𝑑! ";
𝑙𝑒𝑡 𝑛𝑎𝑚𝑒 = ′𝐽𝑜ℎ𝑛′;
𝑙𝑒𝑡 𝑠𝑒𝑛𝑡𝑒𝑛𝑐𝑒 = `𝑊𝑒𝑙𝑐𝑜𝑚𝑒, ${𝑛𝑎𝑚𝑒}`; // 𝑇𝑒𝑚𝑝𝑙𝑎𝑡𝑒 𝑙𝑖𝑡𝑒𝑟𝑎𝑙 (𝐸𝑆6)
Strings can be created using single, double, or backtick quotes. Template literals allow
embedding expressions and multiline strings.
By Daud Anjum 34
ENTERPRISE SYSTEMS SI-342
b. Number
Represents both integers and floating-point number.
𝑙𝑒𝑡 𝑐𝑜𝑢𝑛𝑡 = 10;
𝑙𝑒𝑡 𝑝𝑟𝑖𝑐𝑒 = 99.99;
𝑙𝑒𝑡 𝑟𝑒𝑠𝑢𝑙𝑡 = 10 / 2;
JS has only one numeric type: 𝑛𝑢𝑚𝑏𝑒𝑟. Even decimals and whole numbers are treated the
same,
c. Boolean
A logical data types that can only be true or false.
𝑙𝑒𝑡 𝑖𝑠𝐿𝑜𝑔𝑔𝑒𝑑𝐼𝑛 = 𝑡𝑟𝑢𝑒;
𝑙𝑒𝑡 𝑖𝑠𝐴𝑑𝑚𝑖𝑛 = 𝑓𝑎𝑙𝑠𝑒;
Booleans are used in conditional statements and logic-based operations.
d. Undefined
A variable declared but not assigned a value is undefined.
𝑙𝑒𝑡 𝑢𝑠𝑒𝑟;
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. log(𝑢𝑠𝑒𝑟) ; // 𝑂𝑢𝑡𝑝𝑢𝑡: 𝑢𝑛𝑑𝑒𝑓𝑖𝑛𝑒𝑑
e. Null
A special keyword that represents a deliberately empty or unknown value.
𝑙𝑒𝑡 𝑠𝑒𝑙𝑒𝑐𝑡𝑒𝑑𝑃𝑟𝑜𝑑𝑢𝑐𝑡 = 𝑛𝑢𝑙𝑙;
Note: 𝑡𝑦𝑝𝑒𝑜𝑓 𝑛𝑢𝑙𝑙 returns ′𝑜𝑏𝑗𝑒𝑐𝑡′, which is long-standing bug in JS.
f. BigInt
Used for representing very large integers beyond the safe integer limit of 𝑛𝑢𝑚𝑏𝑒𝑟.
𝑙𝑒𝑡 𝑏𝑖𝑔𝑁𝑢𝑚𝑏𝑒𝑟 = 9007199254740991𝑛; // 𝑁𝑜𝑡𝑖𝑐𝑒 𝑡ℎ𝑒 ′𝑛′ 𝑎𝑡 𝑡ℎ𝑒 𝑒𝑛𝑑
g. Symbol
Used to created unique and immutable identifiers for object properties.
𝑙𝑒𝑡 𝑖𝑑 = 𝑆𝑦𝑚𝑏𝑜𝑙("𝑢𝑠𝑒𝑟𝐼𝐷");
2) Non-Primitive (Reference) Data Types
These types store references to memory locations rather than actual values.
• Object
• Array
• Function
• Date, RegExp, and other built-in object types
a. Object
An unordered collection of key-value pairs.
𝑙𝑒𝑡 𝑝𝑒𝑟𝑠𝑜𝑛 = {
𝑛𝑎𝑚𝑒: "𝐴𝑙𝑖𝑐𝑒",
𝑎𝑔𝑒: 30,
𝑖𝑠𝐴𝑑𝑚𝑖𝑛: 𝑡𝑟𝑢𝑒
}
By Daud Anjum 35
ENTERPRISE SYSTEMS SI-342
Objects are the backbone of JS and are widely used is enterprise applications to represent
structured data (e.g., JSON).
b. Array
An ordered list of values (of any type).
𝑙𝑒𝑡 𝑓𝑟𝑢𝑖𝑡𝑠 = ["𝐴𝑝𝑝𝑙𝑒", "𝐵𝑎𝑛𝑎𝑛𝑎", "𝐶ℎ𝑒𝑟𝑟𝑦"];
𝑙𝑒𝑡 𝑛𝑢𝑚𝑏𝑒𝑟𝑠 = [1, 2, 3, 4];
Arrays are zero-indexed and supports numerous methods like 𝑝𝑢𝑠ℎ(), 𝑝𝑜𝑝(), 𝑚𝑎𝑝(),
𝑓𝑖𝑙𝑡𝑒𝑟(), and 𝑟𝑒𝑑𝑢𝑐𝑒().
c. Function
In JS, functions are first-class citizens – they can be stored in variables, passed as
arguments, and returned from other functions.
𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑔𝑟𝑒𝑒𝑡 (𝑛𝑎𝑚𝑒) {
𝑟𝑒𝑡𝑢𝑟𝑛 "𝐻𝑒𝑙𝑙𝑜, " + 𝑛𝑎𝑚𝑒;
}
𝑙𝑒𝑡 𝑠𝑎𝑦𝐻𝑒𝑙𝑙𝑜 = 𝑔𝑟𝑒𝑒𝑡;
Functions can also be declared using arrow syntax (ES6):
𝑐𝑜𝑛𝑠𝑡 𝑎𝑑𝑑 = (𝑎, 𝑏) => 𝑎 + 𝑏;
Dynamic Typing
JS is dynamically types, so you can change the type of a variable at runtime:
𝑙𝑒𝑡 𝑑𝑎𝑡𝑎 = 10; // 𝑛𝑢𝑚𝑏𝑒𝑟
𝑑𝑎𝑡𝑎 = Ten; // 𝑛𝑜𝑤 𝑎 𝑠𝑡𝑟𝑖𝑛𝑔
This flexibility is powerful but can also lead to runtime errors if not carefully managed – especially
in ES with complex logic and data models.
Variables and Functions
In JS, variables and functions are fundamental building blocks of any application, including large-
scale ES. Variables allow the storage and manipulation of data, while functions enable reusable
and modular blocks of logic. Understanding how to declare and use variables and functions
effectively is key to writing scalable and maintainable code.
Variables in JavaScript
A variable in JS acts as a named container that holds a value, which can be of any data type. JS
supports three main keywords for variables declaration: 𝑣𝑎𝑟, 𝑙𝑒𝑡, and 𝑐𝑜𝑛𝑠𝑡.
1) var (Function-scoped)
The 𝑣𝑎𝑟 keyword was traditionally used before ES6. It has function-level scope, meaning if
declared inside a function, it is accessible only within that function.
𝑣𝑎𝑟 𝑛𝑎𝑚𝑒 = "𝐴𝑙𝑖𝑐𝑒";
By Daud Anjum 36
ENTERPRISE SYSTEMS SI-342
One limitation of 𝑣𝑎𝑟 is that it does not support block-level scope, which can lead to
unintended behaviours in complex applications.
2) let (Block-scoped)
introduced in ES6, 𝑙𝑒𝑡 is the modern, preferred way of declaring variables that can change.
𝑙𝑒𝑡 𝑎𝑔𝑒 = 30;
𝑎𝑔𝑒 = 31;
𝑙𝑒𝑡 has block-level scope, meaning it is only accessible within the block ( { } ) where it is
defined.
3) const (Block-scoped Constant)
Also introduced in ES6, 𝑐𝑜𝑛𝑠𝑡 is used to declare constants – variables that cannot be reassigned
after initialization.
𝑐𝑜𝑛𝑠𝑡 𝑝𝑖 = 3.14;
However, for objects and arrays, 𝑐𝑜𝑛𝑠𝑡 prevents reassignment of the variable reference, not its
contents.
Variable Hoisting
JavaScript uses a concept called hoisting, where variable and function declarations are moved to
the top of their scope during execution. However, only the declarations are hoisted, not the
initializations.
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. log(𝑥) ; // 𝑢𝑛𝑑𝑒𝑓𝑖𝑛𝑒𝑑
𝑣𝑎𝑟 𝑥 = 10;
𝑙𝑒𝑡 and 𝑐𝑜𝑛𝑠𝑡 are hoisted too, but they are not initialized and remain in a temporal dead zone,
which prevents access before their declaration line.
Functions in JavaScript
A function is a reusable block of code that performs a specific task. Functions improve code
modularity and reduce redundancy, which is crucial in enterprise-scale development.
1) Function Declaration (Named Function)
𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑔𝑟𝑒𝑒𝑡 (𝑛𝑎𝑚𝑒) {
𝑟𝑒𝑡𝑢𝑟𝑛 "𝐻𝑒𝑙𝑙𝑜, " + 𝑛𝑎𝑚𝑒;
}
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. log (𝑔𝑟𝑒𝑒𝑡("𝐴𝑙𝑖𝑐𝑒"));
Function declarations are hoisted, meaning you can call the function before its declaration in
the code.
2) Function Expression
A function can also be stored in a variable:
𝑐𝑜𝑛𝑠𝑡 𝑔𝑟𝑒𝑒𝑡 = 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛(𝑛𝑎𝑚𝑒) {
𝑟𝑒𝑡𝑢𝑟𝑛 "𝐻𝑒𝑙𝑙𝑜, " + 𝑛𝑎𝑚𝑒;
}
Function expressions are not hoisted, so you must define them before calling.
By Daud Anjum 37
ENTERPRISE SYSTEMS SI-342
3) Arrow Functions (ES6)
Arrow functions provide a more concise syntax:
𝑐𝑜𝑛𝑠𝑡 𝑎𝑑𝑑 = (𝑎, 𝑏) => 𝑎 + 𝑏;
Arrow functions have lexical 𝑡ℎ𝑖𝑠 binding, meaning they inherit 𝑡ℎ𝑖𝑠 from the parent scope,
unlike traditional functions which define their own 𝑡ℎ𝑖𝑠.
Parameters and Argument
Functions can accept parameters and return values. In JS, functions do not enforce the number or
type of arguments.
𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑦 (𝑎, 𝑏) {
𝑟𝑒𝑡𝑢𝑟𝑛 𝑎 ∗ 𝑏;
}
𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑦(5, 2); // 10
𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑦(5); // 𝑁𝑎𝑁 (𝑏 𝑖𝑠 𝑢𝑛𝑑𝑒𝑓𝑖𝑛𝑒𝑑
You can also use default parameters:
𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑚𝑢𝑙𝑡𝑖𝑝𝑙𝑦 (𝑎, 𝑏 = 1) {
𝑟𝑒𝑡𝑢𝑟𝑛 𝑎 ∗ 𝑏;
}
Rest Parameters and the 𝑎𝑟𝑔𝑢𝑚𝑒𝑛𝑡𝑠 Object
Rest Parameter (…) is used for variable number of arguments:
𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑠𝑢𝑚(… 𝑛𝑢𝑚𝑏𝑒𝑟𝑠) {
𝑟𝑒𝑡𝑢𝑟𝑛 𝑛𝑢𝑚𝑏𝑒𝑟𝑠. 𝑟𝑒𝑑𝑢𝑐𝑒((𝑎, 𝑏) => 𝑎 + 𝑏, 0);
}
𝑎𝑟𝑔𝑢𝑚𝑒𝑛𝑡𝑠 Object is available in traditional functions (not arrow functions):
𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑠ℎ𝑜𝑤𝐴𝑟𝑔𝑠() {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. log(𝑎𝑟𝑔𝑢𝑚𝑒𝑛𝑡𝑠) ;
}
Anonymous and Callback Functions
Anonymous functions are unnamed functions, commonly used as callbacks:
𝑠𝑒𝑡𝑇𝑖𝑚𝑒𝑜𝑢𝑡(𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛() {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. log ("𝐸𝑥𝑒𝑐𝑢𝑡𝑒𝑑 𝑎𝑓𝑡𝑒𝑟 1 𝑠𝑒𝑐𝑜𝑛𝑑");
}, 1000);
Function Scope and Closures
JS functions create their own scope. A closure is formed when a function "remembers" variables
from its outer lexical scope, even after that scope has closed.
𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑜𝑢𝑡𝑒𝑟() {
𝑙𝑒𝑡 𝑐𝑜𝑢𝑛𝑡 = 0;
𝑟𝑒𝑡𝑢𝑟𝑛 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑖𝑛𝑛𝑒𝑟() {
𝑐𝑜𝑢𝑛𝑡 + +;
By Daud Anjum 38
ENTERPRISE SYSTEMS SI-342
𝑟𝑒𝑡𝑢𝑟𝑛 𝑐𝑜𝑢𝑛𝑡;
}
}
𝑐𝑜𝑛𝑠𝑡 𝑐𝑜𝑢𝑛𝑡𝑒𝑟 = 𝑜𝑢𝑡𝑒𝑟();
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. log (𝑐𝑜𝑢𝑛𝑡𝑒𝑟()); // 1
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. log (𝑐𝑜𝑢𝑛𝑡𝑒𝑟()); // 2
Closures are especially important in ES development for encapsulating logic, securing data, and
creating modules.
Immediately Invoked Function Expressions (IIFE)
IIFE is a function that runs immediately after being defined. Often used to create private scopes
in enterprise-level JavaScript modules.
(𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛() {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. log ("𝐼𝐼𝐹𝐸 𝑒𝑥𝑒𝑐𝑢𝑡𝑒𝑑");
})();
Debugging JavaScript Using Firebug
In enterprise-scale JS applications, debugging is an essential skill for maintaining code quality,
detecting issues, and enduring smooth user experiences. One of the earlier and most popular tools
for debugging JS in web browsers was Firebug. Although Firebug has since been discontinued
and its features merged into modern browser developer tools, understanding its role provides
insight into how JS debugging evolved and how tools assist in managing large-scale ES.
What is Firebug?
It was a free and powerful web development tool integrated as an extension into the Mozilla
Firefox browser. It allowed developers to inspect, edit, and debug HTML, CSS, and JS in real
time. Firebug was especially popular in the mid-to-late 2000s and was one of the first tools to
provide rich debugging capabilities for client-side code.
For JS specifically, Firebug allowed developers to:
• View and interact with JS code
• Set breakpoints
• Inspect variables and execution context
• Monitor console outputs
• Step through code line-by-line
Using Firebug for JavaScript Debugging
1) Console Tab
The Console tab in Firebug was used to log information using 𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔(), view errors, and
evaluate JavaScript expressions directly.
Example:
By Daud Anjum 39
ENTERPRISE SYSTEMS SI-342
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔("𝑈𝑠𝑒𝑟 𝐼𝐷: ", 𝑢𝑠𝑒𝑟𝐼𝑑);
This output appeared in the console and helped developers trace the flow of execution and spot
logic errors.
2) Script Tab (JavaScript Debugging)
The Script tab allowed developers to view all JS files loaded in the current webpage. From
here, one could:
• Set breakpoints by clicking on a line number
• Step over, step into, or step out of functions
• Pause execution at any time to inspect the state
• View the call stack, watch expressions, and local variables
This made it possible to identify bugs by closely observing how the program's state changed
during execution
3) DOM Inspection
Firebug allowed inspection and editing of the Document Object Model (DOM). Since JS often
manipulates the DOM dynamically, being able to see live changes was crucial for debugging
interactive features like form handling, modals, and AJAX-loaded content.
Advantages in Enterprise Development
Firebug played a critical role in enterprise JS development by enabling:
• Faster identification of bugs in production and staging environments
• Real-time inspection of client-side behaviour
• Improved collaboration between developers and testers
• Enhanced learning and debugging for developers writing complex logic and asynchronous
code
Limitations and Discontinuation
Despite its popularity, Firebug had performance issues with large-scale applications and was
limited to Firefox. With the evolution of browser engines and the releases of Firefox Quantum,
Firebug became obsolete. Its key features were integrated into the built-in Firefox Developer
Tools, which are faster and more deeply integrated into the browser.
As of now, modern tools like Chrome DevTools, Firefox DevTools, Edge DevTools, and even
Visual Studio Code offer superior debugging capabilities compared to Firebug.
6. Introduction to Various Object Models
In the context of ES and web-based application development, object models play a crucial role in
structuring, accessing, and manipulating data, documents, and interfaces programmatically. An
object model is essentially a programming interface that represents data or functionality as objects,
allowing developers to interact with them through code. In web development and enterprise
software, object models abstract the underlying structure of the system – making it easier to build,
manage, and extend complex applications.
By Daud Anjum 40
ENTERPRISE SYSTEMS SI-342
JS, which is a cornerstone technology for front-end enterprise applications, interacts with several
object models that represent different parts of the web environment. The most widely used object
models include the Global Object Model, Document Object Model, and Browser Object Model.
Global Object Model
In JS, the Global Object Model refers to the environment that provides access to al globally defined
objects, functions, and variables. For browsers, the global object is typically the 𝑤𝑖𝑛𝑑𝑜𝑤 object.
Every variable or function declared globally becomes a property or method of this global object.
Example:
𝑣𝑎𝑟 𝑛𝑎𝑚𝑒 = "𝐸𝑛𝑡𝑒𝑟𝑝𝑟𝑖𝑠𝑒𝑠";
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. log(𝑤𝑖𝑛𝑑𝑜𝑤. 𝑛𝑎𝑚𝑒) ; // 𝑂𝑢𝑡𝑝𝑢𝑡: 𝐸𝑛𝑡𝑒𝑟𝑝𝑟𝑖𝑠𝑒
This model ensures that all global constructs are accessible from a central reference point. It’s
useful in large enterprise applications where managing namespaces, modules, and application-
wide configurations is necessary.
Document Object Model (DOM)
The DOM represents the structure of an HTML or XML document as a tree of objects. Each
element, attribute, and piece of text becomes an object that can be dynamically accessed and
manipulated using JS. The DOM is essential for developing dynamic enterprise applications that
need to interact with user interfaces in real time.
Browser Object Model (BOM)
The BOM provides access to browser-specific objects like 𝑛𝑎𝑣𝑖𝑔𝑎𝑡𝑜𝑟, 𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛, 𝑠𝑐𝑟𝑒𝑒𝑛,
ℎ𝑖𝑠𝑡𝑜𝑟𝑦, and others. It allows developers to control and retrieve information from the browser
outside the content of the webpage.
Example:
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔(𝑛𝑎𝑣𝑖𝑔𝑎𝑡𝑜𝑟. 𝑢𝑠𝑒𝑟𝐴𝑔𝑒𝑛𝑡); // 𝑂𝑢𝑡𝑝𝑢𝑡𝑠 𝑏𝑟𝑜𝑤𝑠𝑒𝑟 𝑑𝑒𝑡𝑎𝑖𝑙𝑠
𝑤𝑖𝑛𝑑𝑜𝑤. 𝑜𝑝𝑒𝑛("ℎ𝑡𝑡𝑝𝑠://𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚"); // 𝑂𝑝𝑒𝑛𝑠 𝑎 𝑛𝑒𝑤 𝑡𝑎𝑏 𝑜𝑟 𝑤𝑖𝑛𝑑𝑜𝑤
BOM is important in ES for enhancing user experience, managing browser history, performing
redirects, and detecting browser capabilities.
XML Object Model (e.g., DOM for XML)
For enterprise applications that exchange data in XML format, especially in service-oriented
architectures or legacy systems, the XML Document Object Model is used. It functions similarly
to the HTML DOM, allowing JavaScript or other languages (like C# or Java) to traverse and
manipulate XML data structure.
This is often useful in enterprise data transformation, integration with external APIs, or
configuration-based systems.
By Daud Anjum 41
ENTERPRISE SYSTEMS SI-342
Custom Object Models in Enterprise Applications
In large-scale enterprise development, especially with frameworks like .NET or Java EE, custom
object models are created to represent real-world entities such as users, invoices, orders, projects,
or tasks. These object models serve as blueprints for the system’s data and behaviour and are
structured using object-oriented principles such as inheritance, encapsulation, and polymorphism.
These models are often:
• Created as classes or interfaces
• Interconnected through relationships (e.g., one-to-many, many-to-many)
• Exposed via APIs or services for frontend consumption
They form the backbone of the business logic layer and interact with both the data access layer
and the presentation layer.
Browser’s Object (BOM)
The BOM is a critical part of client-side web development. It provides a way for JS to interact
with the browser environment itself, rather than just the content of a webpage. While the DOM
deals with HTML documents, the BOM deals with the browser window and all its components,
such as navigation, location, screen information, browser history, and even popup management.
In ES, especially those involving rich user interfaces and dynamic behaviour, BOM is important
for tasks like controlling navigation, detecting browser features, managing windows, and working
with URLs – all of which are crucial in large-scale, user-centric applications.
What is the BOM?
The BOM is not standardized by the W3C like the DOM, but it is implemented by all major
browsers. The core object in BOM is the 𝑤𝑖𝑛𝑑𝑜𝑤 object, which represents the browser window
or frame displaying the webpage. Every global variable or function declared in JS becomes a
property or method of the 𝑤𝑖𝑛𝑑𝑜𝑤 object.
Example:
𝑣𝑎𝑟 𝑎𝑝𝑝𝑁𝑎𝑚𝑒 = "𝐸𝑛𝑡𝑒𝑟𝑝𝑟𝑖𝑠𝑒 𝐴𝑝𝑝";
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. log (𝑤𝑖𝑛𝑑𝑜𝑤. 𝑎𝑝𝑝𝑁𝑎𝑚𝑒);
Because the BOM is centred around the 𝑤𝑖𝑛𝑑𝑜𝑤 object, many features you use in everyday JS
like 𝑎𝑙𝑒𝑟𝑡(), 𝑠𝑒𝑡𝑇𝑖𝑚𝑒𝑜𝑢𝑡(), or 𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔() are technically part of BOM.
Key Components of the BOM
1) 𝑤𝑖𝑛𝑑𝑜𝑤 Object
The 𝑤𝑖𝑛𝑑𝑜𝑤 object is the global object for the browser. It contains all other BOM objects and
many built-in methods.
By Daud Anjum 42
ENTERPRISE SYSTEMS SI-342
Example:
𝑤𝑖𝑛𝑑𝑜𝑤. 𝑎𝑙𝑒𝑟𝑡("𝑊𝑒𝑙𝑐𝑜𝑚𝑒! ");
𝑤𝑖𝑛𝑑𝑜𝑤. 𝑠𝑒𝑡𝑇𝑖𝑚𝑒𝑜𝑢𝑡(𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛() {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. log ("𝐷𝑒𝑙𝑎𝑦𝑒𝑑 𝑚𝑒𝑠𝑠𝑎𝑔𝑒");
}, 2000);
Even calling 𝑎𝑙𝑒𝑟𝑡() without 𝑤𝑖𝑛𝑑𝑜𝑤. still works because it defaults to the global scope.
2) 𝑛𝑎𝑣𝑖𝑔𝑎𝑡𝑜𝑟 Object
The 𝑛𝑎𝑣𝑖𝑔𝑎𝑡𝑜𝑟 object provides information about the browser itself, such as its name, version,
platform, and capabilities. It is useful for browser detection, especially in enterprise apps that
may need to handle different browsers differently.
Example:
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔(𝑛𝑎𝑣𝑖𝑔𝑎𝑡𝑜𝑟. 𝑢𝑠𝑒𝑟𝐴𝑔𝑒𝑛𝑡); // 𝐹𝑢𝑙𝑙 𝑏𝑟𝑜𝑤𝑠𝑒𝑟 𝑖𝑛𝑓𝑜 𝑠𝑡𝑟𝑖𝑛𝑔
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔(𝑛𝑎𝑣𝑖𝑔𝑎𝑡𝑜𝑟. 𝑙𝑎𝑛𝑔𝑢𝑎𝑔𝑒); // 𝑒. 𝑔. , "𝑒𝑛 − 𝑈𝑆"
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔(𝑛𝑎𝑣𝑖𝑔𝑎𝑡𝑜𝑟. 𝑜𝑛𝐿𝑖𝑛𝑒); // 𝑡𝑟𝑢𝑒 𝑜𝑟 𝑓𝑎𝑙𝑠𝑒
3) 𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛 Object
The 𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛 object represents the current URL of the browser window and allows you to read
or modify it.
Example:
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔(𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛. ℎ𝑟𝑒𝑓); // 𝐹𝑢𝑙𝑙 𝑈𝑅𝐿
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔(𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛. ℎ𝑜𝑠𝑡𝑛𝑎𝑚𝑒); // 𝐷𝑜𝑚𝑎𝑖𝑛 𝑛𝑎𝑚𝑒
𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛. ℎ𝑟𝑒𝑓 = "ℎ𝑡𝑡𝑝𝑠://𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚"; // 𝑅𝑒𝑑𝑖𝑟𝑒𝑐𝑡 𝑡𝑜 𝑎𝑛𝑜𝑡ℎ𝑒𝑟 𝑝𝑎𝑔𝑒
This is essential in signal-page applications, routing, and URL-based logic used in enterprise
web platforms.
4) ℎ𝑖𝑠𝑡𝑜𝑟𝑦 Object
The ℎ𝑖𝑠𝑡𝑜𝑟𝑦 object allows you to manipulate the session history of the browser (i.e., the pages
visited in the current tab or window). This is commonly used for navigation and back/forward
functionality in dynamic enterprise apps.
Example:
ℎ𝑖𝑠𝑡𝑜𝑟𝑦. 𝑏𝑎𝑐𝑘(); // 𝐺𝑜 𝑏𝑎𝑐𝑘 𝑜𝑛𝑒 𝑝𝑎𝑔𝑒
ℎ𝑖𝑠𝑡𝑜𝑟𝑦. 𝑓𝑜𝑟𝑤𝑎𝑟𝑑(); // 𝐺𝑜 𝑓𝑜𝑟𝑤𝑎𝑟𝑑
In advanced applications, ℎ𝑖𝑠𝑡𝑜𝑟𝑦. 𝑝𝑢𝑠ℎ𝑆𝑡𝑎𝑡𝑒() is used in combination with AJAX to change
the URL without reloading the page (important for SPAs).
5) 𝑠𝑐𝑟𝑒𝑒𝑛 Object
The 𝑠𝑐𝑟𝑒𝑒𝑛 object provides information about the user's screen resolution, which is useful for
responsive layouts or applications that need to adapt to specific display environments.
Example:
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔(𝑠𝑐𝑟𝑒𝑒𝑛. 𝑤𝑖𝑑𝑡ℎ); // 𝑆𝑐𝑟𝑒𝑒𝑛 𝑤𝑖𝑑𝑡ℎ 𝑖𝑛 𝑝𝑖𝑥𝑒𝑙𝑠
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔(𝑠𝑐𝑟𝑒𝑒𝑛. ℎ𝑒𝑖𝑔ℎ𝑡); // 𝑆𝑐𝑟𝑒𝑒𝑛 ℎ𝑒𝑖𝑔ℎ𝑡
While not frequently used in basic apps, screen data is important in enterprise-level
applications that require adaptive interfaces or kiosk modes.
6) Popup and Window Control
BOM allows developers to create new browser windows or tabs using 𝑤𝑖𝑛𝑑𝑜𝑤. 𝑜𝑝𝑒𝑛().
By Daud Anjum 43
ENTERPRISE SYSTEMS SI-342
Example:
𝑣𝑎𝑟 𝑛𝑒𝑤𝑊𝑖𝑛 = 𝑤𝑖𝑛𝑑𝑜𝑤. 𝑜𝑝𝑒𝑛("ℎ𝑡𝑡𝑝𝑠://𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚", "_𝑏𝑙𝑎𝑛𝑘", "𝑤𝑖𝑑𝑡ℎ =
600, ℎ𝑒𝑖𝑔ℎ𝑡 = 400");
Enterprise apps may use this for printing, opening detail views, or displaying charts/reports in
separate views.
BOM and Enterprise Systems
In enterprise environments, BOM is especially valuable in:
• Customizing browser-based experiences
• Handling redirects and session-based navigation
• Detecting client capabilities
• Implementing advanced security prompts or logout timers
• Managing multi-tab workflows
Its usage can directly impact user interface consistency, accessibility, and cross-browser
compatibility – all of which are key concerns in large-scale systems.
Document Object Model
The DOM is a programming interface for web documents. It represents the structure of an HTML
or XML document as a hierarchical tree of objects. Each element, attribute, and piece of content
in the document is modelled as a node in this tree, allowing developers to programmatically access,
modify, delete, or add content dynamically using languages like JavaScript.
For ES, which often rely on rich, interactive web interfaces, the DOM is central to front-end
development. Whether it’s updating dashboards, validating forms, or building single-page
applications, understanding and manipulating the DOM is essential for delivering dynamic and
responsive user experiences.
What is the DOM?
The DOM is not the document itself, but an in-memory representation of it. When a web page is
leaded in a browser, the HTML is parsed and converted into the DOM. This allows scripts to
interact with the page structure while it is being viewed or used.
The DOM is platform and language independent, and in standardized by the W3C (World Wide
Web Consortium). It provides a structured, object-oriented representation of the document, where
each element is treated as a node object.
DOM Tree Structure
At the top of the DOM hierarchy is the 𝑑𝑜𝑐𝑢𝑚𝑒𝑛𝑡 object, which acts as the root. The HTML
document is represented as a tree of nodes:
• Element Nodes (e.g., <div>, <p>, <a>)
• Attribute Nodes (e.g., class=“btn”, href=“…”)
• Text Nodes (e.g., the actual text inside elements)
By Daud Anjum 44
ENTERPRISE SYSTEMS SI-342
• Comment Nodes (e.g., <!--comment -->)
Example HTML:
< ℎ𝑡𝑚𝑙 >
< 𝑏𝑜𝑑𝑦 >
< 𝑝 𝑖𝑑 = "𝑖𝑛𝑡𝑟𝑜" > 𝑊𝑒𝑙𝑐𝑜𝑚𝑒 </𝑝 >
</𝑏𝑜𝑑𝑦 >
</ℎ𝑡𝑚𝑙 >
DOM Tree Representation:
𝑑𝑜𝑐𝑢𝑚𝑒𝑛𝑡
└── ℎ𝑡𝑚𝑙
└── 𝑏𝑜𝑑𝑦
└── 𝑝 (𝑖𝑑 = "𝑖𝑛𝑡𝑟𝑜")
└── "𝑊𝑒𝑙𝑐𝑜𝑚𝑒" (𝑇𝑒𝑥𝑡 𝑁𝑜𝑑𝑒)
Accessing the DOM with JavaScript
The DOM can be accessed and manipulated using built-in JS methods and properties provided
through the 𝑑𝑜𝑐𝑢𝑚𝑒𝑛𝑡 object.
Common Methods:
• 𝑔𝑒𝑡𝐸𝑙𝑒𝑚𝑒𝑛𝑡𝐵𝑦𝐼𝑑("𝑖𝑑") – Get an element by its ID
• 𝑔𝑒𝑡𝐸𝑙𝑒𝑚𝑒𝑛𝑡𝑠𝐵𝑦𝐶𝑙𝑎𝑠𝑠𝑁𝑎𝑚𝑒("𝑐𝑙𝑎𝑠𝑠") – Get all elements by class name
• 𝑔𝑒𝑡𝐸𝐿𝑒𝑚𝑒𝑛𝑡𝑠𝐵𝑦𝑇𝑎𝑔𝑁𝑎𝑚𝑒("𝑡𝑎𝑔") – Get all elements by tag
• 𝑞𝑢𝑒𝑟𝑦𝑆𝑒𝑙𝑒𝑐𝑡𝑜𝑟("𝑠𝑒𝑙𝑒𝑐𝑡𝑜𝑟") – Get the first element matching a CSS selector
• 𝑞𝑢𝑒𝑟𝑦𝑆𝑒𝑙𝑒𝑐𝑡𝑜𝑟𝐴𝑙𝑙("𝑠𝑒𝑙𝑒𝑐𝑡𝑜𝑟") – Get all elements matching a CSS selector
Example:
𝑙𝑒𝑡 𝑝𝑎𝑟𝑎𝑔𝑟𝑎𝑝ℎ = 𝑑𝑜𝑐𝑢𝑚𝑒𝑛𝑡. 𝑔𝑒𝑡𝐸𝑙𝑒𝑚𝑒𝑛𝑡𝐵𝑦𝐼𝑑("𝑖𝑛𝑡𝑟𝑜");
𝑝𝑎𝑟𝑎𝑔𝑟𝑎𝑝ℎ. 𝑖𝑛𝑛𝑒𝑟𝑇𝑒𝑥𝑡 = "𝐻𝑒𝑙𝑙𝑜, 𝐸𝑛𝑡𝑒𝑟𝑝𝑟𝑖𝑠𝑒! ";
Manipulating the DOM
The DOM is live and dynamic, meaning changes made using JS immediately reflect in the user's
browser view.
Updating Elements:
• 𝑒𝑙𝑒𝑚𝑒𝑛𝑡. 𝑖𝑛𝑛𝑒𝑟𝐻𝑇𝑀𝐿 – Change or get the HTML content
• 𝑒𝑙𝑒𝑚𝑒𝑛𝑡. 𝑖𝑛𝑛𝑒𝑟𝑇𝑒𝑥𝑡 / 𝑡𝑒𝑥𝑡𝐶𝑜𝑛𝑡𝑒𝑛𝑡 – Change or get the text content
• 𝑒𝑙𝑒𝑚𝑒𝑛𝑡. 𝑠𝑒𝑡𝐴𝑡𝑡𝑟𝑖𝑏𝑢𝑡𝑒(𝑛𝑎𝑚𝑒, 𝑣𝑎𝑙𝑢𝑒) – Modify attributes
• 𝑒𝑙𝑒𝑚𝑒𝑛𝑡. 𝑠𝑡𝑦𝑙𝑒. 𝑝𝑟𝑜𝑝𝑒𝑟𝑡𝑦 – Modify CSS styles
Example:
𝑙𝑒𝑡 𝑏𝑡𝑛 = 𝑑𝑜𝑐𝑢𝑚𝑒𝑛𝑡. 𝑔𝑒𝑡𝐸𝑙𝑒𝑚𝑒𝑛𝑡𝐵𝑦𝐼𝑑("𝑠𝑎𝑣𝑒𝐵𝑡𝑛");
𝑏𝑡𝑛. 𝑠𝑡𝑦𝑙𝑒. 𝑏𝑎𝑐𝑘𝑔𝑟𝑜𝑢𝑛𝑑𝐶𝑜𝑙𝑜𝑟 = "𝑔𝑟𝑒𝑒𝑛";
𝑏𝑡𝑛. 𝑠𝑒𝑡𝐴𝑡𝑡𝑟𝑖𝑏𝑢𝑡𝑒("𝑑𝑖𝑠𝑎𝑏𝑙𝑒𝑑", 𝑡𝑟𝑢𝑒);
Creating and Inserting Elements:
𝑙𝑒𝑡 𝑛𝑒𝑤𝐷𝑖𝑣 = 𝑑𝑜𝑐𝑢𝑚𝑒𝑛𝑡. 𝑐𝑟𝑒𝑎𝑡𝑒𝐸𝑙𝑒𝑚𝑒𝑛𝑡("𝑑𝑖𝑣");
By Daud Anjum 45
ENTERPRISE SYSTEMS SI-342
𝑛𝑒𝑤𝐷𝑖𝑣. 𝑖𝑛𝑛𝑒𝑟𝑇𝑒𝑥𝑡 = "𝑁𝑒𝑤 𝑆𝑒𝑐𝑡𝑖𝑜𝑛";
𝑑𝑜𝑐𝑢𝑚𝑒𝑛𝑡. 𝑏𝑜𝑑𝑦. 𝑎𝑝𝑝𝑒𝑛𝑑𝐶ℎ𝑖𝑙𝑑(𝑛𝑒𝑤𝐷𝑖𝑣);
Removing Elements:
𝑙𝑒𝑡 𝑖𝑡𝑒𝑚 = 𝑑𝑜𝑐𝑢𝑚𝑒𝑛𝑡. 𝑔𝑒𝑡𝐸𝑙𝑒𝑚𝑒𝑛𝑡𝐵𝑦𝐼𝑑("𝑖𝑡𝑒𝑚1");
𝑖𝑡𝑒𝑚. 𝑟𝑒𝑚𝑜𝑣𝑒(); // 𝑀𝑜𝑑𝑒𝑟𝑛 𝑎𝑝𝑝𝑟𝑜𝑎𝑐ℎ
Event Handling in the DOM
Enterprise web apps require interactivity, which is achieved by responding to user actions via
event listeners.
𝑙𝑒𝑡 𝑏𝑡𝑛 = 𝑑𝑜𝑐𝑢𝑚𝑒𝑛𝑡. 𝑔𝑒𝑡𝐸𝑙𝑒𝑚𝑒𝑛𝑡𝐵𝑦𝐼𝑑("𝑠𝑢𝑏𝑚𝑖𝑡");
𝑏𝑡𝑛. 𝑎𝑑𝑑𝐸𝑣𝑒𝑛𝑡𝐿𝑖𝑠𝑡𝑒𝑛𝑒𝑟("𝑐𝑙𝑖𝑐𝑘", 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 () {
𝑎𝑙𝑒𝑟𝑡("𝐹𝑜𝑟𝑚 𝑠𝑢𝑏𝑚𝑖𝑡𝑡𝑒𝑑! ");
});
Events like 𝑐𝑙𝑖𝑐𝑘, 𝑖𝑛𝑝𝑢𝑡, 𝑐ℎ𝑎𝑛𝑔𝑒, 𝑚𝑜𝑢𝑠𝑒𝑜𝑣𝑒𝑟, and 𝑘𝑒𝑦𝑑𝑜𝑤𝑛 are commonly used to handle user
input and interaction in real-time.
DOM in Enterprise Systems
The DOM is crucial for enterprise applications where user interfaces must be dynamic, responsive,
and data-driven. For example:
• Live form validation in HR or financial apps.
• Interactive dashboards with charts and graphs.
• Real-time updates from back-end servers via AJAX or WebSockets.
• Single-page applications (SPAs) using frameworks like React or Angular, which heavily
manipulate the virtual DOM.
ES also use DOM scripting for accessibility enhancements, automation (like filling in forms), and
custom UI workflows.
7. Introduction to jQuery
jQuery is a fast, small, and feature-rich JS library. It simplifies HTML document traversal and
manipulation, event handling, animation, and AJAX with an easy-to-use API that works across a
multitude of browsers. Created by John Resig in 2006, jQuery became one of the most popular JS
libraries in the world, used in countless websites and enterprise applications.
The motto of jQuery is: “Write less, do more.” It allows developers to accomplish complex JS
tasks in fewer lines of code, which significantly improves development efficiency, especially in
large-scale ES where time, compatibility, and performance are critical.
By Daud Anjum 46
ENTERPRISE SYSTEMS SI-342
Why jQuery in Enterprise Systems?
In enterprise web development, especially before modern frameworks like React, Angular, or Vue
dominated the market, jQuery played a central role. Many enterprise systems were built using
legacy code and continue to rely on jQuery due to:
• Cross-browser compatibility
• Reduced development time
• Ease of learning and use
• Lightweight footprint
• Strong community support
Even today, many internal enterprise dashboards, admin panels, and legacy applications still use
jQuery because of its reliability and backward compatibility.
Key Features of jQuery
1) DOM Manipulation
jQuery provides simple syntax for selecting and modifying HTML elements. For example:
$("#ℎ𝑒𝑎𝑑𝑖𝑛𝑔"). 𝑡𝑒𝑥𝑡("𝑊𝑒𝑙𝑐𝑜𝑚𝑒 𝑡𝑜 𝐸𝑛𝑡𝑒𝑟𝑝𝑟𝑖𝑠𝑒 𝑃𝑜𝑟𝑡𝑎𝑙");
2) Event Handling
It streamlines binding event handlers to elements, replacing verbose JS syntax. For example:
$("#𝑠𝑢𝑏𝑚𝑖𝑡𝐵𝑡𝑛"). 𝑐𝑙𝑖𝑐𝑘(𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛() {
𝑎𝑙𝑒𝑟𝑡("𝐹𝑜𝑟𝑚 𝑆𝑢𝑏𝑚𝑖𝑡𝑡𝑒𝑑");
});
3) AJAX Support
jQuery simplifies making asynchronous requests to the server using AJAX. For example:
$. 𝑔𝑒𝑡("𝑑𝑎𝑡𝑎. 𝑝ℎ𝑝", 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛(𝑑𝑎𝑡𝑎) {
$("#𝑟𝑒𝑠𝑢𝑙𝑡"). ℎ𝑡𝑚𝑙(𝑑𝑎𝑡𝑎);
});
This is crucial in ES where real-time data fetching and dynamic updates are required without
refreshing the page.
4) Animations and Effects
jQuery has built-in methods for creating UI effects, such as:
$("#𝑏𝑜𝑥"). 𝑓𝑎𝑑𝑒𝐼𝑛();
$("#𝑏𝑜𝑥"). 𝑠𝑙𝑖𝑑𝑒𝑈𝑝();
These effects enhance the UX, especially in rich enterprise interfaces.
5) Cross-Browser Compatibility
jQuery ensures that your code works uniformly across different browsers like Chrome, Firefox,
Safari, and Internet Explorer – an essential feature in corporate environments where users
might use different or outdated browsers.
Syntax Overview
jQuery syntax is built on a combination of selectors and actions:
$(𝑠𝑒𝑙𝑒𝑐𝑡𝑜𝑟). 𝑎𝑐𝑡𝑖𝑜𝑛();
Example:
By Daud Anjum 47
ENTERPRISE SYSTEMS SI-342
$("𝑝"). ℎ𝑖𝑑𝑒(); // 𝐻𝑖𝑑𝑒𝑠 𝑎𝑙𝑙 <p> 𝑒𝑙𝑒𝑚𝑒𝑛𝑡𝑠 𝑜𝑛 𝑡ℎ𝑒 𝑝𝑎𝑔𝑒
The dollar sign $ is shorthand for the 𝑗𝑄𝑢𝑒𝑟𝑦 function, which wraps a set of elements and returns
a jQuery object with methods you can use to manipulate them.
How jQuery Works Behind the Scenes
When a web page loads, jQuery can hook into the page using the $(𝑑𝑜𝑐𝑢𝑚𝑒𝑛𝑡). 𝑟𝑒𝑎𝑑𝑦()
function:
$(𝑑𝑜𝑐𝑢𝑚𝑒𝑛𝑡). 𝑟𝑒𝑎𝑑𝑦(𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛() {
// 𝐶𝑜𝑑𝑒 𝑡𝑜 𝑏𝑒 𝑒𝑥𝑒𝑐𝑢𝑡𝑒𝑑 𝑎𝑓𝑡𝑒𝑟 𝑡ℎ𝑒 𝐷𝑂𝑀 𝑖𝑠 𝑓𝑢𝑙𝑙𝑦 𝑙𝑜𝑎𝑑𝑒𝑑
});
This ensures that scripts don’t try to manipulate elements that haven’t yet been rendered which is
especially important in complex enterprise web pages.
jQuery and Enterprise Development Patterns
In many enterprise systems, jQuery is used alongside server-side technologies like [Link], PHP,
or Java. It plays a crucial role in:
• Validating forms on the client-side before submission.
• Making dynamic UI updates without server reloads.
• Fetching and rendering data asynchronously via AJAX.
• Building interactive elements like tabs, modals, and collapsible sections.
Many enterprise UI frameworks (such as Bootstrap 3 and 4) also depend on jQuery, making it an
essential skill in legacy development environments.
Limitations and Modern Usage
While jQuery was once the standard, modern JavaScript (ES6+) has absorbed many of its features.
Additionally, frameworks like React, Angular, and Vue offer more structured and component-
based approaches to enterprise UI development.
However, jQuery is still widely used:
• In maintaining or upgrading legacy systems.
• For quick UI enhancements.
• In small-to-medium projects with no need for heavy frameworks.
jQuery Effects
One of the major reasons for jQuery’s widespread adoption – especially in ES – is its rich set of
visual effects and animations that enhance the user interface and improve interactivity without
requiring in-depth knowledge of JS or CSS animations.
jQuery effects are pre-built functions that allow developers to animate elements, control visibility,
and create smooth UI transitions with just a few lines of code. This is especially important in
enterprise applications where UX plays a key role in system efficiency and useability.
By Daud Anjum 48
ENTERPRISE SYSTEMS SI-342
Types of jQuery Effects
jQuery provides several categories of effects, primarily focused on:
1) Visibility Effects
2) Fading Effects
3) Sliding Effects
4) Custom Animations
5) Callback and Chaining Mechanisms
Each of these categories helps enhance the front-end of enterprise applications like dashboards,
data-entry forms, and report interfaces.
1) Visibility Effects
These effects allow elements to appear or disappear from the page, often with a smooth
transition.
• ℎ𝑖𝑑𝑒() – Hides the selected elements
• 𝑠ℎ𝑜𝑤() – Shows hidden elements
• 𝑡𝑜𝑔𝑔𝑙𝑒() – Switches between hiding and showing
$("#𝑖𝑛𝑓𝑜𝐵𝑜𝑥"). ℎ𝑖𝑑𝑒(); // 𝐻𝑖𝑑𝑒𝑠 𝑡ℎ𝑒 𝑏𝑜𝑥
$("#𝑖𝑛𝑓𝑜𝐵𝑜𝑥"). 𝑠ℎ𝑜𝑤(); // 𝑆ℎ𝑜𝑤𝑠 𝑡ℎ𝑒 𝑏𝑜𝑥
$("#𝑖𝑛𝑓𝑜𝐵𝑜𝑥"). 𝑡𝑜𝑔𝑔𝑙𝑒(); // 𝑇𝑜𝑔𝑔𝑙𝑒𝑠 𝑣𝑖𝑠𝑖𝑏𝑖𝑙𝑖𝑡𝑦
Use in ES: Toggle visibility of user controls or sections based on roles, input selection, or data
availability.
2) Fading Effects
Fading effects allow smooth transitions in opacity, giving a subtle appearance/disappearance.
• 𝑓𝑎𝑑𝑒𝐼𝑛(𝑠𝑝𝑒𝑒𝑑) – Fades in the element
• 𝑓𝑎𝑑𝑒𝑂𝑢𝑡(𝑠𝑝𝑒𝑒𝑑) – Fades out the element
• 𝑓𝑎𝑑𝑒𝑇𝑜𝑔𝑔𝑙𝑒(𝑠𝑝𝑒𝑒𝑑) – Toggles fade in/out
• 𝑓𝑎𝑑𝑒𝑇𝑜(𝑠𝑝𝑒𝑒𝑑, 𝑜𝑝𝑎𝑐𝑖𝑡𝑦) – Fades to a specific opacity
$("#𝑚𝑒𝑠𝑠𝑎𝑔𝑒"). 𝑓𝑎𝑑𝑒𝑂𝑢𝑡("𝑠𝑙𝑜𝑤");
$("#𝑝𝑜𝑝𝑢𝑝"). 𝑓𝑎𝑑𝑒𝑇𝑜("𝑓𝑎𝑠𝑡", 0.5);
Use in ES: Fading alerts, error messages, or modal overlays.
3) Sliding Effects
Sliding effects animate elements vertically, ideal for expanding or collapsing sections.
• 𝑠𝑙𝑖𝑑𝑒𝐷𝑜𝑤𝑛(𝑠𝑝𝑒𝑒𝑑) – Slides element down to make it visible
• 𝑠𝑙𝑖𝑑𝑒𝑈𝑝(𝑠𝑝𝑒𝑒𝑑) – Slides element up to hide it
• 𝑠𝑙𝑖𝑑𝑒𝑇𝑜𝑔𝑔𝑙𝑒(𝑠𝑝𝑒𝑒𝑑) – Toggles between slide up and down
$("#𝑓𝑖𝑙𝑡𝑒𝑟𝑃𝑎𝑛𝑒𝑙"). 𝑠𝑙𝑖𝑑𝑒𝐷𝑜𝑤𝑛();
$("#𝑑𝑒𝑡𝑎𝑖𝑙𝑠"). 𝑠𝑙𝑖𝑑𝑒𝑈𝑝();
$("#𝑚𝑒𝑛𝑢"). 𝑠𝑙𝑖𝑑𝑒𝑇𝑜𝑔𝑔𝑙𝑒();
Use in ES: Expandable menus, search filters, and collapsible content panels.
By Daud Anjum 49
ENTERPRISE SYSTEMS SI-342
4) Custom Animations
The 𝑎𝑛𝑖𝑚𝑎𝑡𝑒() function allows developers to create custom animations by changing CSS
properties over time.
Syntax:
$(𝑠𝑒𝑙𝑒𝑐𝑡𝑜𝑟). 𝑎𝑛𝑖𝑚𝑎𝑡𝑒({𝑝𝑟𝑜𝑝𝑒𝑟𝑡𝑦1: 𝑣𝑎𝑙𝑢𝑒1, 𝑝𝑟𝑜𝑝𝑒𝑟𝑡𝑦2: 𝑣𝑎𝑙𝑢𝑒2}, 𝑑𝑢𝑟𝑎𝑡𝑖𝑜𝑛);
Example:
$("#𝑏𝑜𝑥"). 𝑎𝑛𝑖𝑚𝑎𝑡𝑒({
𝑤𝑖𝑑𝑡ℎ: "300𝑝𝑥",
ℎ𝑒𝑖𝑔ℎ𝑡: "200𝑝𝑥",
𝑜𝑝𝑎𝑐𝑖𝑡𝑦: 0.5
}, 1000);
This provides more flexibility than preset effects and is useful when building custom UI
workflows in enterprise applications.
5) Callback Functions
All jQuery effects can accept a callback function – a function that runs after the effect is
complete. This is helpful for sequential actions.
Example:
$("#𝑏𝑜𝑥"). 𝑓𝑎𝑑𝑒𝑂𝑢𝑡(500, 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛() {
𝑎𝑙𝑒𝑟𝑡("𝐵𝑜𝑥 𝑖𝑠 𝑛𝑜𝑤 ℎ𝑖𝑑𝑑𝑒𝑛");
});
Use is ES: Sequential form validation feedback, chained UI interactions, etc.
6) Chaining Effects
jQuery allows method chaining, meaning you can apply multiple effects or actions on the same
element in sequence.
Example:
$("#𝑝𝑎𝑛𝑒𝑙"). 𝑠𝑙𝑖𝑑𝑒𝐷𝑜𝑤𝑛(). 𝑐𝑠𝑠("𝑐𝑜𝑙𝑜𝑟", "𝑏𝑙𝑢𝑒"). 𝑓𝑎𝑑𝑒𝑂𝑢𝑡(2000);
This makes code cleaner and more readable, a benefit for teams maintaining large enterprise
systems.
Performance Consideration in Enterprise Systems
While jQuery effects are lightweight and easy to implement, performance can become an issue
when effects are overused, especially in data-heavy enterprise applications. Developers should:
• Use effects judiciously to avoid overwhelming users
• Prefer hardware-accelerated CSS transitions when working on mobile/tablet UIs
• Minimize animations on critical data-entry interfaces
By Daud Anjum 50
ENTERPRISE SYSTEMS SI-342
8. Introducing LINQ
LINQ (Language Integrated Query) is a powerful feature introduced in .NET Framework 3.5 that
allows developers to write structured, type-safe queries directly in C# or [Link] code. It brings
the power of querying data – traditionally done using SQL or XPath – into the programming
language itself. LINQ integrated query capabilities directly into the language syntax, allowing
developers to query collections, databases, XML documents, and other data sources using a unified
approach.
Why LINQ in Enterprise Systems?
Enterprise systems deal with large volumes of data from multiple sources like relational databases,
XML files, APIs, and in-memory collections. Traditionally, developers had to use different query
languages or APIs (like SQL for databases or XPath for XML). LINQ simplifies this by offering
a consistent query syntax across all types of data, improving developer productivity, code
readability, and maintainability.
It reduces the need for writing separate SQL queries and parsing their results manually, which is
especially helpful in large, data-driven enterprise applications.
Key Benefits of LINQ
1) Unified Query Syntax
Whether you’re querying a database, XML, or a list of objects, LINQ provides a consistent
way to write queries, reducing the learning curve and cognitive load.
2) Compile-time Checking
Since LINQ queries are written in C#, errors are detected at compile-time, unlike traditional
SQL strings which often fail at runtime.
3) IntelliSense Support
Visual Studio provides IntelliSense and autocompletion for LINQ queries, making it easier and
faster to write and debug.
4) Improved Readability and Maintainability
LINQ queries are generally more readable and maintainable than traditional loops and
conditional logic for data filtering and manipulation.
Types of LINQ
LINQ is not limited to databases. It supports querying multiple data types through various LINQ
providers:
• LINQ to Objects – Query in-memory data structures like arrays, lists, etc.
• LINQ to SQL – Query Microsoft SQL Server databases.
• LINQ to Entities – Used with Entity Framework to query databases in an object-oriented
manner.
• LINQ to XML – Query and manipulate XML documents.
• LINQ to DataSet – Works with [Link] datasets.
By Daud Anjum 51
ENTERPRISE SYSTEMS SI-342
• LINQ to JSON – Often used via third-party libraries like [Link] ([Link]).
Basic LINQ Syntax
LINQ supports two syntaxes: Query syntax and Method syntax.
Query Syntax (SQL-like):
𝑣𝑎𝑟 𝑟𝑒𝑠𝑢𝑙𝑡 = 𝑓𝑟𝑜𝑚 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 𝑖𝑛 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠
𝑤ℎ𝑒𝑟𝑒 𝑠𝑡𝑢𝑑𝑒𝑛𝑡. 𝐴𝑔𝑒 > 18
𝑠𝑒𝑙𝑒𝑐𝑡 𝑠𝑡𝑢𝑑𝑒𝑛𝑡;
Method Syntax (Fluent API):
𝑣𝑎𝑟 𝑟𝑒𝑠𝑢𝑙𝑡 = 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝑊ℎ𝑒𝑟𝑒(𝑠 => 𝑠. 𝐴𝑔𝑒 > 18);
Both approaches return the same result and can be used interchangeably, depending on readability
preferences.
How LINQ Works Internally
When a LINQ query is executed, it is transformed into lambda expressions and expression trees
by the compiler. For LINQ to SQL or LINQ to Entities, these are translated into actual SQL queries
and executed on the database server. For LINQ to Objects, the query runs on the in-memory
collection using deferred or immediate execution.
Deferred vs. Immediate Execution
• Deferred Execution: The query is not executed until the data is actually needed (e.g., when
looping through results using 𝑓𝑜𝑟𝑒𝑎𝑐ℎ). This improves performance.
• Immediate Execution: Happens when methods like 𝑇𝑜𝐿𝑖𝑠𝑡(), 𝐶𝑜𝑢𝑛𝑡(), or 𝐹𝑖𝑟𝑠𝑡() are used –
these force the query to execute immediately and store the results.
𝑣𝑎𝑟 𝑞𝑢𝑒𝑟𝑦 = 𝑓𝑟𝑜𝑚 𝑜𝑟𝑑𝑒𝑟 𝑖𝑛 𝑜𝑟𝑑𝑒𝑟𝑠
𝑤ℎ𝑒𝑟𝑒 𝑜𝑟𝑑𝑒𝑟. 𝐴𝑚𝑜𝑢𝑛𝑡 > 500
𝑠𝑒𝑙𝑒𝑐𝑡 𝑜𝑟𝑑𝑒𝑟; // 𝐷𝑒𝑓𝑒𝑟𝑟𝑒𝑑
𝑣𝑎𝑟 𝑟𝑒𝑠𝑢𝑙𝑡 = 𝑞𝑢𝑒𝑟𝑦. 𝑇𝑜𝐿𝑖𝑠𝑡(); // 𝑁𝑜𝑤 𝑒𝑥𝑒𝑐𝑢𝑡𝑒𝑑 𝑖𝑚𝑚𝑒𝑑𝑖𝑎𝑡𝑒𝑙𝑦
LINQ in Real-World Enterprise Applications
In enterprise systems, LINQ is commonly used for:
• Filtering and sorting lists of customers, employees, or products.
• Querying results from Entity Framework ORM.
• Manipulating XML configuration files.
• Integrating with APIs that return JSON/XML and require transformation or filtering.
It helps streamline the business logic layer and reduces repetitive, boilerplate code that
traditionally plagued enterprise development.
By Daud Anjum 52
ENTERPRISE SYSTEMS SI-342
LINQ to Objects
This refers to the use of LINQ to query in-memory collections such as arrays, lists, dictionaries,
and other objects that implement the 𝐼𝐸𝑛𝑢𝑚𝑒𝑟𝑎𝑏𝑙𝑒 or 𝐼𝐸𝑛𝑢𝑚𝑒𝑟𝑎𝑏𝑙𝑒<T> interface in .NET. It
allows developers to write declarative queries using standard LINQ syntax directly on data that
already exists in memory, without reading a database or external data source.
Unlike LINQ to SQL or LINQ to Entities, LINQ to Objects doesn’t translate queries into SQL or
any other external query language. Instead, it executes queries in-process, making it very efficient
and useful for quick, type-safe, and readable data manipulation within the applications.
Why LINQ to Objects in Enterprise Systems?
ES often require complex data processing logic over in-memory data, such as:
• Filtering employee records from an API
• Aggregating in-memory reports
• Sorting lists of products or transactions
• Performing business logic on collections retrieved from user input or service layers
Before LINQ, such tasks involve loops, conditional statements, and lots of verbose code. LINQ to
Object simplifies this by providing a concise, readable, and consistent syntax to work with in-
memory collections – ideal for enterprise-level applications where clarity and maintainability are
essential.
Features of LINQ to Objects
1) Type-Safe Queries: Since LINQ is integrated into the C# language, it provides compile-time
checking and IntelliSense support in IDEs like Visual Studio.
2) Extensibility: You can create custom LINQ extension methods for repeated operations.
3) Deferred Execution: Many LINQ queries don’t execute until the result is enumerated, which
allows flexible and efficient memory usage.
4) Chained Operations: LINQ methods can be chained for clean, readable query pipelines.
Examples of LINQ to Objects
Basic Filtering
𝐿𝑖𝑠𝑡<𝑖𝑛𝑡> 𝑛𝑢𝑚𝑏𝑒𝑟𝑠 = 𝑛𝑒𝑤 𝐿𝑖𝑠𝑡<𝑖𝑛𝑡> { 1, 2, 3, 4, 5, 6 };
𝑣𝑎𝑟 𝑒𝑣𝑒𝑛𝑁𝑢𝑚𝑏𝑒𝑟𝑠 = 𝑓𝑟𝑜𝑚 𝑛 𝑖𝑛 𝑛𝑢𝑚𝑏𝑒𝑟𝑠
𝑤ℎ𝑒𝑟𝑒 𝑛 % 2 == 0
𝑠𝑒𝑙𝑒𝑐𝑡 𝑛;
Using Method Syntax
𝑣𝑎𝑟 𝑒𝑣𝑒𝑛𝑁𝑢𝑚𝑏𝑒𝑟𝑠 = 𝑛𝑢𝑚𝑏𝑒𝑟𝑠. 𝑊ℎ𝑒𝑟𝑒 (𝑛 => 𝑛 % 2 == 0). 𝑇𝑜𝐿𝑖𝑠𝑡();
Sorting and Selecting
𝐿𝑖𝑠𝑡<𝑠𝑡𝑟𝑖𝑛𝑔> 𝑛𝑎𝑚𝑒𝑠 = 𝑛𝑒𝑤 𝐿𝑖𝑠𝑡<𝑠𝑡𝑟𝑖𝑛𝑔> { "𝐽𝑜ℎ𝑛", "𝐴𝑙𝑖𝑐𝑒", "𝐵𝑜𝑏" };
𝑣𝑎𝑟 𝑠𝑜𝑟𝑡𝑒𝑑𝑁𝑎𝑚𝑒𝑠 = 𝑛𝑎𝑚𝑒𝑠. 𝑂𝑟𝑑𝑒𝑟𝐵𝑦(𝑛𝑎𝑚𝑒 => 𝑛𝑎𝑚𝑒). 𝑆𝑒𝑙𝑒𝑐𝑡(𝑛𝑎𝑚𝑒 =>
𝑛𝑎𝑚𝑒. 𝑇𝑜𝑈𝑝𝑝𝑒𝑟());
By Daud Anjum 53
ENTERPRISE SYSTEMS SI-342
Aggregating Data
𝑖𝑛𝑡 𝑠𝑢𝑚 = 𝑛𝑢𝑚𝑏𝑒𝑟𝑠. 𝑆𝑢𝑚();
𝑑𝑜𝑢𝑏𝑙𝑒 𝑎𝑣𝑒𝑟𝑎𝑔𝑒 = 𝑛𝑢𝑚𝑏𝑒𝑟𝑠. 𝐴𝑣𝑒𝑟𝑎𝑔𝑒();
These operations are particularly useful in enterprise software like reporting dashboards, inventory
modules, or HR systems where quick data manipulation is needed without hitting the database.
Working with Complex Types
LINQ to Objects is not limited to primitive types. It works just as efficiently with user-defined
classes:
𝑐𝑙𝑎𝑠𝑠 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒 {
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑁𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝑆𝑎𝑙𝑎𝑟𝑦 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
}
𝐿𝑖𝑠𝑡<𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒> 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠 = 𝑛𝑒𝑤 𝐿𝑖𝑠𝑡<𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒> {
𝑛𝑒𝑤 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒 { 𝑁𝑎𝑚𝑒 = "𝐴𝑙𝑖", 𝑆𝑎𝑙𝑎𝑟𝑦 = 50000 },
𝑛𝑒𝑤 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒 { 𝑁𝑎𝑚𝑒 = "𝑆𝑎𝑟𝑎", 𝑆𝑎𝑙𝑎𝑟𝑦 = 70000 },
𝑛𝑒𝑤 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒 { 𝑁𝑎𝑚𝑒 = "𝐽𝑜ℎ𝑛", 𝑆𝑎𝑙𝑎𝑟𝑦 = 60000 }
};
𝑣𝑎𝑟 ℎ𝑖𝑔ℎ𝐸𝑎𝑟𝑛𝑒𝑟𝑠 = 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠. 𝑊ℎ𝑒𝑟𝑒(𝑒 => 𝑒. 𝑆𝑎𝑙𝑎𝑟𝑦 > 60000). 𝑇𝑜𝐿𝑖𝑠𝑡();
This example shows how LINQ to Objects helps filter complex data structures, a common
requirement in enterprise-level HR, finance, or CRM systems.
Deferred vs Immediate Execution
LINQ to Object supports both deferred and immediate execution:
• Deferred Execution: The query is not executed when defined but only when the result is used,
like in a 𝑓𝑜𝑟𝑒𝑎𝑐ℎ loop.
• Immediate Execution: Occurs when methods like . 𝑇𝑜𝐿𝑖𝑠𝑡() or . 𝐶𝑜𝑢𝑛𝑡() are used.
This distinction allows developers to control memory and performance better, which is crucial for
scalable enterprise software.
Chaining and Composition
You can chain multiple LINQ methods together for elegant query pipelines:
𝑣𝑎𝑟 𝑟𝑒𝑠𝑢𝑙𝑡 = 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠
. 𝑊ℎ𝑒𝑟𝑒(𝑒 => 𝑒. 𝑆𝑎𝑙𝑎𝑟𝑦 > 50000)
. 𝑂𝑟𝑑𝑒𝑟𝐵𝑦(𝑒 => 𝑒. 𝑁𝑎𝑚𝑒)
. 𝑆𝑒𝑙𝑒𝑐𝑡(𝑒 => 𝑒. 𝑁𝑎𝑚𝑒);
This improves code readability and maintainability, making it easier for teams to collaborate and
modify enterprise applications over time.
By Daud Anjum 54
ENTERPRISE SYSTEMS SI-342
Limitations of LINQ to Objects
While LINQ to Objects is powerful, it operates in memory only and cannot be used to directly
query a database or external XML document. For those scenarios, LINQ to SQL, LINQ to XML,
or LINQ to Entities must be used.
Also, LINQ to Objects may be less efficient than traditional loops for extremely large datasets
because of its reliance on deferred execution and iterator patterns. Performance tuning may be
required for high-throughput enterprise systems.
LINQ to SQL
It is a component of the .NET Framework that provides a runtime infrastructure of managing
relational data as objects without losing the ability to query. It enables developers to interact with
Microsoft SQL Server databases using LINQ syntax directly in C# or [Link]. Instead of writing
traditional SQL queries as strings, developers use strongly typed LINQ expressions to fetch,
manipulate, and update data.
LINQ to SQL bridges the gap between relational data (stored in SQL Server) and object-oriented
programming (in .NET). It allows for a more seamless and maintainable integration of database
access into .NET applications.
Why Use LINQ to SQL in Enterprise Systems?
In ES, data is usually stored in relational databases like SQL Server, and traditional data access
required writing [Link] code or raw SQL queries. This was error-prone, less secure, and harder
to maintain. LINQ to SQL simplifies data access by allowing developers to:
• Use object-oriented code instead of SQL
• Avoid manual parsing of query results
• Benefit from compile-time checking and IntelliSense
• Maintain cleaner and more consistent business logic
It’s especially helpful in small to medium-sized enterprise applications that need quick and
straightforward database interaction.
Key Features of LINQ to SQL
1) ORM Capabilities: LINQ to SQL acts as a lightweight Object Relational Mapper (ORM). It
maps database tables to .NET classes and table rows to objects.
2) Strongly-Typed Queries: Queries are written in C# or [Link], making them type-safe and
catching errors at compile-time.
3) Change Tracking: It tracks changes made to objects and automatically generates the necessary
SQL to update the database.
4) Deferred Execution: Queries are executed only when their results are needed.
5) Auto-generated Classes: Visual Studio provides a designer tool to generate mapped classes
from database tables.
By Daud Anjum 55
ENTERPRISE SYSTEMS SI-342
Architecture of LINQ to SQL
The process of using LINQ to SQL generally involves the following steps:
1) Define the Data Context: This is a class derived from 𝐷𝑎𝑡𝑎𝐶𝑜𝑛𝑡𝑒𝑥𝑡 that manages the database
connection and acts as a gateway to the database.
2) Map Database Tables to Classes: Tables are represented by C# classes with properties
corresponding to columns.
3) Write LINQ Queries: You can now use LINQ to perform operations like SELECT, INSERT,
UPDATE, DELETE.
4) Submit Changes: Any changes to objects are persisted to the database using
𝑆𝑢𝑏𝑚𝑖𝑡𝐶ℎ𝑎𝑛𝑔𝑒𝑠().
Example of LINQ to SQL
Suppose we have a table 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠 in SQL Server.
Step 1: Create DataContext
𝐷𝑎𝑡𝑎𝐶𝑜𝑛𝑡𝑒𝑥𝑡 𝑑𝑏 = 𝑛𝑒𝑤 𝐷𝑎𝑡𝑎𝐶𝑜𝑛𝑡𝑒𝑥𝑡(@"𝑦𝑜𝑢𝑟_𝑐𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛_𝑠𝑡𝑟𝑖𝑛𝑔");
Step 2: Define a Mapped Class
[𝑇𝑎𝑏𝑙𝑒(𝑁𝑎𝑚𝑒 = "𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 {
[𝐶𝑜𝑙𝑢𝑚𝑛(𝐼𝑠𝑃𝑟𝑖𝑚𝑎𝑟𝑦𝐾𝑒𝑦 = 𝑡𝑟𝑢𝑒)]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝐼𝐷 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
[𝐶𝑜𝑙𝑢𝑚𝑛]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑁𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
[𝐶𝑜𝑙𝑢𝑚𝑛]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝐴𝑔𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
}
Step 3: Query Data
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 = 𝑓𝑟𝑜𝑚 𝑠 𝑖𝑛 𝑑𝑏. 𝐺𝑒𝑡𝑇𝑎𝑏𝑙𝑒<𝑆𝑡𝑢𝑑𝑒𝑛𝑡>()
𝑤ℎ𝑒𝑟𝑒 𝑠. 𝐴𝑔𝑒 > 18
𝑠𝑒𝑙𝑒𝑐𝑡 𝑠;
𝑓𝑜𝑟𝑒𝑎𝑐ℎ (𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 𝑖𝑛 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠)
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒(𝑠𝑡𝑢𝑑𝑒𝑛𝑡. 𝑁𝑎𝑚𝑒);
}
Step 4: Insert Data
𝑆𝑡𝑢𝑑𝑒𝑛𝑡 𝑛𝑒𝑤𝑆𝑡𝑢𝑑𝑒𝑛𝑡 = 𝑛𝑒𝑤 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 { 𝐼𝐷 = 5, 𝑁𝑎𝑚𝑒 = "𝐴𝑙𝑖", 𝐴𝑔𝑒 = 22 };
𝑑𝑏. 𝐺𝑒𝑡𝑇𝑎𝑏𝑙𝑒<𝑆𝑡𝑢𝑑𝑒𝑛𝑡>(). 𝐼𝑛𝑠𝑒𝑟𝑡𝑂𝑛𝑆𝑢𝑏𝑚𝑖𝑡(𝑛𝑒𝑤𝑆𝑡𝑢𝑑𝑒𝑛𝑡);
𝑑𝑏. 𝑆𝑢𝑏𝑚𝑖𝑡𝐶ℎ𝑎𝑛𝑔𝑒𝑠();
Benefits in Enterprise Environments
• Rapid Development: Speeds up database programming for enterprise developers
By Daud Anjum 56
ENTERPRISE SYSTEMS SI-342
• Maintainability: Easier to read and maintain than raw SQL queries
• Integrated Debugging: Easier to debug inside Visual Studio
• Security: Eliminates risks of SQL injection by using parameterized queries under the hood.
• Consistency: Helps keep data access code consistent across modules.
Limitations of LINQ to SQL
Despite its benefits, LINQ to SQL also has some drawbacks, especially in large-scale or complex
enterprise applications:
• Limited to SQL Server: LINQ to SQL only works with Microsoft SQL Server. For other
databases, developers must use Entity Framework or other ORMs.
• Not Ideal for Complex Databases: It doesn’t handle complex relationships, stored procedures,
or advanced mappings as effectively as Entity Framework.
• No Built-in Support for Multi-Database Models: It's not suited for applications using multiple
database systems or large-scale enterprise architectures.
LINQ to Objects vs. LINQ to SQL
Feature LINQ to Objects LINQ to SQL
Data Source In-memory collections (e.g., SQL Server database tables
arrays, lists, dictionaries)
Namespace 𝑆𝑦𝑠𝑡𝑒𝑚. 𝐿𝑖𝑛𝑞 𝑆𝑦𝑠𝑡𝑒𝑚. 𝐷𝑎𝑡𝑎. 𝐿𝑖𝑛𝑞
Query Execution Performed on in-memory data Translated into SQL and
executed on the database
server
Performance Fast for small data sets; Efficient for large data sets;
limited by system memory offloads processing to SQL
Server
Type of Query Language Pure .NET language queries Queries are converted to SQL
before execution
Data Storage Data is already loaded in Data resides in a relational
memory database (SQL Server)
Deferred Execution Supported Supported
CRUD Operations Manual (add/remove items in Automatically generates SQL
collections) for INSERT, UPDATE,
DELETE
ORM Support Not applicable Acts a lightweight ORM
Change Tracking Not provided automatically Built-in support via
𝐷𝑎𝑡𝑎𝐶𝑜𝑛𝑡𝑒𝑥𝑡
Usage Scenario When data is already in When working with persistent
memory data in a SQL Server database
Dependency Works with any .NET Requires SQL Server and
collection LINQ to SQL-specific setup
Tooling Support (Visual Not required Offers designer tools for
Studio) mapping classes to database
tables
By Daud Anjum 57
ENTERPRISE SYSTEMS SI-342
Security (e.g., SQL injection) Not applicable Safe from SQL injection due
to parameterized query
generation
Query Syntax
Query Syntax in LINQ refers to a SQL-like, declarative syntax used to write LINQ queries within
C# or [Link]. It is often called "comprehension syntax" because it resembles SQL, making it
more intuitive for those familiar with traditional database queries.
Query syntax offers a readable and expressive way to query collections or databases directly from
.NET code. It is particularly useful in ES where complex data manipulation and retrieval are
frequent.
Structure of a Query Syntax Expression
The basic structure of a LINQ query using query syntax follows this pattern:
𝑓𝑟𝑜𝑚 <𝑟𝑎𝑛𝑔𝑒 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒> 𝑖𝑛 <𝑑𝑎𝑡𝑎 𝑠𝑜𝑢𝑟𝑐𝑒>
𝑤ℎ𝑒𝑟𝑒 <𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛>
𝑠𝑒𝑙𝑒𝑐𝑡 <𝑟𝑒𝑠𝑢𝑙𝑡 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛>;
• 𝑓𝑟𝑜𝑚: Specifies the data source
• 𝑤ℎ𝑒𝑟𝑒: Applies a filter condition
• 𝑠𝑒𝑙𝑒𝑐𝑡: Projects the final result
This syntax works with both in-memory collections (via LINQ to Objects) and databases (via
LINQ to SQL or Entity Framework), with the underlying engine converting it to either method
calls or SQL queries.
Example: Query Syntax with LINQ to Objects
𝑖𝑛𝑡 [ ] 𝑛𝑢𝑚𝑏𝑒𝑟𝑠 = { 1, 2, 3, 4, 5, 6 };
𝑣𝑎𝑟 𝑒𝑣𝑒𝑛𝑁𝑢𝑚𝑏𝑒𝑟𝑠 = 𝑓𝑟𝑜𝑚 𝑛𝑢𝑚 𝑖𝑛 𝑛𝑢𝑚𝑏𝑒𝑟𝑠
𝑤ℎ𝑒𝑟𝑒 𝑛𝑢𝑚 % 2 == 0
𝑠𝑒𝑙𝑒𝑐𝑡 𝑛𝑢𝑚;
𝑓𝑜𝑟𝑒𝑎𝑐ℎ (𝑣𝑎𝑟 𝑛 𝑖𝑛 𝑒𝑣𝑒𝑛𝑁𝑢𝑚𝑏𝑒𝑟𝑠)
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒 (𝑛);
}
This is syntactically similar to SQL: 𝑆𝐸𝐿𝐸𝐶𝑇 𝑛𝑢𝑚 𝐹𝑅𝑂𝑀 𝑛𝑢𝑚𝑏𝑒𝑟𝑠 𝑊𝐻𝐸𝑅𝐸 𝑛𝑢𝑚 % 2 == 0.
Supported Clauses in Query Syntax
Query Syntax supports many clauses that enable complex queries:
1) 𝑓𝑟𝑜𝑚: Declares the data source and iteration variable
By Daud Anjum 58
ENTERPRISE SYSTEMS SI-342
2) 𝑤ℎ𝑒𝑟𝑒: Filters data based on a Boolean condition
3) 𝑠𝑒𝑙𝑒𝑐𝑡: Defines the shape of the returned result
4) 𝑜𝑟𝑑𝑒𝑟𝑏𝑦 / 𝑜𝑟𝑑𝑒𝑟𝑏𝑦 𝑑𝑒𝑠𝑐𝑒𝑛𝑑𝑖𝑛𝑔: Sorts data based on one or more keys
5) 𝑔𝑟𝑜𝑢𝑝 𝑏𝑦: Groups elements that share a common key
6) 𝑗𝑜𝑖𝑛: Combines two data sources based on a key
7) 𝑙𝑒𝑡: Introduces a new variable to store intermediate computations
8) 𝑖𝑛𝑡𝑜: Continues a query after a 𝑔𝑟𝑜𝑢𝑝 𝑏𝑦 or 𝑠𝑒𝑙𝑒𝑐𝑡 clause
Example: Query Syntax with 𝑜𝑟𝑑𝑒𝑟𝑏𝑦 and 𝑔𝑟𝑜𝑢𝑝
𝑠𝑡𝑟𝑖𝑛𝑔[ ] 𝑛𝑎𝑚𝑒𝑠 = { "𝐴𝑙𝑖", "𝑍𝑎𝑟𝑎", "𝐵𝑖𝑙𝑎𝑙", "𝐴ℎ𝑚𝑒𝑑", "𝑍𝑎𝑖𝑛" };
𝑣𝑎𝑟 𝑔𝑟𝑜𝑢𝑝𝑒𝑑𝐵𝑦𝐹𝑖𝑟𝑠𝑡𝐿𝑒𝑡𝑡𝑒𝑟 = 𝑓𝑟𝑜𝑚 𝑛𝑎𝑚𝑒 𝑖𝑛 𝑛𝑎𝑚𝑒𝑠
𝑜𝑟𝑑𝑒𝑟𝑏𝑦 𝑛𝑎𝑚𝑒
𝑔𝑟𝑜𝑢𝑝 𝑛𝑎𝑚𝑒 𝑏𝑦 𝑛𝑎𝑚𝑒[0] 𝑖𝑛𝑡𝑜 𝑔
𝑠𝑒𝑙𝑒𝑐𝑡 𝑔;
𝑓𝑜𝑟𝑒𝑎𝑐ℎ (𝑣𝑎𝑟 𝑔𝑟𝑜𝑢𝑝 𝑖𝑛 𝑔𝑟𝑜𝑢𝑝𝑒𝑑𝐵𝑦𝐹𝑖𝑟𝑠𝑡𝐿𝑒𝑡𝑡𝑒𝑟)
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒("𝐺𝑟𝑜𝑢𝑝: " + 𝑔𝑟𝑜𝑢𝑝. 𝐾𝑒𝑦);
𝑓𝑜𝑟𝑒𝑎𝑐ℎ (𝑣𝑎𝑟 𝑛𝑎𝑚𝑒 𝑖𝑛 𝑔𝑟𝑜𝑢𝑝)
{
𝐶𝑜𝑛𝑠𝑜𝑙𝑒. 𝑊𝑟𝑖𝑡𝑒𝐿𝑖𝑛𝑒(" − " + 𝑛𝑎𝑚𝑒);
}
}
This query:
• Orders names alphabetically
• Groups them by their first letter
• Displays the grouped names
Query Syntax vs Method Syntax
LINQ also supports Method Syntax, which uses method calls and lambda expressions. Both
syntaxes are functionally equivalent and can be mixed.
Feature Query Syntax Method Syntax
Readability More readable for SQL users More concise for functional
programmers
Flexibility Limited for some advanced Supports full range of LINQ
operations operators
Learning Curve Easier for beginners Requires understanding of
delegates/lambdas
Common Use 𝑓𝑟𝑜𝑚, 𝑤ℎ𝑒𝑟𝑒, 𝑠𝑒𝑙𝑒𝑐𝑡, . 𝑊ℎ𝑒𝑟𝑒(), . 𝑆𝑒𝑙𝑒𝑐𝑡(),
𝑔𝑟𝑜𝑢𝑝 . 𝐺𝑟𝑜𝑢𝑝𝐵𝑦(), etc.
By Daud Anjum 59
ENTERPRISE SYSTEMS SI-342
Use in Enterprise Systems
In enterprise-level applications, especially those with large teams and complex logic, query syntax
provides:
• Maintainability: SQL-like format is easier to understand for developers from different
backgrounds.
• Consistency: Encourages writing consistent, readable data-access logic.
• Integration: Cleanly integrates with LINQ to SQL, Entity Framework, and XML data sources.
It is particularly valuable when collaborating with database professionals or working on systems
that require transparency in how data is accessed and filtered.
Operations (Projection, Filtering, and Join) using LINQ Queries
LINQ allows developers to perform powerful operations on collections and data sources in a type-
safe, concise, and readable manner. Among the most essential LINQ operations used in ES are:
• Projection: Transforming or selecting specific fields from a data source.
• Filtering: Selecting elements that satisfy specific conditions.
• Join: Combining data from two or more sources based on a key.
These operations are critical in enterprise applications where querying and transforming data are
daily tasks. LINQ supports these operations both in query syntax and method syntax.
Projection
It refers to the operation of transforming or extracting specific fields from data. In LINQ, the
𝑠𝑒𝑙𝑒𝑐𝑡 clause or the 𝑆𝑒𝑙𝑒𝑐𝑡( ) method is used for projection.
It is useful when only a subset of fields from a collection or database is needed – reducing memory
usage and improving performance.
Example: Query Syntax
𝑣𝑎𝑟 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠 = 𝑓𝑟𝑜𝑚 𝑒𝑚𝑝 𝑖𝑛 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐿𝑖𝑠𝑡
𝑠𝑒𝑙𝑒𝑐𝑡 𝑛𝑒𝑤 { 𝑒𝑚𝑝. 𝑁𝑎𝑚𝑒, 𝑒𝑚𝑝. 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 };
This query projects only the 𝑁𝑎𝑚𝑒 and 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 fields from each employee object, ignoring
the rest of the data.
Example: Method Syntax
𝑣𝑎𝑟 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠 = 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐿𝑖𝑠𝑡. 𝑆𝑒𝑙𝑒𝑐𝑡(𝑒𝑚𝑝 =>
𝑛𝑒𝑤 { 𝑒𝑚𝑝. 𝑁𝑎𝑚𝑒, 𝑒𝑚𝑝. 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 });
Both achieve the same goal but use different syntaxes.
Filtering
It means selecting only those elements that satisfy a specific condition. In LINQ, the 𝑤ℎ𝑒𝑟𝑒 clause
or the 𝑊ℎ𝑒𝑟𝑒( ) method is used for filtering.
Filtering is essential in any enterprise application to work with relevant subsets of large datasets.
By Daud Anjum 60
ENTERPRISE SYSTEMS SI-342
Example: Query Syntax
𝑣𝑎𝑟 ℎ𝑖𝑔ℎ𝑆𝑎𝑙𝑎𝑟𝑦𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠 = 𝑓𝑟𝑜𝑚 𝑒𝑚𝑝 𝑖𝑛 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐿𝑖𝑠𝑡
𝑤ℎ𝑒𝑟𝑒 𝑒𝑚𝑝. 𝑆𝑎𝑙𝑎𝑟𝑦 > 50000
𝑠𝑒𝑙𝑒𝑐𝑡 𝑒𝑚𝑝;
This retrieves employees whose salary is greater than 50,000.
Example: Method Syntax
𝑣𝑎𝑟 ℎ𝑖𝑔ℎ𝑆𝑎𝑙𝑎𝑟𝑦𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠 = 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐿𝑖𝑠𝑡. 𝑊ℎ𝑒𝑟𝑒(𝑒𝑚𝑝 => 𝑒𝑚𝑝. 𝑆𝑎𝑙𝑎𝑟𝑦 >
50000);
Filtering can also involve multiple conditions using logical operators (&&, ||).
Join
It is used to combine two or more data sources based on a related key. In LINQ, the 𝑗𝑜𝑖𝑛 keyword
or the 𝐽𝑜𝑖𝑛( ) method is used.
It is crucial in ES where data resides in different tables, such as joining employees with their
respective departments.
Example: Query Syntax
𝑣𝑎𝑟 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐷𝑒𝑡𝑎𝑖𝑙𝑠 = 𝑓𝑟𝑜𝑚 𝑒𝑚𝑝 𝑖𝑛 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐿𝑖𝑠𝑡
𝑗𝑜𝑖𝑛 𝑑𝑒𝑝𝑡 𝑖𝑛 𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡𝐿𝑖𝑠𝑡
𝑜𝑛 𝑒𝑚𝑝. 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡𝐼𝑑 𝑒𝑞𝑢𝑎𝑙𝑠 𝑑𝑒𝑝𝑡. 𝐼𝑑
𝑠𝑒𝑙𝑒𝑐𝑡 𝑛𝑒𝑤 { 𝑒𝑚𝑝. 𝑁𝑎𝑚𝑒, 𝑑𝑒𝑝𝑡. 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡𝑁𝑎𝑚𝑒 };
This joins the employee list with the department list where 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡𝐼𝑑 matches 𝐼𝑑.
Example: Method Syntax
𝑣𝑎𝑟 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐷𝑒𝑡𝑎𝑖𝑙𝑠 = 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐿𝑖𝑠𝑡. 𝐽𝑜𝑖𝑛(𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡𝐿𝑖𝑠𝑡,
𝑒𝑚𝑝 => 𝑒𝑚𝑝. 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡𝐼𝑑,
𝑑𝑒𝑝𝑡 => 𝑑𝑒𝑝𝑡. 𝐼𝑑,
(𝑒𝑚𝑝, 𝑑𝑒𝑝𝑡) => 𝑛𝑒𝑤 { 𝑒𝑚𝑝. 𝑁𝑎𝑚𝑒, 𝑑𝑒𝑝𝑡. 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡𝑁𝑎𝑚𝑒 });
The result is a combined object containing information from both data sources.
Advanced Joins
LINQ also supports:
• Group Join: For hierarchical data representation
• Multiple Join: Chaining multiple 𝑗𝑜𝑖𝑛 clauses
• Left Join: Includes all elements from the left collection even if no match is found
Enterprise Relevance
In enterprise applications:
• Projection is used for shaping data for reports, UI rendering, or API responses.
• Filtering is essential for user-specific data, access control, and search functionalities.
• Joining helps correlate data from different sources like customer orders, product catalogues,
and employee records.
Using LINQ for these operations enhances code readability, type-safety, maintainability, and
developer productivity, all of which are critical in enterprise environments.
By Daud Anjum 61
ENTERPRISE SYSTEMS SI-342
9. Introduction to [Link] Entity Framework
[Link] Entity Framework (EF) is an Object-Relational Mapping (ORM) framework
developed by Microsoft as part of the .NET ecosystem. It enables developers to interact with
relational databases using .NET objects, eliminating the need to write most of the data access code
manually. Essentially, EF allows developers to map database tables to .NET classes, and then use
those classes to perform database operations like querying, inserting, updating, and deleting data
– all without writing raw SQL.
Entity Framework sits on top of [Link], the traditional .NET data access technology, and
simplifies many of the lower-level database programming tasks. It provides a higher level of
abstraction, letting developers focus on their application’s logic and data models rather than
database internals.
Why EF is Important in Enterprise Systems?
In enterprise applications, working with relational databases is fundamental. Large systems often
require the manipulation of large and complex datasets, relationship among tables, and frequent
changes in business logic. Traditionally, developers would use [Link] DataSets or SQL queries
with commands and connections, which could become tedious and error-prone.
Entity Framework offers a more maintainable, scalable, and efficient way to handle data. With EF,
developers can:
• Model the database using C# classes
• Use LINQ to query data
• Automatically generate and synchronize the database schema from the model
• Perform updates using strongly types objects and relationships
These features make EF an ideal choice for enterprise systems, where maintainability, speed of
development, and data integrity are crucial.
Key Features of Entity Framework
1) Object-Relational Mapping (ORM)
EF maps relational database tables to .NET classes and columns to class properties, enabling
object-oriented interaction with data.
2) Modelling Approaches
EF supports three main approaches to modelling:
• Database-First: Generate the model from an existing database
• Model-First: Design the model visually and generate the database from it
• Code-First: Define your model in code and let EF create the database
3) LINQ Support
Queries can be written using LINQ, which provides type safety, IntelliSense support, and
compile-time checking.
By Daud Anjum 62
ENTERPRISE SYSTEMS SI-342
4) Change Tracking and Transactions
EF tracks changes made to objects and manages transactions automatically when
𝑆𝑎𝑣𝑒𝐶ℎ𝑎𝑛𝑔𝑒𝑠( ) is called.
5) Migration Support
With Code-First Migrations, EF can apply schema changes to the database without data loss,
useful for iterative development in large systems.
How EF Simplifies Development
Without EF:
• Developers manually write SQL queries
• Manually manage connections and transactions
• Manually map results to .NET objects
With EF:
• SQL is auto-generated based on object queries
• Database connections and object tracking are handled by EF
• Insert/update/delete operations are performed on objects directly
This greatly reduces development time and the risk of introducing bugs due to SQL mismatches
or incorrect mappings.
The Role of EF in Modern Applications
EF has evolved into a powerful ORM with wide adoption in the enterprise world. With the release
of Entity Framework Core (EF Core), Microsoft modernized the framework to support cross-
platform development (.NET Core), making EF even more relevant for cloud-based, distributed,
and microservice-based systems.
The Entity Data Model
The Entity Data Model (EDM) is the core conceptual model behind the [Link] Entity
Framework. It acts as a blueprint or schema that defines the structure of data in terms of entities
and their relationships, abstracting away the underlying database details. In simple terms, EDM
allows developers to interact with the database using domain-specific objects rather than dealing
with raw tables and columns
The goal of EDM is to provide a higher-level abstraction of data that aligns more closely with how
developers and business users think about data – through objects and relationships – instead of the
rigid structure of a relational database.
Main Components of the EDM
The Entity Data Model is made up of three interrelated parts, often referred to as CSDL, SSDL,
and MSL:
By Daud Anjum 63
ENTERPRISE SYSTEMS SI-342
1) CSDL (Conceptual Schema Definition Language)
The describes the conceptual model, i.e., the object model exposed to the application. It defines
the entities, their properties, and the relationships between them as they appear in the
application code. This is independent of the database structure.
2) SSDL (Storage Schema Definition Language)
This describes the storage model, i.e., the structure of the database. It includes tables, columns,
constraints, keys, and relationships as they are defined in the relational database.
3) MSL (Mapping Specification Language)
This defines the mapping between the conceptual model (CSDL) and the storage model
(SSDL). It ensures that operations performed on the object model are correctly translated to
SQL queries on the database.
These three layers allow developers to separate how the data is structured (database schema) from
how the data is consumed (object model).
Entities and Relationships
An entity in EDM represents a real-world concept or object, such as 𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟, 𝑂𝑟𝑑𝑒𝑟, or
𝑃𝑟𝑜𝑑𝑢𝑐𝑡. Each entity is modelled as a class in .NET and typically maps to a table in the database.
Properties of the entity represent the columns of that table, while navigation properties are used to
represent relationships with other entities (such as foreign keys).
Relationships in EDM can be:
• One-to-One (e.g., User and Profile)
• One-to-Many (e.g., Customer and Orders)
• Many-to-Many (e.g., Students and Courses)
These relationships are defined in the model and are handled through object references and
collections, simplifying complex join operations in code.
Benefits of the EDM in Enterprise Systems
1) Abstraction
EDM abstracts the database layer, allowing developers to work with domain-specific classes
rather than raw SQL or table structures. This makes the code more readable and maintainable.
2) Code Reusability
Since the EDM maps database tables to .NET classes, the same object model can be reused
across different parts of the application (UI, business logic, data access).
3) Consistency
With a central model defining the structure and relationships of data, the chances of
inconsistencies are reduced across different layers of the application.
4) Productivity
Developers can spend more time solving business problems rather than managing boilerplate
data access code, thanks to the automation provided by the EDM.
By Daud Anjum 64
ENTERPRISE SYSTEMS SI-342
5) Support for LINQ
Since EDM works seamlessly with LINQ, developers can write type-safe queries using C# or
[Link] that are translated into SQL behind the scenes.
Visual Representation and Tools
Microsoft provides tools like the Entity Data Model Designer in Visual Studio that lets developers
visually create and manage the EDM. This designer allows drag-and-drop functionality to define
entities, properties, and relationships, and can generate the necessary CSDL, SSDL, and MSL
files.
Alternatively, using Code First or Database First approaches, EDM can be generated automatically
from the database or from classes, making it adaptable to different development styles.
CSDL
CSDL, which stands for Conceptual Schema Definition Language, is one of the three key
components of the EDM in [Link] Entity Framework. It defines the conceptual model – a
high-level, abstract representation of the data that the application will use. Unlike the actual
structure of the database, CSDL represents how data is perceived and handled within the
application layer, using an object-oriented approach.
The CSDL is written in XML and describes the entities (classes), their properties (fields), and
relationships (like associations and navigation properties) as they appear in the .NET application.
It abstracts away the underlying SQL database tables and focuses instead on business-centric
entities.
Purpose and Role of CSDL
The primary role of CSDL is to act as the blueprint for how the application should interact with
data. It tells the Entity Framework how to:
• Represent tables as objects (entities)
• Treat columns as properties
• Understand relationships like one-to-many or many-to-many
• Enable LINQ queries by providing a structured, strongly typed model
This schema allows EF to translate LINQ or object queries into actual SQL commands by linking
them later through the Mapping (MSL) file to the database (SSDL).
Structure of a CSDL File
A CSDL file is an XML file and typically contains:
1) Entity Types: These represent real-world objects such as 𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟, 𝑂𝑟𝑑𝑒𝑟, etc.
2) Properties: Attributes or fields of an entity, like 𝐶𝑢𝑠𝑡𝑜𝑚𝑒𝑟𝐼𝑑, 𝑁𝑎𝑚𝑒, or 𝐸𝑚𝑎𝑖𝑙.
3) Entity Sets: Collections of entity types (similar to tables).
4) Associations: Represent relationships between entities.
By Daud Anjum 65
ENTERPRISE SYSTEMS SI-342
5) Navigation Properties: Allow navigation from one entity to related entities (like foreign keys
in database).
Example Snippet of a CSDL
< 𝑆𝑐ℎ𝑒𝑚𝑎 𝑁𝑎𝑚𝑒𝑠𝑝𝑎𝑐𝑒 = "𝐶𝑜𝑚𝑝𝑎𝑛𝑦𝑀𝑜𝑑𝑒𝑙" 𝑥𝑚𝑙𝑛𝑠 = "ℎ𝑡𝑡𝑝://
𝑠𝑐ℎ𝑒𝑚𝑎𝑠. 𝑚𝑖𝑐𝑟𝑜𝑠𝑜𝑓𝑡. 𝑐𝑜𝑚/𝑎𝑑𝑜/2009/11/𝑒𝑑𝑚" >
< 𝐸𝑛𝑡𝑖𝑡𝑦𝑇𝑦𝑝𝑒 𝑁𝑎𝑚𝑒 = "𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒" >
< 𝐾𝑒𝑦 >
< 𝑃𝑟𝑜𝑝𝑒𝑟𝑡𝑦𝑅𝑒𝑓 𝑁𝑎𝑚𝑒 = "𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐼𝐷" />
</𝐾𝑒𝑦 >
< 𝑃𝑟𝑜𝑝𝑒𝑟𝑡𝑦 𝑁𝑎𝑚𝑒 = "𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝐼𝐷" 𝑇𝑦𝑝𝑒 = "𝐼𝑛𝑡32" 𝑁𝑢𝑙𝑙𝑎𝑏𝑙𝑒 = "𝑓𝑎𝑙𝑠𝑒" />
< 𝑃𝑟𝑜𝑝𝑒𝑟𝑡𝑦 𝑁𝑎𝑚𝑒 = "𝑁𝑎𝑚𝑒" 𝑇𝑦𝑝𝑒 = "𝑆𝑡𝑟𝑖𝑛𝑔" 𝑁𝑢𝑙𝑙𝑎𝑏𝑙𝑒 = "𝑓𝑎𝑙𝑠𝑒" />
< 𝑃𝑟𝑜𝑝𝑒𝑟𝑡𝑦 𝑁𝑎𝑚𝑒 = "𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡" 𝑇𝑦𝑝𝑒 = "𝑆𝑡𝑟𝑖𝑛𝑔" />
</𝐸𝑛𝑡𝑖𝑡𝑦𝑇𝑦𝑝𝑒 >
< 𝐸𝑛𝑡𝑖𝑡𝑦𝐶𝑜𝑛𝑡𝑎𝑖𝑛𝑒𝑟 𝑁𝑎𝑚𝑒 = "𝐶𝑜𝑚𝑝𝑎𝑛𝑦𝐸𝑛𝑡𝑖𝑡𝑖𝑒𝑠" >
< 𝐸𝑛𝑡𝑖𝑡𝑦𝑆𝑒𝑡 𝑁𝑎𝑚𝑒 = "𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠" 𝐸𝑛𝑡𝑖𝑡𝑦𝑇𝑦𝑝𝑒 = "𝐶𝑜𝑚𝑝𝑎𝑛𝑦𝑀𝑜𝑑𝑒𝑙. 𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒" />
</𝐸𝑛𝑡𝑖𝑡𝑦𝐶𝑜𝑛𝑡𝑎𝑖𝑛𝑒𝑟 >
</𝑆𝑐ℎ𝑒𝑚𝑎 >
Benefits of Using CSDL
1) Abstraction Layer
CSDL abstracts the physical structure of the database. Developers can work with objects
instead of dealing with SQL queries and raw tables.
2) Type Safety and IntelliSense
Since it maps to .NET classes, developers benefit from compile-time type checking and IDE
features like IntelliSense when writing queries.
3) Separation of Concerns
CSDL separates the application’s data model from the storage model (SSDL), promoting a
cleaner architecture and easier maintenance.
4) Support for Complex Types and Relationships
CSDL supports advanced features like inheritance, complex types, and associations between
entities.
5) Foundation for Code Generation
Visual Studio and other tools can use the CSDL to auto-generate .NET classes, saving
development time and reducing errors.
CSDL in Different Modelling Approaches
• In Database-First and Model-First approaches, the CSDL is often auto-generated by Visual
Studio’s EDM Designer.
• In Code-First approach, CSDL is not explicitly written. Instead, it is generated at runtime based
on the code model (POCO classes and annotations).
By Daud Anjum 66
ENTERPRISE SYSTEMS SI-342
In either case, the concepts of the CSDL still apply: it remains the conceptual schema that the
application understands and uses.
Eager vs Lazy Loading
In the [Link] Entity Framework, loading related data (i.e., navigation properties or child
entities) from the database is a common operation. The EF provides three strategies to load related
data: Eager Loading, Lazy Loading, and Explicit Loading. Among these, Eager and Lazy Loading
are the most commonly used. Each loading strategy determines when and how the related data is
fetched from the database.
Choosing the right loading strategy is essential for performance optimization and avoiding
common pitfalls like the N+1 query problem or unnecessary data retrieval.
Eager Loading
Eager Loading is a strategy where related data is loaded at the same time as the main entity,
typically through JOIN queries. In other words, when a query is executed, EF retrieves not only
the main entity but also all the specified related entities in a single query. It is usually implemented
using the . 𝐼𝑛𝑐𝑙𝑢𝑑𝑒( ) method in LINQ queries. This instructs EF to include the related entities
during the initial query.
Example:
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠
. 𝐼𝑛𝑐𝑙𝑢𝑑𝑒(𝑠 => 𝑠. 𝐶𝑜𝑢𝑟𝑠𝑒𝑠)
. 𝑇𝑜𝐿𝑖𝑠𝑡();
In this example, for each 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 entity, their associated 𝐶𝑜𝑢𝑟𝑠𝑒𝑠 will be loaded immediately
from the database.
Advantages:
• Reduces round trips to the database.
• Improves performance when related data is definitely needed.
• Prevents the N+1 select problem, where multiple queries are executed to retrieve related data.
Disadvantages:
• Can lead to large and heavy queries if multiple levels of related data are included.
• May result in over-fetching, i.e., retrieving data that is not actually used.
Lazy Loading
Lazy Loading is a strategy where related data is loaded on demand, i.e., only when it is accessed
for the first time. In this case, EF issues a separate query each time a navigation property is
accessed. Lazy loading requires navigation properties to be declared as virtual, the context must
support proxy creation (enabled by default in EF), and the related property should not be explicitly
leaded or eagerly loaded earlier.
By Daud Anjum 67
ENTERPRISE SYSTEMS SI-342
Example:
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝐹𝑖𝑟𝑠𝑡𝑂𝑟𝐷𝑒𝑓𝑎𝑢𝑙𝑡();
𝑣𝑎𝑟 𝑐𝑜𝑢𝑟𝑠𝑒𝑠 = 𝑠𝑡𝑢𝑑𝑒𝑛𝑡. 𝐶𝑜𝑢𝑟𝑠𝑒𝑠;
Here, the 𝐶𝑜𝑢𝑟𝑠𝑒𝑠 collection is not loaded until it is accessed, causing EF to execute an additional
query.
Advantages:
• Reduces the initial query size, fetching only what is immediately required.
• Suitable for large data models where not all related data is needed at once.
• Allows for more modular and flexible code.
Disadvantages:
• Can cause multiple trips to the database (N+1 problem), severely degrading performance.
• Harder to debug and trace performance issues.
• Risk of accidentally loading too much data if used without care in loops or recursive structures.
Comparison Table
Feature Eager Loading Lazy Loading
When is data loaded? Immediately with main entity On-demand (when accessed)
Query type Single query with JOIN Separate query per navigation
Performance Better for required related Better for optimal data
data
Risk Over-fetching data N+1 query problem
Implementation . 𝐼𝑛𝑐𝑙𝑢𝑑𝑒( ) method Virtual navigation properties
Best Practices
• Use Eager Loading when you are sure that related data will be used immediately, such as
displaying a list with associated details.
• Use Lazy Loading when you might not need the related data and want to avoid fetching it
unnecessarily.
• Combine with Explicit Loading (a manual strategy) when you want precise control over when
related data is loaded.
• For high-performance applications, profiling and query optimization are essential to determine
the right strategy.
POCO classes
POCO stands for Plain Old CLR Object. In the context of the .NET Framework and Entity
Framework (EF), a POCO class is a simple .NET class that does not depend on any specific
framework-related base classes or attributes. It represents a clean, unencumbered domain object,
designed using only standard .NET features, making it easier to work with, test, and maintain.
By Daud Anjum 68
ENTERPRISE SYSTEMS SI-342
These classes contain just properties and logic that model real-world entities, such as 𝑆𝑡𝑢𝑑𝑒𝑛𝑡,
𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒, or 𝑂𝑟𝑑𝑒𝑟. They do not inherit from an Entity Framework base class, nor do they need
special data annotations (though annotations or Fluent API can still be used optionally).
Example of a POCO Class
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑆𝑡𝑢𝑑𝑒𝑛𝑡
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐼𝑑 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑁𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
// 𝑁𝑎𝑣𝑖𝑔𝑎𝑡𝑖𝑜𝑛 𝑝𝑟𝑜𝑝𝑒𝑟𝑡𝑦
𝑝𝑢𝑏𝑙𝑖𝑐 𝑣𝑖𝑟𝑡𝑢𝑎𝑙 𝐼𝐶𝑜𝑙𝑙𝑒𝑐𝑡𝑖𝑜𝑛<𝐶𝑜𝑢𝑟𝑠𝑒> 𝐶𝑜𝑢𝑟𝑠𝑒𝑠 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
}
In this example:
• The 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 class is a POCO.
• It has standard properties (𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐼𝑑, 𝑁𝑎𝑚𝑒).
• 𝐶𝑜𝑢𝑟𝑠𝑒𝑠 is a navigation property to related entities.
• No EF-specific attributes or base classes are used.
Why Use POCO Classes in EF?
POCO classes are part of a Code First approach, where the domain model (i.e., business objects)
is designed first, and the database is generated from it, as opposed to generating code from an
existing database.
Advantages:
1) Simplicity: POCOs are just C# classes. No EF-specific clutter.
2) Maintainability: Easier to update and manage.
3) Testability: Because POCOs are free of farmwork dependencies, they are perfect for unit
testing and mock testing.
4) Decoupling: Keeps your business logic and data access concerns separate.
5) Flexibility: Can be used across different ORM tools or layers in the application.
POCO in Entity Framework Workflows
1) Code First Approach
In Code First, POCO classes are the starting point. EF uses these classes to generate the
database schema through migrations.
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑆𝑐ℎ𝑜𝑜𝑙𝐶𝑜𝑛𝑡𝑒𝑥𝑡 ∶ 𝐷𝑏𝐶𝑜𝑛𝑡𝑒𝑥𝑡
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝐷𝑏𝑆𝑒𝑡<𝑆𝑡𝑢𝑑𝑒𝑛𝑡> 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
}
When 𝑆𝑐ℎ𝑜𝑜𝑙𝐶𝑜𝑛𝑡𝑒𝑥𝑡 is run, EF will generate a 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠 table based on the 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 POCO
class.
By Daud Anjum 69
ENTERPRISE SYSTEMS SI-342
2) Model First or Database First
Even in these approaches, EF can generate POCO classes from an existing database or model
using the Entity Designer or reverse engineering tools.
Key Characteristics of POCO Classes
• They must have a public parameterless constructor.
• Typically have auto-implemented properties.
• Navigation properties should be virtual if lazy loading is desired.
• No requirement for inheritance from EF classes like 𝐸𝑛𝑡𝑖𝑡𝑦𝑂𝑏𝑗𝑒𝑐𝑡.
POCO vs non-POCO Classes
Feature POCO Class Non-POCO Class (EF-
specific)
Inheritance None (just CLR object) Often inherits from
𝐸𝑛𝑡𝑖𝑡𝑦𝑂𝑏𝑗𝑒𝑐𝑡
Annotations Optional Frequently includes EF
attributes
Testability Very high Harder to isolate and test
Decoupling Decoupled from EF Tightly coupled
EF Feature Support Full support (with Full, but no cleanly separated
configuration)
When to Use POCO Classes
• When building domain-driven designs
• When you want clean separation of concerns
• For unit testing and mocking business logic
• In Code First scenarios, where you have control over the class design and database schema
DBContext API
The DbContext API is the central class in EF used to interact with the database using .NET objects.
It serves as a bridge between the domain or entity classes (POCOs) and the database. DbContext
is responsible for querying, saving, tracking changes, and managing the lifecycle of entities.
Introduces in Entity Framework 4.1, 𝐷𝑏𝐶𝑜𝑛𝑡𝑒𝑥𝑡 greatly simplified the way dvelopers work with
EF, replacing the more complex 𝑂𝑏𝑗𝑒𝑐𝑡𝐶𝑜𝑛𝑡𝑒𝑥𝑡 used in earlier versions.
What is DbContext?
𝐷𝑏𝐶𝑜𝑛𝑡𝑒𝑥𝑡 is a class in the 𝑆𝑦𝑠𝑡𝑒𝑚. 𝐷𝑎𝑡𝑎. 𝐸𝑛𝑡𝑖𝑡𝑦 manespace. It acts as a wrapper around the
lower-level 𝑂𝑏𝑗𝑒𝑐𝑡𝐶𝑜𝑛𝑡𝑒𝑥𝑡 class and provides a cleaner and more developer-friendly API.
You typically create a class that inherits from 𝐷𝑏𝐶𝑜𝑛𝑡𝑒𝑥𝑡 and includes 𝐷𝑏𝑆𝑒𝑡<𝑇𝐸𝑛𝑡𝑖𝑡𝑦>
properties to represent the tables in your database.
Example:
By Daud Anjum 70
ENTERPRISE SYSTEMS SI-342
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑆𝑐ℎ𝑜𝑜𝑙𝐶𝑜𝑛𝑡𝑒𝑥𝑡 ∶ 𝐷𝑏𝐶𝑜𝑛𝑡𝑒𝑥𝑡
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝐷𝑏𝑆𝑒𝑡<𝑆𝑡𝑢𝑑𝑒𝑛𝑡> 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
𝑝𝑢𝑏𝑙𝑖𝑐 𝐷𝑏𝑆𝑒𝑡<𝐶𝑜𝑢𝑟𝑠𝑒> 𝐶𝑜𝑢𝑟𝑠𝑒𝑠 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
}
In this example, 𝑆𝑐ℎ𝑜𝑜𝑙𝐶𝑜𝑛𝑡𝑒𝑥𝑡 is a custom context class inherting from 𝐷𝑏𝐶𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠
and 𝐶𝑜𝑢𝑟𝑠𝑒𝑠 represent tables in the database.
Core Responsibilities of DbContext
1) Querying the Database
Using LINQ, developers can query entities through DbSet. For example:
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝑊ℎ𝑒𝑟𝑒(𝑠 =>
𝑠. 𝑁𝑎𝑚𝑒. 𝑆𝑡𝑎𝑟𝑡𝑠𝑊𝑖𝑡ℎ("𝐴")). 𝑇𝑜𝐿𝑖𝑠𝑡( );
2) Saving Data
The 𝑆𝑎𝑣𝑒𝐶ℎ𝑎𝑛𝑔𝑒𝑠( ) methods persists all changes made in the context to the database.
𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑎𝑣𝑒𝐶ℎ𝑎𝑛𝑔𝑒𝑠 ( );
3) Tracking Changes
DbContext automatically tracks changes to entities so it knows what to insert, update, or delete.
4) Entity Lifecycle Management
It controls how entities are loaded (e.g., lazy or eager loading). It also manages entity states
(Added, Modified, Deleted, Unchanged, Detached).
5) Connection Management
It can be configured to use a specific database connection string.
6) Validation
It automatically performs validation on entities based on Data Annotations before saving.
Important Members of DbContext
1) DbSet<TEntity>
It represents a collection for a given entity in the model. Think of it as a table in the database.
2) SaveChanges( )
It commits changes made in the context to the database. This includes inserts, updates, and
deletes.
3) Entry(object entity)
It provides access to change tracking information and allows you to set the state of the entity.
𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝐸𝑛𝑡𝑟𝑦(𝑠𝑡𝑢𝑑𝑒𝑛𝑡 ). 𝑆𝑡𝑎𝑡𝑒 = 𝐸𝑛𝑡𝑖𝑡𝑦𝑆𝑡𝑎𝑡𝑒. 𝑀𝑜𝑑𝑖𝑓𝑖𝑒𝑑;
4) ChangeTracker
It gives information about tracked entities and their states.
𝑣𝑎𝑟 𝑒𝑛𝑡𝑟𝑖𝑒𝑠 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝐶ℎ𝑎𝑛𝑔𝑒𝑇𝑟𝑎𝑐𝑘𝑒𝑟. 𝐸𝑛𝑡𝑟𝑖𝑒𝑠 ( );
5) Database
It gives access to database-level operations like executing raw SQL, migrations, and
transactions.
By Daud Anjum 71
ENTERPRISE SYSTEMS SI-342
𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝐷𝑎𝑡𝑎𝑏𝑎𝑠𝑒. 𝐸𝑥𝑒𝑐𝑢𝑡𝑒𝑆𝑞𝑙𝐶𝑜𝑚𝑚𝑎𝑛𝑑("𝑈𝑃𝐷𝐴𝑇𝐸 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠 𝑆𝐸𝑇 𝑁𝑎𝑚𝑒 =
′𝐽𝑜ℎ𝑛′ 𝑊𝐻𝐸𝑅𝐸 𝐼𝑑 = 1");
Entity States in DbContext
Each entity in the context has a state:
• Added – Entity is new and will be inserted
• Modified – Entity has been changed and will be updated
• Deleted – Entity is marked for deletion
• Unchanged – No changes have been made since it was loaded
• Detached – Entity is not tracked by the context
Managing these states manually can give you precise control over the database operations.
Configuration and Initialization
You can configure the database connection using a connection string in 𝑎𝑝𝑝. 𝑐𝑜𝑛𝑓𝑖𝑔 or
𝑤𝑒𝑏. 𝑐𝑜𝑛𝑓𝑖𝑔, or by passing it directly to the DbContext constructor.
𝑝𝑢𝑏𝑙𝑖𝑐 𝑆𝑐ℎ𝑜𝑜𝑙𝐶𝑜𝑛𝑡𝑒𝑥𝑡 ( ) ∶ 𝑏𝑎𝑠𝑒("𝑛𝑎𝑚𝑒=𝑆𝑐ℎ𝑜𝑜𝑙𝐷𝐵𝐶𝑜𝑛𝑛𝑒𝑐𝑡𝑖𝑜𝑛") { }
You can also override methods like 𝑂𝑛𝑀𝑜𝑑𝑒𝑙𝐶𝑟𝑒𝑎𝑡𝑖𝑛𝑔( ) to customize mappings using Fluent
API:
𝑝𝑟𝑜𝑡𝑒𝑐𝑡𝑒𝑑 𝑜𝑣𝑒𝑟𝑟𝑖𝑠𝑒 𝑣𝑜𝑖𝑑 𝑂𝑛𝑀𝑜𝑑𝑒𝑙𝐶𝑟𝑒𝑎𝑡𝑖𝑛𝑔(𝐷𝑏𝑀𝑜𝑑𝑒𝑙𝐵𝑢𝑖𝑙𝑑𝑒𝑟 𝑚𝑜𝑑𝑒𝑙𝐵𝑢𝑖𝑙𝑑𝑒𝑟)
{
𝑚𝑜𝑑𝑒𝑙𝐵𝑢𝑖𝑙𝑑𝑒𝑟. 𝐸𝑛𝑡𝑖𝑡𝑦<𝑆𝑡𝑢𝑑𝑒𝑛𝑡>( ). 𝐻𝑎𝑠𝐾𝑒𝑦(𝑠 => 𝑠. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐼𝑑 );
}
DbContext Lifecycle and Best Practices
• Short-lived: Create and dispose of 𝐷𝑏𝐶𝑜𝑛𝑡𝑒𝑥𝑡 object quickly, ideally per web request or
service call.
• Thread Safety: 𝐷𝑏𝐶𝑜𝑛𝑡𝑒𝑥𝑡 is not thread-safe, never share it across multiple threads.
• Dispose Properly: Always dispose of the context using 𝑢𝑠𝑖𝑛𝑔 blocks or dependency injection
to avoid memory leaks.
𝑢𝑠𝑖𝑛𝑔 (𝑣𝑎𝑟 𝑐𝑜𝑛𝑡𝑒𝑥𝑡 = 𝑛𝑒𝑤 𝑆𝑐ℎ𝑜𝑜𝑙𝐶𝑜𝑛𝑡𝑒𝑥𝑡 ( ))
{
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 = 𝑛𝑒𝑤 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 { 𝑁𝑎𝑚𝑒 = "𝐴𝑙𝑖" };
𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝐴𝑑𝑑 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 );
𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑎𝑣𝑒𝐶ℎ𝑎𝑛𝑔𝑒𝑠 ( );
}
Querying Entity Data Models
In the [Link] Entity Framework, once as Entity Data Model (EDM) is created, it can be
queried to retrieve, update, and manipulate data from the underlying database. Querying the EDM
By Daud Anjum 72
ENTERPRISE SYSTEMS SI-342
allows developers to interact with data in an object-oriented way, without writing raw SQL queries.
EF supports multiple querying methods, such as LINQ to Entities, Entity SQL, and method-based
(lambda) syntax.
The querying capabilities of the EF provide a powerful and flexible mechanism to work with the
data model while maintaining type safety and strong integration with the .NET language features.
Querying with LINQ to Entities
LINQ is the most commonly used querying methods in EF. It allows developers to write queries
directly in C# or [Link] syntax using the familiar LINQ keywords.
𝑢𝑠𝑖𝑛𝑔 (𝑣𝑎𝑟 𝑐𝑜𝑛𝑡𝑒𝑥𝑡 = 𝑛𝑒𝑤 𝑆𝑐ℎ𝑜𝑜𝑙𝐶𝑜𝑛𝑡𝑒𝑥𝑡 ( ))
{
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 = 𝑓𝑟𝑜𝑚 𝑠 𝑖𝑛 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠
𝑤ℎ𝑒𝑟𝑒 𝑠. 𝐴𝑔𝑒 > 18
𝑠𝑒𝑙𝑒𝑐𝑡 𝑠;
}
This query retrieves all students above the age of 18 from the 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠 table. The query is
translated by EF into SQL and executed on the database.
Advantage:
• Strongly typed
• IntelliSense support in Visual Studio
• Compile-time syntax checking
Method-Based Syntax (Lambda Expressions)
Instead of using LINQ query syntax, developers can use method chaining with lambda
expressions, which is especially useful for more complex queries.
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠
. 𝑊ℎ𝑒𝑟𝑒(𝑠 => 𝑠. 𝐴𝑔𝑒 > 18)
. 𝑂𝑟𝑑𝑒𝑟𝐵𝑦(𝑠 => 𝑠. 𝑁𝑎𝑚𝑒)
. 𝑇𝑜𝐿𝑖𝑠𝑡 ( );
This method-based syntax performs the same logic but is often more concise and preferred by
many developers for advanced filtering and transformations.
Entity SQL
Entity SQL is a query language that resembles traditional SQL but is designed to query EF objects.
It provides more flexibility in some advanced scenarios, but it's less commonly used in modern
EF projects.
𝑠𝑡𝑟𝑖𝑛𝑔 𝑞𝑢𝑒𝑟𝑦 = "𝑆𝐸𝐿𝐸𝐶𝑇 𝑉𝐴𝐿𝑈𝐸 𝑠 𝐹𝑅𝑂𝑀 𝑆𝑐ℎ𝑜𝑜𝑙𝐶𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠 𝐴𝑆 𝑠
𝑊𝐻𝐸𝑅𝐸 𝑠. 𝐴𝑔𝑒 > 18";
𝑂𝑏𝑗𝑒𝑐𝑡𝑄𝑢𝑒𝑟𝑦<𝑆𝑡𝑢𝑑𝑒𝑛𝑡> 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝐶𝑟𝑒𝑎𝑡𝑒𝑄𝑢𝑒𝑟𝑦<𝑆𝑡𝑢𝑑𝑒𝑛𝑡>(𝑞𝑢𝑒𝑟𝑦);
Entity SQL is particularly useful when queries need to be constructed dynamically or if you are
dealing with metadata directly.
By Daud Anjum 73
ENTERPRISE SYSTEMS SI-342
Using Navigation Properties in Queries
One of the key strengths of EF is its support for navigation properties, which allows you to query
related data using relationships defined in the EDM.
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠
. 𝐼𝑛𝑐𝑙𝑢𝑑𝑒(𝑠 => 𝑠. 𝐶𝑜𝑢𝑟𝑠𝑒𝑠)
. 𝑇𝑜𝐿𝑖𝑠𝑡( );
This retrieves students along with the courses they are enrolled in, using a single query (eager
loading).
Filtering, Sorting, and Paging
EF allows advanced operations like filtering, sorting, and paging with minimal code.
• Filtering
𝑣𝑎𝑟 𝑟𝑒𝑠𝑢𝑙𝑡 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝑊ℎ𝑒𝑟𝑒(𝑠 => 𝑠. 𝑁𝑎𝑚𝑒. 𝐶𝑜𝑛𝑡𝑎𝑖𝑛𝑠("𝐴𝑙𝑖"));
• Sorting
𝑣𝑎𝑟 𝑟𝑒𝑠𝑢𝑙𝑡 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝑂𝑟𝑑𝑒𝑟𝐵𝑦𝐷𝑒𝑠𝑐𝑒𝑛𝑑𝑖𝑛𝑔(𝑠 => 𝑠. 𝑅𝑒𝑔𝑖𝑠𝑡𝑟𝑎𝑡𝑖𝑜𝑛𝐷𝑎𝑡𝑒);
• Paging
𝑣𝑎𝑟 𝑟𝑒𝑠𝑢𝑙𝑡 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝑆𝑘𝑖𝑝(10). 𝑇𝑎𝑘𝑒(10);
These techniques are essential in real-world applications to handle large datasets efficiently.
Anonymous Types and Projections
You can project data into new anonymous types or custom DTOs (Data Transfer Objects) to reduce
data transfer and decouple logic from entity models.
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑁𝑎𝑚𝑒𝑠 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠
. 𝑆𝑒𝑙𝑒𝑐𝑡(𝑠 => 𝑛𝑒𝑤 { 𝑠. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐼𝑑, 𝑠. 𝑁𝑎𝑚𝑒 })
. 𝑇𝑜𝐿𝑖𝑠𝑡( );
This retrieves only the necessary fields, improving performance and clarity.
Asynchronous Queries
EF supports asynchronous querying with async/await for better scalability in web applications.
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 = 𝑎𝑤𝑎𝑖𝑡 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝑇𝑜𝐿𝑖𝑠𝑡𝐴𝑠𝑦𝑛𝑐 ( );
This ensures the applications remain responsive while waiting for the database operation to
complete.
Query Execution and Deferred Loading
Queries in EF use deferred execution, which means the query is not executed against the database
until it is enumerated (e.g., via. . 𝑇𝑜𝐿𝑖𝑠𝑡() or a 𝑓𝑜𝑟𝑒𝑎𝑐ℎ loop). This allows for building complex
queries step by step before execution.
𝑣𝑎𝑟 𝑞𝑢𝑒𝑟𝑦 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝑊ℎ𝑒𝑟𝑒(𝑠 => 𝑠. 𝐴𝑔𝑒 > 18);
// 𝑁𝑜𝑡 𝑒𝑥𝑒𝑐𝑢𝑡𝑒𝑑 𝑦𝑒𝑡
𝑣𝑎𝑟 𝑟𝑒𝑠𝑢𝑙𝑡 = 𝑞𝑢𝑒𝑟𝑦. 𝑇𝑜𝐿𝑖𝑠𝑡( ); // 𝑄𝑢𝑒𝑟𝑦 𝑖𝑠 𝑒𝑥𝑒𝑐𝑢𝑡𝑒𝑑 ℎ𝑒𝑟𝑒
By Daud Anjum 74
ENTERPRISE SYSTEMS SI-342
Raw SQL Queries
Although LINQ is preferred, EF also supports executing raw SQL when needed.
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 = 𝑐𝑜𝑛𝑡𝑒𝑥𝑡. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠
. 𝐹𝑟𝑜𝑚𝑆𝑞𝑙𝑅𝑎𝑤("𝑆𝐸𝐿𝐸𝐶𝑇 ∗ 𝐹𝑅𝑂𝑀 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠 𝑊𝐻𝐸𝑅𝐸 𝐴𝑔𝑒 > 18")
. 𝑇𝑜𝐿𝑖𝑠𝑡( );
Raw SQL queries can be useful for complex operations or stored procedure calls, but they should
be used cautiously to avoid SQL injection and reduce maintenance complexity.
10. Introduction to [Link] MVC
[Link] MVC is a powerful, lightweight, and highly testable web application framework
developed by Microsoft. It is part of the [Link] platform and is designed to separate an
application into three main components: Model, View, and Controller. This architectural pattern
helps manage complexity in large-scale applications by organizing code logically and promoting
a clean separation of concerns.
The Model represents the application’s data and business logic. It defines how data is stored,
retrieved, and validated. The View is responsible for rendering the UI and presenting data to the
user. It is typically made using HTML, CSS, and Razor syntax. The Controller handles incoming
HTTP requests, processes user input, interacts with the Model, and returns the appropriate View
to the browser. This separation allows developers to work on different aspects of an application
independently, improving maintainability and scalability.
[Link] MVC was introduced as an alternative to Web Forms, offering more control over HTML
and JS, supporting clean URLs and RESTful APIs. It aligns closely with modern web development
practices, including test-driven development (TDD), responsive design, and integration with front-
end frameworks like Angular, React, and jQuery.
One of the key advantages of [Link] MVC is its flexibility. Unlike Web Forms, it does not rely
on server-side controls and view state. Instead, it uses a routing engine to map incoming requests
to controller actions, allowing developers to define custom URL patterns and logic with precision.
It also supports dependency injection, custom filters, and extensible components, making it ideal
for building modular, testable enterprise applications.
[Link] MVC is tightly integrated with the .NET Framework and later with .NET Core (now
simply .NET), providing seamless access to libraries, authentication, security, and database
technologies such as [Link] and EF. Developers can build robust applications that follow
SOLID principles, support until testing, and are easier to maintain over time.
Although [Link] MVC has evolved and merged into [Link] Core MVC in recent versions
of .NET, its core principles remain foundational for understanding modern Microsoft web
development technologies.
By Daud Anjum 75
ENTERPRISE SYSTEMS SI-342
MVC Application Structure
[Link] MVC applications follow a structured and organized project layout that aligns with the
core principles of the MVC architectural pattern. This structure separates concerns within an
application by dividing it into three major components. Each component plays a specific role in
managing role, user interface, and application logic, which enhances maintainability, scalability,
and testability.
Models
The Model represents the business layer or data logic of the application. It contains classes that
define the data structure and rules related to business operations. In an [Link] MVC project,
models are typically stored in the Models folder.
For example, if you’re building a student management system, a 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 model may include
properties like 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐼𝑑, 𝑁𝑎𝑚𝑒, 𝐷𝑂𝐵, and 𝐸𝑚𝑎𝑖𝑙. Models may also contain data annotations
for validation and methods to perform business operations.
Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑆𝑡𝑢𝑑𝑒𝑛𝑡
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐼𝑑 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
[𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝑑]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑁𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
𝑝𝑢𝑏𝑙𝑖𝑐 𝐷𝑎𝑡𝑒𝑇𝑖𝑚𝑒 𝐷𝑂𝐵 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
}
Models can be standalone or connected to a database using ORM tools like EF. They are the core
components through which the application interacts with data.
Views
The View is the UI component of the MVC structure. It is responsible for presenting data to the
user and collecting input. Views are created using Razor syntax (. 𝑐𝑠ℎ𝑡𝑚𝑙) files and are stored in
the Views folder. Each controller has a corresponding subfolder in the Views directory to keep
views organized.
For example, if you have a 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟, its views will be located in 𝑉𝑖𝑒𝑤𝑠/𝑆𝑡𝑢𝑑𝑒𝑛𝑡/
such as:
• 𝐼𝑛𝑑𝑒𝑥. 𝑐𝑠ℎ𝑡𝑚𝑙
• 𝐷𝑒𝑡𝑎𝑖𝑙𝑠. 𝑐𝑠ℎ𝑡𝑚𝑙
• 𝐶𝑟𝑒𝑎𝑡𝑒. 𝑐𝑠ℎ𝑡𝑚𝑙
Views can be strongly typed, meaning they are bound to a model class, allowing IntelliSense and
compile-time checking.
By Daud Anjum 76
ENTERPRISE SYSTEMS SI-342
Example (𝐼𝑛𝑑𝑒𝑥. 𝑐𝑠ℎ𝑡𝑚𝑙):
@𝑚𝑜𝑑𝑒𝑙 𝐼𝐸𝑛𝑢𝑚𝑒𝑟𝑎𝑏𝑙𝑒<𝑀𝑦𝐴𝑝𝑝. 𝑀𝑜𝑑𝑒𝑙𝑠. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡>
@𝑓𝑜𝑟𝑒𝑎𝑐ℎ (𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 𝑖𝑛 𝑀𝑜𝑑𝑒𝑙)
{
<𝑝>@𝑠𝑡𝑢𝑑𝑒𝑛𝑡. 𝑁𝑎𝑚𝑒</𝑝>
}
Views can also include partial views and layout pages for reuseable HTML content across the
applications.
Controllers
The Controller acta as a mediator between Models and Views. It handles incoming HTTP requests,
processes user inputs, interacts with the model to retrieve or update data, and returns appropriate
views. Controllers are stored in the Controllers folder and typically end with the word 𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟.
For example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟 ∶ 𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐼𝑛𝑑𝑒𝑥( )
{
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 = 𝑑𝑏. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝑇𝑜𝐿𝑖𝑠𝑡 ( );
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠);
}
}
Each method inside a controller is called an Action Method, and each action corresponds to a URL
endpoint in the application. The default routing in MVC maps URLs to controller actions using
the pattern:
/{𝑐𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟}/{𝑎𝑐𝑡𝑖𝑜𝑛}/{𝑖𝑑}
Routing
Although not a physical folder, Routing is an essential part of the application structure. It is defined
in 𝑅𝑜𝑢𝑡𝑒𝐶𝑜𝑛𝑓𝑖𝑔. 𝑐𝑠 (in older versions) or 𝑆𝑡𝑎𝑟𝑡𝑢𝑝. 𝑐𝑠 (in [Link] Core MVC). Routing maps
incoming requests to the appropriate controller action.
Example:
𝑟𝑜𝑢𝑡𝑒𝑠. 𝑀𝑎𝑝𝑅𝑜𝑢𝑡𝑒(
𝑛𝑎𝑚𝑒: "𝐷𝑒𝑓𝑎𝑢𝑙𝑡",
𝑢𝑟𝑙: "{𝑐𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟}/{𝑎𝑐𝑡𝑖𝑜𝑛}/{𝑖𝑑}",
𝑑𝑒𝑓𝑎𝑢𝑙𝑡𝑠: 𝑛𝑒𝑤 { 𝑐𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟 = Home, 𝑎𝑐𝑡𝑖𝑜𝑛 = Index, 𝑖𝑑 =
𝑈𝑟𝑙𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟. 𝑂𝑝𝑡𝑖𝑜𝑛𝑎𝑙 }
);
This enables friendly URLs and supports RESTful patterns.
Other Folders and Files
In addition to the core MVC folders, an [Link] MVC project typically includes:
By Daud Anjum 77
ENTERPRISE SYSTEMS SI-342
• App_Start: Contains configuration files such as 𝑅𝑜𝑢𝑡𝑒𝐶𝑜𝑛𝑓𝑖𝑔. 𝑐𝑠, 𝐹𝑖𝑙𝑡𝑒𝑟𝐶𝑜𝑛𝑓𝑖𝑔. 𝑐𝑠,
𝐵𝑢𝑛𝑑𝑙𝑒𝐶𝑜𝑛𝑓𝑖𝑔. 𝑐𝑠, and more.
• Scripts: Contains JS files including jQuery, validation scripts, and custom JS code.
• Content: Holds static files such as CSS, images, fonts, and themes.
• Views/Shared: Contains views shared across multiple controllers, such as layout pages
(_𝐿𝑎𝑦𝑜𝑢𝑡. 𝑐𝑠ℎ𝑡𝑚𝑙) and partial views (_𝐿𝑜𝑔𝑖𝑛𝑃𝑎𝑟𝑡𝑖𝑎𝑙. 𝑐𝑠ℎ𝑡𝑚𝑙).
• [Link]: The configuration file for setting up connection strings, authentication,
compilation, etc.
• [Link]: An optional file used to handle application-level events like 𝐴𝑝𝑝𝑙𝑖𝑐𝑎𝑡𝑖𝑜𝑛_𝑆𝑡𝑎𝑟𝑡.
Controllers Overview
The Controller plays a central role in handling user interaction, managing the flow of the
application, and coordinating responses between the Model and the View. It acts as the
intermediary that interprets incoming HTTP requests, processes them, and returns the appropriate
response. Understanding how controllers work is essential for building structured and responsive
web applications.
What is a Controller?
A controller is a C# class that derives from the 𝑆𝑦𝑠𝑡𝑚𝑒. 𝑊𝑒𝑏. 𝑀𝑣𝑐. 𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟 base class. It
contains action methods that correspond to different operations or endpoints of a web application.
Each action method is responsible for executing logic (like interacting with a database or business
rules) and then returning a View, JSON, Redirect, or other result types to the browser.
For example, in a student management application, a controller named 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟 might
contain actions like 𝐼𝑛𝑑𝑒𝑥, 𝐷𝑒𝑡𝑎𝑖𝑙𝑠, 𝐶𝑟𝑒𝑎𝑡𝑒, 𝐸𝑑𝑖𝑡, and 𝐷𝑒𝑙𝑒𝑡𝑒.
Structure and Naming Convention
[Link] MVC follows a naming convention where the controller class ends with the word
𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟. This helps the routing engine identify the controller from the URL. For instance, the
URL:
/𝑆𝑡𝑢𝑑𝑒𝑛𝑡/𝐷𝑒𝑡𝑎𝑖𝑙𝑠/5
Will be mapped to the 𝐷𝑒𝑡𝑎𝑖𝑙𝑠 action of 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟, and 5 will be passed as a parameter.
By Daud Anjum 78
ENTERPRISE SYSTEMS SI-342
A Basic Controller Example
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟 ∶ 𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐼𝑛𝑑𝑒𝑥( )
{
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠 = 𝑑𝑏. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝑇𝑜𝐿𝑖𝑠𝑡( );
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤(𝑠𝑡𝑢𝑑𝑒𝑛𝑡𝑠);
}
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐷𝑒𝑡𝑎𝑖𝑙𝑠(𝑖𝑛𝑡 𝑖𝑑)
{
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 = 𝑑𝑏. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝐹𝑖𝑛𝑑(𝑖𝑑);
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤(𝑠𝑡𝑢𝑑𝑒𝑛𝑡);
}
}
Each method inside the controller returns an ActionResult, which represents the response to the
client.
Action Methods
An action method is a public method in a controller that can be invoked via an HTTP request.
These methods perform operations and return responses. They can return various types such as:
• 𝑉𝑖𝑒𝑤( ) – returns an HTML page
• 𝑅𝑒𝑑𝑖𝑟𝑒𝑐𝑡𝑇𝑜𝐴𝑐𝑡𝑖𝑜𝑛( ) – redirects to another action
• 𝐽𝑠𝑜𝑛( ) – returns JSON data (used in APIs)
• 𝐶𝑜𝑛𝑡𝑒𝑛𝑡( ) – returns plain text
• 𝐹𝑖𝑙𝑒( ) – returns a file to download
You can also decorate action methods with attributes like [𝐻𝑡𝑡𝑝𝐺𝑒𝑡], [𝐻𝑡𝑡𝑝𝑃𝑜𝑠𝑡], [𝐴𝑢𝑡ℎ𝑜𝑟𝑖𝑧𝑒],
and [𝑉𝑎𝑙𝑖𝑑𝑎𝑡𝑒𝐴𝑛𝑡𝑖𝐹𝑜𝑟𝑔𝑒𝑟𝑦𝑇𝑜𝑘𝑒𝑛] to control how they behave.
Controller Lifecycle
The controller lifecycle in MVC follows a predictable pattern:
1) Routing engine maps the request to the appropriate controller and action
2) MVC creates an instance of the controller
3) MVC calls the action method
4) Action method returns a result (e.g., a ViewResult)
5) MVC sends the result back to the browser
This lifecycle ensures a clean and testable approach to handling HTTP requests.
Model Binding in Controllers
[Link] MVC includes a powerful Model Binding mechanism that automatically maps data from
HTTP requests (like form inputs or query strings) to method parameters and model objects.
By Daud Anjum 79
ENTERPRISE SYSTEMS SI-342
[𝐻𝑡𝑡𝑝𝑃𝑜𝑠𝑡]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐶𝑟𝑒𝑎𝑡𝑒(𝑆𝑡𝑢𝑑𝑒𝑛𝑡 𝑠𝑡𝑢𝑑𝑒𝑛𝑡)
{
𝑖𝑓 (𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐼𝑠𝑉𝑎𝑙𝑖𝑑)
{
𝑑𝑏. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝐴𝑑𝑑(𝑠𝑡𝑢𝑑𝑒𝑛𝑡);
𝑑𝑏. 𝑆𝑎𝑣𝑒𝐶ℎ𝑎𝑛𝑔𝑒𝑠();
𝑟𝑒𝑡𝑢𝑟𝑛 𝑅𝑒𝑑𝑖𝑟𝑒𝑐𝑡𝑇𝑜𝐴𝑐𝑡𝑖𝑜𝑛("𝐼𝑛𝑑𝑒𝑥");
}
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤(𝑠𝑡𝑢𝑑𝑒𝑛𝑡);
}
Here, MVC automatically binds form inputs to the 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 object.
Controller Responsibilities
In summary, the responsibilities of a controller include:
• Receiving input from the user (via URL or form data)
• Calling the appropriate business logic or data access code (often using the Model)
• Determining the appropriate view or response to return
• Returning a response to the browser
Action Methods
In the [Link] MVC Framework, Action Methods are at the core of request handling. Every
controller in MVC contains multiple actions methods that are responsible for responding to
incoming HTTP requests. These methods define the behaviour of an MVC application and
determine what result (such as an HTML view, JSON data, a redirect, or a file) should be returned
to the user.
Definition and Purpose
An Action Method is simply a public method in a controller class that in invoked by the MVC
Framework in response to a URL request. It must return a type that inherits from the 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡
class or one of its derived types, such as 𝑉𝑖𝑒𝑤𝑅𝑒𝑠𝑢𝑙𝑡, 𝐽𝑠𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡, 𝑅𝑒𝑑𝑖𝑟𝑒𝑐𝑡𝑇𝑜𝑅𝑜𝑢𝑡𝑒𝑅𝑒𝑠𝑢𝑙𝑡,
etc.
By Daud Anjum 80
ENTERPRISE SYSTEMS SI-342
Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝐻𝑜𝑚𝑒𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟 ∶ 𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐼𝑛𝑑𝑒𝑥( )
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤( );
}
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐴𝑏𝑜𝑢𝑡( )
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤( );
}
}
Here, 𝐼𝑛𝑑𝑒𝑥 and 𝐴𝑏𝑜𝑢𝑡 are action methods that return views when a request is made to
/𝐻𝑜𝑚𝑒/𝐼𝑛𝑑𝑒𝑥 or /𝐻𝑜𝑚𝑒/𝐴𝑏𝑜𝑢𝑡.
Rules for Action Methods
1) Must be public: Action methods must be marked as 𝑝𝑢𝑏𝑙𝑖𝑐, or the MVC framework will not
consider them as valid endpoints.
2) Must not be static: Action methods cannot be static.
3) Cannot be overloaded solely based on method signatures: Since URLs do not differentiate
methods by parameters, overloading requires the use of attributes (like [𝐴𝑐𝑡𝑖𝑜𝑛𝑁𝑎𝑚𝑒(" … ")]).
4) Must not be decorated with the [𝑁𝑜𝑛𝐴𝑐𝑡𝑖𝑜𝑛] attribute: This attribute is used to prevent a public
method from being used as an action method.
Types of Action Method Results
Action methods return different types of results based on the response required:
• 𝑉𝑖𝑒𝑤𝑅𝑒𝑠𝑢𝑙𝑡 (𝑉𝑖𝑒𝑤 ( )): Returns an HTML page.
• 𝑅𝑒𝑑𝑖𝑟𝑒𝑐𝑡𝑇𝑜𝑅𝑜𝑢𝑡𝑒𝑅𝑒𝑠𝑢𝑙𝑡 (𝑅𝑒𝑑𝑖𝑟𝑒𝑐𝑡𝑇𝑜𝐴𝑐𝑡𝑖𝑜𝑛( ) / 𝑅𝑒𝑑𝑖𝑟𝑒𝑐𝑡( )): Redirects to another action
or URL.
• 𝐽𝑠𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 (𝐽𝑠𝑜𝑛( )): Returns data in JSON format.
• 𝐶𝑜𝑛𝑡𝑒𝑛𝑡𝑅𝑒𝑠𝑢𝑙𝑡 (𝐶𝑜𝑛𝑡𝑒𝑛𝑡 ( )): Returns plain text or other types of content.
• 𝐹𝑖𝑙𝑒𝑅𝑒𝑠𝑢𝑙𝑡 (𝐹𝑖𝑙𝑒 ( )): Sends a file to the browser.
• 𝐸𝑚𝑝𝑡𝑦𝑅𝑒𝑠𝑢𝑙𝑡: Returns nothing (used when no response is needed).
Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐺𝑒𝑡𝑀𝑒𝑠𝑠𝑎𝑔𝑒( )
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝐶𝑜𝑛𝑡𝑒𝑛𝑡("𝐻𝑒𝑙𝑙𝑜, 𝑊𝑜𝑟𝑙𝑑! ");
}
By Daud Anjum 81
ENTERPRISE SYSTEMS SI-342
Handling HTTP Verbs (GET, POST, etc.)
[Link] MVC allows you to control which HTTP verbs (GET, POST, PUT, DELETE) can call
an action method using attributes like:
• [𝐻𝑡𝑡𝑝𝐺𝑒𝑡]: Responds to GET requests
• [𝐻𝑡𝑡𝑝𝑃𝑜𝑠𝑡]: Responds to POST requests
• [𝐻𝑡𝑡𝑝𝑃𝑢𝑡], [𝐻𝑡𝑡𝑝𝐷𝑒𝑙𝑒𝑡𝑒]: For PUT and DELETE requests (usually used in Web APIs)
• [𝐴𝑐𝑐𝑒𝑝𝑡𝑉𝑒𝑟𝑏𝑠 (𝐻𝑡𝑡𝑝𝑉𝑒𝑟𝑏𝑠. 𝐺𝑒𝑡 | 𝐻𝑡𝑡𝑝𝑉𝑒𝑟𝑏𝑠. 𝑃𝑜𝑠𝑡)]: Supports multiple verbs
Example:
[𝐻𝑡𝑡𝑝𝑃𝑜𝑠𝑡]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝑆𝑢𝑏𝑚𝑖𝑡𝐹𝑜𝑟𝑚(𝑀𝑦𝑀𝑜𝑑𝑒𝑙 𝑚𝑜𝑑𝑒𝑙)
{
// 𝐻𝑎𝑛𝑑𝑙𝑒 𝑓𝑜𝑟𝑚 𝑠𝑢𝑏𝑚𝑖𝑠𝑠𝑖𝑜𝑛
𝑟𝑒𝑡𝑢𝑟𝑛 𝑅𝑒𝑑𝑖𝑟𝑒𝑐𝑡𝑇𝑜𝐴𝑐𝑡𝑖𝑜𝑛("𝐼𝑛𝑑𝑒𝑥");
}
Parameter Binding in Action Methods
[Link] MVC uses model binding to automatically map form inputs, query strings, or route data
to method parameters.
Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐷𝑒𝑡𝑎𝑖𝑙𝑠(𝑖𝑛𝑡 𝑖𝑑)
{
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 = 𝑑𝑏. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝐹𝑖𝑛𝑑 (𝑖𝑑 );
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 );
}
You can also bind complex models:
[𝐻𝑡𝑡𝑝𝑃𝑜𝑠𝑡]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐶𝑟𝑒𝑎𝑡𝑒(𝑆𝑡𝑢𝑑𝑒𝑛𝑡 𝑠𝑡𝑢𝑑𝑒𝑛𝑡)
{
𝑖𝑓 (𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐼𝑠𝑉𝑎𝑙𝑖𝑑)
{
𝑑𝑏. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝐴𝑑𝑑(𝑠𝑡𝑢𝑑𝑒𝑛𝑡);
𝑑𝑏. 𝑆𝑎𝑣𝑒𝐶ℎ𝑎𝑛𝑔𝑒𝑠( );
𝑟𝑒𝑡𝑢𝑟𝑛 𝑅𝑒𝑑𝑖𝑟𝑒𝑐𝑡𝑇𝑜𝐴𝑐𝑡𝑖𝑜𝑛("𝐼𝑛𝑑𝑒𝑥");
}
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤(𝑠𝑡𝑢𝑑𝑒𝑛𝑡);
}
Attribute Routing and Action Names
You can specify the route or action name using attributes:
By Daud Anjum 82
ENTERPRISE SYSTEMS SI-342
[𝐴𝑐𝑡𝑖𝑜𝑛𝑁𝑎𝑚𝑒("𝑆ℎ𝑜𝑤")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐷𝑖𝑠𝑝𝑙𝑎𝑦( )
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤( );
}
And with route attributes (in [Link] MVC 5+):
[𝑅𝑜𝑢𝑡𝑒("𝑠𝑡𝑢𝑑𝑒𝑛𝑡/𝑑𝑒𝑡𝑎𝑖𝑙𝑠/{𝑖𝑑}")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐷𝑒𝑡𝑎𝑖𝑙𝑠(𝑖𝑛𝑡 𝑖𝑑)
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤( );
}
Action Method Filters
Filters like [𝐴𝑢𝑡ℎ𝑜𝑟𝑖𝑧𝑒], [𝐻𝑎𝑛𝑑𝑙𝑒𝐸𝑟𝑟𝑜𝑟], [𝑂𝑢𝑡𝑝𝑢𝑡𝐶𝑎𝑐ℎ𝑒], and [𝑉𝑎𝑙𝑖𝑑𝑎𝑡𝑒𝐴𝑛𝑡𝑖𝐹𝑜𝑟𝑔𝑒𝑟𝑦𝑇𝑜𝑘𝑒𝑛]
are often used with action methods to add cross-cutting concerns like security, error handling,
caching, etc.
[𝐴𝑢𝑡ℎ𝑜𝑟𝑖𝑧𝑒]
[𝐻𝑡𝑡𝑝𝑃𝑜𝑠𝑡]
[𝑉𝑎𝑙𝑖𝑑𝑎𝑡𝑒𝐴𝑛𝑡𝑖𝐹𝑜𝑟𝑔𝑒𝑟𝑦𝑇𝑜𝑘𝑒𝑛]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐷𝑒𝑙𝑒𝑡𝑒(𝑖𝑛𝑡 𝑖𝑑)
{
// 𝑆𝑒𝑐𝑢𝑟𝑒𝑙𝑦 𝑑𝑒𝑙𝑒𝑡𝑒 𝑡ℎ𝑒 𝑟𝑒𝑐𝑜𝑟𝑑
}
Parameterized Action Methods
In [Link] MVC, parameterized action methods are controller actions that accept input values
directly from the URL, form submissions, or query strings. These parameters allow the application
to be dynamic and interactive, enabling users to send data to the server for processing, such as
submitting a form, requesting details for a specific record, or filtering a data set.
Basic Concept
A parameterized action method is defined just like a regular method, but it includes parameters in
its signature. [Link] MVC uses Model Binding to automatically map these parameters from the
HTTP request.
Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐷𝑒𝑡𝑎𝑖𝑙𝑠(𝑖𝑛𝑡 𝑖𝑑)
{
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 = 𝑑𝑏. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝐹𝑖𝑛𝑑 (𝑖𝑑 );
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 );
}
By Daud Anjum 83
ENTERPRISE SYSTEMS SI-342
Here, the 𝑖𝑑 parameter will be populated automatically from a URL like /𝑆𝑡𝑢𝑑𝑒𝑛𝑡/𝐷𝑒𝑡𝑎𝑖𝑙𝑠/5.
This is because MVC maps the {𝑖𝑑} in the route to the 𝑖𝑑 parameter in the method.
Sources of Parameters
MVC binds parameters from various sources:
1) Route Data: From the URL structure (e.g., /𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑠/𝐷𝑒𝑡𝑎𝑖𝑙𝑠/10)
2) Query String: From the URL query (e.g., /𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑠/𝐷𝑒𝑡𝑎𝑖𝑙𝑠/𝑖𝑑=10)
3) Form Data: From submitted form inputs (for POST methods)
4) Request Body: Especially in JSON or XML payloads in API-style requests (for POST/PUT)
Using Multiple Parameters
You can have multiple parameters in an action method. MVC will try to match values from the
request to all of them:
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝑆𝑒𝑎𝑟𝑐ℎ(𝑠𝑡𝑟𝑖𝑛𝑔 𝑛𝑎𝑚𝑒, 𝑖𝑛𝑡 𝑎𝑔𝑒)
{
// 𝑈𝑠𝑒 𝑡ℎ𝑒 𝑝𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑠 𝑓𝑜𝑟 𝑓𝑖𝑙𝑡𝑒𝑟𝑖𝑛𝑔
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤( );
}
URL example: /𝐻𝑜𝑚𝑒/𝑆𝑒𝑎𝑟𝑐ℎ? 𝑛𝑎𝑚𝑒=𝐴𝑙𝑖&𝑎𝑔𝑒=25
Optional Parameters and Default Values
Action methods can include optional parameters by assigning default values:
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐿𝑖𝑠𝑡(𝑖𝑛𝑡 𝑝𝑎𝑔𝑒 = 1)
{
// 𝐷𝑒𝑓𝑎𝑢𝑙𝑡 𝑡𝑜 𝑝𝑎𝑔𝑒 1 𝑖𝑓 𝑛𝑜𝑡 𝑝𝑟𝑜𝑣𝑖𝑑𝑒𝑑
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤( );
}
If the user accesses /𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑠/𝐿𝑖𝑠𝑡, it defaults to 𝑝𝑎𝑔𝑒 = 1.
Binding Complex Types
MVC can also bind custom complex types passed as parameters:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑈𝑠𝑒𝑟𝐼𝑛𝑓𝑜
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑁𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝐴𝑔𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
}
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝑅𝑒𝑔𝑖𝑠𝑡𝑒𝑟(𝑈𝑠𝑒𝑟𝐼𝑛𝑓𝑜 𝑢𝑠𝑒𝑟)
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤(𝑢𝑠𝑒𝑟);
}
If a form contains 𝑁𝑎𝑚𝑒 and 𝐴𝑔𝑒 fields, MVC binds them to the 𝑈𝑠𝑒𝑟𝐼𝑛𝑓𝑜 object.
By Daud Anjum 84
ENTERPRISE SYSTEMS SI-342
[FromBody], [FromQuery], and [FromRoute] (in Web APIs)
Although used more in [Link] Web API, these attributes help specify where to bind from. In
standard MVC, similar functionality is handled automatically through routing and model binding,
but attributes can also be used for clarity.
Attribute Routing with Parameters
With attribute routing, you can define parameterized routes directly above the action:
[𝑅𝑜𝑢𝑡𝑒("𝑆𝑡𝑢𝑑𝑒𝑛𝑡/𝐷𝑒𝑡𝑎𝑖𝑙𝑠/{𝑖𝑑}")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐷𝑒𝑡𝑎𝑖𝑙𝑠(𝑖𝑛𝑡 𝑖𝑑)
{
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 = 𝑑𝑏. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝐹𝑖𝑛𝑑(𝑖𝑑);
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤(𝑠𝑡𝑢𝑑𝑒𝑛𝑡);
}
Calling /𝑆𝑡𝑢𝑑𝑒𝑛𝑡/𝐷𝑒𝑡𝑎𝑖𝑙𝑠/5 will pass 5 to the 𝑖𝑑 parameter.
Validation and Error Handling
Parameters received through model binding should always be validated to prevent errors or abuse:
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐷𝑒𝑡𝑎𝑖𝑙𝑠(𝑖𝑛𝑡? 𝑖𝑑)
{
𝑖𝑓 (𝑖𝑑 == 𝑛𝑢𝑙𝑙)
𝑟𝑒𝑡𝑢𝑟𝑛 𝑛𝑒𝑤 𝐻𝑡𝑡𝑝𝑆𝑡𝑎𝑡𝑢𝑠𝐶𝑜𝑑𝑒𝑅𝑒𝑠𝑢𝑙𝑡(𝐻𝑡𝑡𝑝𝑆𝑡𝑎𝑡𝑢𝑠𝐶𝑜𝑑𝑒. 𝐵𝑎𝑑𝑅𝑒𝑞𝑢𝑒𝑠𝑡);
𝑣𝑎𝑟 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 = 𝑑𝑏. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝑠. 𝐹𝑖𝑛𝑑(𝑖𝑑);
𝑖𝑓 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 == 𝑛𝑢𝑙𝑙)
𝑟𝑒𝑡𝑢𝑟𝑛 𝐻𝑡𝑡𝑝𝑁𝑜𝑡𝐹𝑜𝑢𝑛𝑑();
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤(𝑠𝑡𝑢𝑑𝑒𝑛𝑡);
}
Security Considerations
Never trust parameter values blindly. Validate user input, apply authorization filters, and consider
anti-forgery tokens for sensitive POST requests. Input coming from parameters can potentially be
exploited for SQL Injection or XSS if not handled properly.
Introduction to Razor Syntax
The Razor syntax is a powerful and elegant templating engine used in [Link] MVC to generate
dynamic web content. It allows seamless integration of server-side C# code with HTML markup.
Razor is designed to be lightweight, compact, and easy to read, making it the preferred syntax for
witing views in [Link] MVC applications.
By Daud Anjum 85
ENTERPRISE SYSTEMS SI-342
What is Razor?
Razor is a markup syntax that lets you embed C# code directly into HTML. It was introduced with
[Link] MVC 3 as an alternative to the older ASPX view engine. Razor files typically use the
. 𝑐𝑠ℎ𝑡𝑚𝑙 extensiom, indicating that the file contains both C# and HTML code.
Unlike traditional scripting languages that require lots of code to switch between markup and
logic, Razor uses the @ symbol to indicate the beginning of a C# code block. This keeps the code
clean and readable.
Basic Razor Syntax
• Outputting data:
<𝑝>𝐻𝑒𝑙𝑙𝑜, @𝑀𝑜𝑑𝑒𝑙. 𝑈𝑠𝑒𝑟𝑁𝑎𝑚𝑒! </𝑝>
• Code block:
@{
𝑣𝑎𝑟 𝑚𝑒𝑠𝑠𝑎𝑔𝑒 = "𝑊𝑒𝑙𝑐𝑜𝑚𝑒 𝑡𝑜 𝑅𝑎𝑧𝑜𝑟! ";
}
<𝑝> @𝑚𝑒𝑠𝑠𝑎𝑔𝑒</𝑝>
• Conditional statements:
@𝑖𝑓 (𝑀𝑜𝑑𝑒𝑙. 𝐼𝑠𝐴𝑑𝑚𝑖𝑛) {
<𝑝>𝑊𝑒𝑙𝑐𝑜𝑚𝑒, 𝐴𝑑𝑚𝑖𝑛! </𝑝>
} 𝑒𝑙𝑠𝑒 {
<𝑝>𝑊𝑒𝑙𝑐𝑜𝑚𝑒, 𝑈𝑠𝑒𝑟! </𝑝>
}
• Loops:
<𝑢𝑙>
@𝑓𝑜𝑟 (𝑖𝑛𝑡 𝑖 = 0; 𝑖 < 5; 𝑖++) {
<𝑙𝑖>𝐼𝑡𝑒𝑚 @𝑖</𝑙𝑖>
}
</𝑢𝑙>
The @ symbol is the central feature of Razor – it signals that what follows is server-side C# code.
Razor Expressions vs. Code Blocks
Expressions: Begin with @ and are used to output values directly into HTML.
<ℎ1>@𝐷𝑎𝑡𝑒𝑇𝑖𝑚𝑒. 𝑁𝑜𝑤</ℎ1>
Code Blocks: Begin with @{ } and are used to define variables or control flow logic.
@{
𝑣𝑎𝑟 𝑛𝑎𝑚𝑒 = "𝐴𝑙𝑖";
}
<𝑝>𝐻𝑒𝑙𝑙𝑜 @𝑛𝑎𝑚𝑒! </𝑝>
HTML Encoding
One of Razor’s powerful features is automatic HTML encoding. When you output a string using
@𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒, Razor automatically encodes it to prevent cross-site scripting (XSS) attacks.
By Daud Anjum 86
ENTERPRISE SYSTEMS SI-342
@𝑀𝑜𝑑𝑒𝑙. 𝑈𝑠𝑒𝑟𝐶𝑜𝑚𝑚𝑒𝑛𝑡
If the comment contains <𝑠𝑐𝑟𝑖𝑝𝑡>, Razor will encode it as &𝑙𝑡; 𝑠𝑐𝑟𝑖𝑝𝑡&𝑔𝑡;, making it safe.
If you do not want encoding, use 𝐻𝑡𝑚𝑙. 𝑅𝑎𝑤( ):
@𝐻𝑡𝑚𝑙. 𝑅𝑎𝑤(𝑀𝑜𝑑𝑒𝑙. 𝑅𝑎𝑤𝐻𝑡𝑚𝑙𝐶𝑜𝑛𝑡𝑒𝑛𝑡)
Using Models in Razor Views
Razor views typically receive data through strongly typed models.
At the top of the Razor view:
@𝑚𝑜𝑑𝑒𝑙 𝑀𝑦𝐴𝑝𝑝. 𝑀𝑜𝑑𝑒𝑙𝑠. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡
You can then use the model like this:
<ℎ2>𝑆𝑡𝑢𝑑𝑒𝑛𝑡: @𝑀𝑜𝑑𝑒𝑙. 𝑁𝑎𝑚𝑒</ℎ2>
This makes Razor views type-safe and allows for IntelliSense support in IDEs like Visual Studio.
Razor Comments
Razor comments do not render in the output HTML and are used to annotate your view logic:
@* 𝑇ℎ𝑖𝑠 𝑖𝑠 𝑎 𝑅𝑎𝑧𝑜𝑟 𝐶𝑜𝑚𝑚𝑒𝑛𝑡 *@
This is different from HTML comments (< ! − − − −>), which are visible in the browser’s
“View Source”.
Layout Pages and Razor
Razor supports layout views, allowing you to create a consistent look across pages.
_𝐿𝑎𝑦𝑜𝑢𝑡. 𝑐𝑠ℎ𝑡𝑚𝑙 (in the 𝑉𝑖𝑒𝑤𝑠/𝑆ℎ𝑎𝑟𝑒𝑑 folder):
< ℎ𝑡𝑚𝑙 >
< ℎ𝑒𝑎𝑑 >
< 𝑡𝑖𝑡𝑙𝑒 > @𝑉𝑖𝑒𝑤𝐵𝑎𝑔. 𝑇𝑖𝑡𝑙𝑒 </𝑡𝑖𝑡𝑙𝑒 >
</ℎ𝑒𝑎𝑑 >
< 𝑏𝑜𝑑𝑦 >
@𝑅𝑒𝑛𝑑𝑒𝑟𝐵𝑜𝑑𝑦( )
</𝑏𝑜𝑑𝑦 >
</ℎ𝑡𝑚𝑙 >
Other views can use this layout:
@{
𝐿𝑎𝑦𝑜𝑢𝑡 = "~/𝑉𝑖𝑒𝑤𝑠/𝑆ℎ𝑎𝑟𝑒𝑑/_𝐿𝑎𝑦𝑜𝑢𝑡. 𝑐𝑠ℎ𝑡𝑚𝑙";
}
Helpers and Partial Views
Razor views often use HTML Helpers and Partial Views to organize and reuse code:
Example of HTML Helper:
@𝐻𝑡𝑚𝑙. 𝑇𝑒𝑥𝑡𝐵𝑜𝑥𝐹𝑜𝑟(𝑚𝑜𝑑𝑒𝑙 => 𝑚𝑜𝑑𝑒𝑙. 𝐸𝑚𝑎𝑖𝑙)
Partial View:
@𝐻𝑡𝑚𝑙. 𝑃𝑎𝑟𝑡𝑖𝑎𝑙("_𝑆𝑡𝑢𝑑𝑒𝑛𝑡𝐷𝑒𝑡𝑎𝑖𝑙𝑠", 𝑀𝑜𝑑𝑒𝑙. 𝑆𝑡𝑢𝑑𝑒𝑛𝑡)
These tools keep Razor code modular and maintainable.
By Daud Anjum 87
ENTERPRISE SYSTEMS SI-342
Advantages of Razor Syntax
• Clean syntax with minimal code-switching
• Automatic HTML encoding increases security
• Strongly typed views with IntelliSense support
• Fast rendering and optimized performance
• Well-integrated with [Link] MVC conventions
Code Expressions
In [Link] MVC, code expressions are used primarily within Razor views (. 𝑐𝑠ℎ𝑡𝑚𝑙 files) to
embed server-side logic within HTML markup. Razor syntax uses these expressions to
dynamically generate content, interact with models, and control rendering logic. Code expressions
are essential for creating interactive and data-driven web pages while maintaining separation of
concerns between the controller logic and the view.
Understanding Code Expressions
A code expression in Razor starts with the @ symbol followed by a C# expression, such as a
variable, method call, or property access. When the Razor engine processes the view, it evaluates
these expressions and injects the result into the HTML output that is sent to the browser.
Example:
<𝑝>𝑊𝑒𝑙𝑐𝑜𝑚𝑒, @𝑀𝑜𝑑𝑒𝑙. 𝑈𝑠𝑒𝑟𝑁𝑎𝑚𝑒! </𝑝>
In this example, @𝑀𝑜𝑑𝑒𝑙. 𝑈𝑠𝑒𝑟𝑁𝑎𝑚𝑒 is a code expression that outputs the current user's name.
Types of Code Expressions
Code expressions can be categorized into the following types:
1) Inline Expressions
These are used to embed small pieces of logic or variable references inside HTML markup:
< 𝑝 > 𝑇ℎ𝑒 𝑐𝑢𝑟𝑟𝑒𝑛𝑡 𝑦𝑒𝑎𝑟 𝑖𝑠 @𝐷𝑎𝑡𝑒𝑇𝑖𝑚𝑒. 𝑁𝑜𝑤. 𝑌𝑒𝑎𝑟. </𝑝 >
2) Variables Assignments and Code Blocks
Although not expressions themselves, you can declare variables or execute logic using Razor
code blocks:
@{
𝑣𝑎𝑟 𝑚𝑒𝑠𝑠𝑎𝑔𝑒 = "𝐻𝑒𝑙𝑙𝑜 𝑓𝑟𝑜𝑚 𝑅𝑎𝑧𝑜𝑟! ";
}
< 𝑝 > @𝑚𝑒𝑠𝑠𝑎𝑔𝑒 </𝑝 >
3) Control Structures as Expressions
Control structures like 𝑖𝑓, 𝑓𝑜𝑟, 𝑓𝑜𝑟𝑒𝑎𝑐ℎ, and 𝑠𝑤𝑖𝑡𝑐ℎ are commonly used with Razor
expressions to control the rendering of HTML:
By Daud Anjum 88
ENTERPRISE SYSTEMS SI-342
@𝑓𝑜𝑟𝑒𝑎𝑐ℎ (𝑣𝑎𝑟 𝑖𝑡𝑒𝑚 𝑖𝑛 𝑀𝑜𝑑𝑒𝑙. 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑠) {
< 𝑙𝑖 > @𝑖𝑡𝑒𝑚. 𝑁𝑎𝑚𝑒 − $@𝑖𝑡𝑒𝑚. 𝑃𝑟𝑖𝑐𝑒 </𝑙𝑖 >
}
Combining HTML and Code Expressions
Code expressions are especially powerful when you need to combine C# logic with HTML
formatting:
@𝑖𝑓 (𝑀𝑜𝑑𝑒𝑙. 𝐼𝑠𝐿𝑜𝑔𝑔𝑒𝑑𝐼𝑛) {
< 𝑝 > 𝐻𝑒𝑙𝑙𝑜, @𝑀𝑜𝑑𝑒𝑙. 𝑁𝑎𝑚𝑒! </𝑝 >
} 𝑒𝑙𝑠𝑒 {
< 𝑝 > 𝑃𝑙𝑒𝑎𝑠𝑒 𝑙𝑜𝑔 𝑖𝑛. </𝑝 >
}
They allow you to tailor the HTML output based on application state or user input.
Best Practices with Code Expressions
• Keep logic in the controller; use expressions only to render output
• Avoid complex logic in Razor views – use ViewModels when needed
• Make use of 𝐻𝑡𝑚𝑙𝐻𝑒𝑙𝑝𝑒𝑟𝑠 and 𝑇𝑎𝑔𝐻𝑒𝑙𝑝𝑒𝑟𝑠 to simplify repetitive code
Code Blocks
In [Link] MVC, code blocks refer to sections of server-side C# code written within Razor views
using the @{ . .. } syntax. Unlike code expressions, which output values directly to the HTML
response, code blocks are used to perform operations, declare variables, execute loops or
conditional statements, and control logic without necessarily generating any direct output.
Purpose and Use of Code Blocks
Code blocks are essential for embedding C# logic directly within a . 𝑐𝑠ℎ𝑡𝑚𝑙 Razor view file. These
blocks help you perform backend operations, such as calculations, model manipulations, and flow
control, all while keeping the logic close to the view rendering.
For example:
@{
𝑣𝑎𝑟 𝑐𝑢𝑟𝑟𝑒𝑛𝑡𝑌𝑒𝑎𝑟 = 𝐷𝑎𝑡𝑒𝑇𝑖𝑚𝑒. 𝑁𝑜𝑤. 𝑌𝑒𝑎𝑟;
}
< 𝑝 > 𝐶𝑢𝑟𝑟𝑒𝑛𝑡 𝑌𝑒𝑎𝑟: @𝑐𝑢𝑟𝑟𝑒𝑛𝑡𝑌𝑒𝑎𝑟 </𝑝 >
Here, the 𝑐𝑢𝑟𝑟𝑒𝑛𝑡𝑌𝑒𝑎𝑟 variable is declared inside a code block and later accessed using a code
expression.
Syntax of Code Blocks
The syntax of a code block in Razor is simple:
By Daud Anjum 89
ENTERPRISE SYSTEMS SI-342
@{
// 𝐶# 𝑐𝑜𝑑𝑒 ℎ𝑒𝑟𝑒
}
You can include:
• Variable declarations
• 𝑖𝑓, 𝑒𝑙𝑠𝑒, 𝑠𝑤𝑖𝑡𝑐ℎ statements
• 𝑓𝑜𝑟, 𝑓𝑜𝑟𝑒𝑎𝑐ℎ, 𝑤ℎ𝑖𝑙𝑒 loops
• Method calls
• Any other valid C# logic
Example:
@{
𝑠𝑡𝑟𝑖𝑛𝑔 𝑔𝑟𝑒𝑒𝑡𝑖𝑛𝑔;
𝑖𝑓 (𝐷𝑎𝑡𝑒𝑇𝑖𝑚𝑒. 𝑁𝑜𝑤. 𝐻𝑜𝑢𝑟 < 12) {
𝑔𝑟𝑒𝑒𝑡𝑖𝑛𝑔 = "𝐺𝑜𝑜𝑑 𝑚𝑜𝑟𝑛𝑖𝑛𝑔! ";
} 𝑒𝑙𝑠𝑒 {
𝑔𝑟𝑒𝑒𝑡𝑖𝑛𝑔 = "𝐺𝑜𝑜𝑑 𝑎𝑓𝑡𝑒𝑟𝑛𝑜𝑜𝑛! ";
}
}
< 𝑝 > @𝑔𝑟𝑒𝑒𝑡𝑖𝑛𝑔 </𝑝 >
Control Structures in Code Blocks
You can implement control structures directly inside code blocks. For example:
@{
𝑣𝑎𝑟 𝑛𝑎𝑚𝑒𝑠 = 𝑛𝑒𝑤 𝐿𝑖𝑠𝑡<𝑠𝑡𝑟𝑖𝑛𝑔> { "𝐴𝑙𝑖", "𝑆𝑎𝑟𝑎", "𝑈𝑠𝑚𝑎𝑛" };
}
< 𝑢𝑙 >
@𝑓𝑜𝑟𝑒𝑎𝑐ℎ (𝑣𝑎𝑟 𝑛𝑎𝑚𝑒 𝑖𝑛 𝑛𝑎𝑚𝑒𝑠) {
< 𝑙𝑖 > @𝑛𝑎𝑚𝑒 </𝑙𝑖 >
}
</𝑢𝑙 >
In this example, a list is created in a code block and then used in a 𝑓𝑜𝑟𝑒𝑎𝑐ℎ loop to display each
name in an unordered list.
Code Blocks vs Code Expressions
It’s important to distinguish code blocks from code expressions:
Feature Code Expression Code Block
Syntax @𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒 @{ … }
Output Directly writes output Does not directly write output
Use Displaying values Running logic or declaring
vars
Placement Inline with HTML Typically at top or isolated
By Daud Anjum 90
ENTERPRISE SYSTEMS SI-342
Implicit vs Explicit Code Expression
Razor, the view engine used in [Link] MVC, allows developers to embed C# code directly into
HTML markup using a clean and concise syntax. This embedding is accomplished through code
expressions. Code expressions in Razor can be implicit or explicit, depending on how they're
written and used within the HTML.
Understanding the difference between implicit and explicit code expressions is key to writing
clean, readable, and bug-free Razor views.
Implicit Code Expressions
Implicit code expressions are the most common and simple way to embed C# values or expressions
directly into HTML. They begin with the @ symbol, followed by a single variable or a simple
expression. Razor automatically treats this as output and renders the result to the HTML page.
Example:
< 𝑝 > 𝑊𝑒𝑙𝑐𝑜𝑚𝑒, @𝑈𝑠𝑒𝑟. 𝐼𝑑𝑒𝑛𝑡𝑖𝑡𝑦. 𝑁𝑎𝑚𝑒! </𝑝 >
Here, @𝑈𝑠𝑒𝑟. 𝐼𝑑𝑒𝑛𝑡𝑖𝑡𝑦. 𝑁𝑎𝑚𝑒 is an implicit code expression. Razor understands that the value
should be evaluated and its output inserted into the HTML.
Explicit Code Expressions
Explicit code expressions are used when Razor might get confused about where the code ends,
especially in more complex scenarios such as when the expression is part of an HTML attribute
or contains nested method calls or operations
These expressions are wrapped in parentheses like this:
@(𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛)
Example:
< 𝑝 > 𝑇𝑜𝑡𝑎𝑙: @(𝑝𝑟𝑖𝑐𝑒 ∗ 𝑞𝑢𝑎𝑛𝑡𝑖𝑡𝑦) </𝑝 >
If you tried to write @𝑝𝑟𝑖𝑐𝑒 ∗ 𝑞𝑢𝑎𝑛𝑡𝑖𝑡𝑦 without parentheses, Razor might interpret the * as part
of the HTML, leading to syntax errors or incorrect output.
Why the Distinction Matters
The Razor engine relies on specific rules to parse and render markup correctly. In simple cases,
implicit expressions are perfect. However, in more complex syntax – such as concatenation,
conditionals, or embedding in HTML attributes – explicit expressions provide clarity and avoid
parsing errors.
Failure to use explicit expressions in these cases can lead to:
• Compilation errors
• Incorrect or partial output
• HTML structure breaking
Comparison
Feature Implicit Expression Explicit Expression
By Daud Anjum 91
ENTERPRISE SYSTEMS SI-342
Syntax @𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛 @(𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛)
Simplicity Simple and clean Slightly more verbose
Use case Straightforward output Complex expressions or in
HTML attributes
Parsing Razor determines where it Clear boundaries with
ends parentheses
Data Annotations
Data Annotations are a powerful feature in [Link] MVC used for validation, formatting, and
metadata specification in models. They are attributes you can apply directly to your model
properties to enforce rules and control how data is handled, displayed, and validated.
These annotations help ensure data integrity and provide a way to apply business rules without
having to writ length validation logic manually. They are part of the
𝑆𝑦𝑠𝑡𝑒𝑚. 𝐶𝑜𝑚𝑝𝑜𝑛𝑒𝑛𝑡𝑀𝑜𝑑𝑒𝑙. 𝐷𝑎𝑡𝑎𝐴𝑛𝑛𝑜𝑡𝑎𝑡𝑖𝑜𝑛𝑠 namespace.
Purpose and Importance
The main goal of data annotations is to:
• Enforce server-side validation automatically
• Trigger client-side validation with the help of unobtrusive JS
• Reduce code duplication in the controller or view
• Help generate better scaffolding and UI in tools like Razor Pages and EF
By applying these attributes to model properties, developers can define constraints such as
required fields, field length, data types, and custom error messages.
Commonly Used Data Annotation Attributes
Below are some of the most widely used data annotations is [Link] MVC:
1) [Required]
Indicates that the field is mandatory.
[𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝑑(𝐸𝑟𝑟𝑜𝑟𝑀𝑒𝑠𝑠𝑎𝑔𝑒 = "𝑁𝑎𝑚𝑒 𝑖𝑠 𝑟𝑒𝑞𝑢𝑖𝑟𝑒𝑑")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑁𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
2) [StringLength]
Specifies the minimum and maximum length of a string.
[𝑆𝑡𝑟𝑖𝑛𝑔𝐿𝑒𝑛𝑔𝑡ℎ(50, 𝑀𝑖𝑛𝑖𝑚𝑢𝑚𝐿𝑒𝑛𝑔𝑡ℎ = 3)]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
3) [Range]
Restricts a numeric value to a specified range.
[𝑅𝑎𝑛𝑔𝑒(18, 60, 𝐸𝑟𝑟𝑜𝑟𝑀𝑒𝑠𝑠𝑎𝑔𝑒 = "𝐴𝑔𝑒 𝑚𝑢𝑠𝑡 𝑏𝑒 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 18 𝑎𝑛𝑑 60")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝐴𝑔𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
By Daud Anjum 92
ENTERPRISE SYSTEMS SI-342
4) [DataType]
Provides hints for the UI about the format of the data.
[𝐷𝑎𝑡𝑎𝑇𝑦𝑝𝑒 (𝐷𝑎𝑡𝑎𝑇𝑦𝑝𝑒. 𝐷𝑎𝑡𝑒)]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐷𝑎𝑡𝑒𝑇𝑖𝑚𝑒 𝐵𝑖𝑟𝑡ℎ𝐷𝑎𝑡𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
5) [Display]
Sets a user-friendly name for the property.
[𝐷𝑖𝑠𝑝𝑙𝑎𝑦(𝑁𝑎𝑚𝑒 = "𝐹𝑢𝑙𝑙 𝑁𝑎𝑚𝑒")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝐹𝑢𝑙𝑙𝑛𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
6) [EmailAddress]
Validates that the value is in a proper email format.
[𝐸𝑚𝑎𝑖𝑙𝐴𝑑𝑑𝑟𝑒𝑠𝑠(𝐸𝑟𝑟𝑜𝑟𝑀𝑒𝑠𝑠𝑎𝑔𝑒 = "𝐼𝑛𝑣𝑎𝑙𝑖𝑑 𝐸𝑚𝑎𝑖𝑙")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝐸𝑚𝑎𝑖𝑙 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
7) [Compare]
Compares two fields, useful for confirming passwords.
[𝐶𝑜𝑚𝑝𝑎𝑟𝑒("𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑", 𝐸𝑟𝑟𝑜𝑟𝑀𝑒𝑠𝑠𝑎𝑔𝑒 = "𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑𝑠 𝑑𝑜 𝑛𝑜𝑡 𝑚𝑎𝑡𝑐ℎ")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝐶𝑜𝑛𝑓𝑖𝑟𝑚𝑃𝑎𝑠𝑠𝑤𝑜𝑟𝑑 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
8) [RegularExpression]
Validates the value using a regular expression pattern.
[𝑅𝑒𝑔𝑢𝑙𝑎𝑟𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛(@"^\𝑑{5}$", 𝐸𝑟𝑟𝑜𝑟𝑀𝑒𝑠𝑠𝑎𝑔𝑒 = "𝐸𝑛𝑡𝑒𝑟 5-𝑑𝑖𝑔𝑖𝑡 𝑍𝐼𝑃 𝑐𝑜𝑑𝑒")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑍𝑖𝑝𝐶𝑜𝑑𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
Client-Side and Server-Side Validation
[Link] MVC integrated data annotations with client-side validation using jQuery Unobtrusive
Validation. When the page is loaded, the framework converts data annotations into JS validation
rules. This reduces the load on the server and provides immediate feedback to users.
However, all validation must also occur server-side, as client-side validation can be bypassed.
[Link] MVC ensures that data annotations are respected when the controller checks the
𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐼𝑠𝑉𝑎𝑙𝑖𝑑 property before processing a request.
Model Validation Flow
1) User submits a form.
2) MVC framework validates model properties based on the data annotations
3) If validation fails:
• Client-side: errors are shown instantly
• Server-side: controller sees 𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐼𝑠𝑉𝑎𝑙𝑖𝑑 as 𝑓𝑎𝑙𝑠𝑒, and the view is re-rendered
with error messages
4) If validation passes controller proceeds with processing (e.g., saving data to database).
Custom Validation with Data Annotations
Sometimes the built-in attributes are not enough. You can create custom annotations by inheriting
from the 𝑉𝑎𝑙𝑖𝑑𝑎𝑡𝑖𝑜𝑛𝐴𝑡𝑡𝑟𝑖𝑏𝑢𝑡𝑒 class.
By Daud Anjum 93
ENTERPRISE SYSTEMS SI-342
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑁𝑜𝑆𝑝𝑒𝑐𝑖𝑎𝑙𝐶ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟𝑠𝐴𝑡𝑡𝑟𝑖𝑏𝑢𝑡𝑒 ∶ 𝑉𝑎𝑙𝑖𝑑𝑎𝑡𝑖𝑜𝑛𝐴𝑡𝑡𝑟𝑖𝑏𝑢𝑡𝑒
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝑜𝑣𝑒𝑟𝑟𝑖𝑑𝑒 𝑏𝑜𝑜𝑙 𝐼𝑠𝑉𝑎𝑙𝑖𝑑(𝑜𝑏𝑗𝑒𝑐𝑡 𝑣𝑎𝑙𝑢𝑒)
{
𝑣𝑎𝑟 𝑠𝑡𝑟 = 𝑣𝑎𝑙𝑢𝑒 𝑎𝑠 𝑠𝑡𝑟𝑖𝑛𝑔;
𝑟𝑒𝑡𝑢𝑟𝑛 𝑠𝑡𝑟 ! = 𝑛𝑢𝑙𝑙 && ! 𝑅𝑒𝑔𝑒𝑥. 𝐼𝑠𝑀𝑎𝑡𝑐ℎ(𝑠𝑡𝑟, @"[^𝑎-𝑧𝐴-𝑍0-9]");
}
}
Then apply it:
[𝑁𝑜𝑆𝑝𝑒𝑐𝑖𝑎𝑙𝐶ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟𝑠(𝐸𝑟𝑟𝑜𝑟𝑀𝑒𝑠𝑠𝑎𝑔𝑒 = "𝑆𝑝𝑒𝑐𝑖𝑎𝑙 𝑐ℎ𝑎𝑟𝑎𝑐𝑡𝑒𝑟𝑠 𝑎𝑟𝑒 𝑛𝑜𝑡 𝑎𝑙𝑙𝑜𝑤𝑒𝑑. ")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑈𝑠𝑒𝑟𝑛𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
Benefits of Using Data Annotations
• Declarative and concise
• Easily understood and maintained
• Seamless integration with both Razor and controller logic
• Automatically integrated with model binding and scaffolding
• Reduces code duplication and increases maintainability
Client and Server-Side Validation
Validation is a critical aspect of web application development. It ensures that data entered by users
conforms to expected formats and business rules before it is processed or stored. In [Link]
MVC, validation is typically implemented in two layers: client-side validation and server-side
validation. Together, they ensure robustness, better UX, and application security.
Server-Side Validation
It occurs after the user submits the form, and the dat is posted back to the server. The controller
checks whether the model passed to it is valid using the 𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐼𝑠𝑉𝑎𝑙𝑖𝑑 property. This is
considered the most secure and essential form of validation, as client-side scripts can be disabled
or manipulated.
Key features:
• Secure: Cannot be bypassed by disabling JS or modifying client code.
• Reliable: Always runs on the server regardless of the user’s browser or settings.
• Supports complex logic: You can write custom validation rules and access external resources
(like databases).
By Daud Anjum 94
ENTERPRISE SYSTEMS SI-342
Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝑆𝑢𝑏𝑚𝑖𝑡𝐹𝑜𝑟𝑚(𝑈𝑠𝑒𝑟𝑀𝑜𝑑𝑒𝑙 𝑚𝑜𝑑𝑒𝑙)
{
𝑖𝑓 (𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐼𝑠𝑉𝑎𝑙𝑖𝑑)
{
// 𝑃𝑟𝑜𝑐𝑒𝑠𝑠 𝑡ℎ𝑒 𝑑𝑎𝑡𝑎 (𝑒. 𝑔. , 𝑠𝑎𝑣𝑒 𝑡𝑜 𝑑𝑎𝑡𝑎𝑏𝑎𝑠𝑒)
}
𝑒𝑙𝑠𝑒
{
// 𝑅𝑒𝑡𝑢𝑟𝑛 𝑓𝑜𝑟𝑚 𝑤𝑖𝑡ℎ 𝑣𝑎𝑙𝑖𝑑𝑎𝑡𝑖𝑜𝑛 𝑚𝑒𝑠𝑠𝑎𝑔𝑒𝑠
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤(𝑚𝑜𝑑𝑒𝑙);
}
}
Here, 𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐼𝑠𝑉𝑎𝑙𝑖𝑑 ensures that all data annotations or custom validations on the model
are satisfied before proceeding.
Client-Side Validation
It takes place in the user's browser before the data is submitted to the server. It provides immediate
feedback, improves UX, and reduces the load on the server. [Link] MVC uses unobtrusive JS
and jQuery Validation to implement client-side validation automatically based on model metadata
and data annotations.
Key features:
• Fast feedback: Errors are shown immediately when the user leaves a field or tries to submit a
form.
• Reduces server load: Prevents unnecessary requests by catching errors early.
• Improves UX: Helps users correct mistakes without page reloads.
Setup Requirements:
To enable client-side validation, make sure:
• jQuery and jQuery Validation libraries are included.
• Unobtrusive validation is enabled in 𝑤𝑒𝑏. 𝑐𝑜𝑛𝑓𝑖𝑔 or via script:
< 𝑎𝑝𝑝𝑆𝑒𝑡𝑡𝑖𝑛𝑔𝑠 >
< 𝑎𝑑𝑑 𝑘𝑒𝑦 = "𝐶𝑙𝑖𝑒𝑛𝑡𝑉𝑎𝑙𝑖𝑑𝑎𝑡𝑖𝑜𝑛𝐸𝑛𝑎𝑏𝑙𝑒𝑑" 𝑣𝑎𝑙𝑢𝑒 = "𝑡𝑟𝑢𝑒" />
< 𝑎𝑑𝑑 𝑘𝑒𝑦 = "𝑈𝑛𝑜𝑏𝑡𝑟𝑢𝑠𝑖𝑣𝑒𝐽𝑎𝑣𝑎𝑆𝑐𝑟𝑖𝑝𝑡𝐸𝑛𝑎𝑏𝑙𝑒𝑑" 𝑣𝑎𝑙𝑢𝑒 = "𝑡𝑟𝑢𝑒" />
</𝑎𝑝𝑝𝑆𝑒𝑡𝑡𝑖𝑛𝑔𝑠 >
[Link] MVC will automatically generate data-* attributes in the HTML that jQuery Validation
uses to enforce rules.
Example output HTML:
< 𝑖𝑛𝑝𝑢𝑡 𝑑𝑎𝑡𝑎-𝑣𝑎𝑙="𝑡𝑟𝑢𝑒" 𝑑𝑎𝑡𝑎-𝑣𝑎𝑙-𝑟𝑒𝑞𝑢𝑖𝑟𝑒𝑑="𝑁𝑎𝑚𝑒 𝑖𝑠 𝑟𝑒𝑞𝑢𝑖𝑟𝑒𝑑" 𝑛𝑎𝑚𝑒="𝑁𝑎𝑚𝑒" />
< 𝑠𝑝𝑎𝑛 𝑑𝑎𝑡𝑎-𝑣𝑎𝑙𝑚𝑠𝑔-𝑓𝑜𝑟="𝑁𝑎𝑚𝑒" 𝑐𝑙𝑎𝑠𝑠="𝑡𝑒𝑥𝑡-𝑑𝑎𝑛𝑔𝑒𝑟" ></𝑠𝑝𝑎𝑛 >
By Daud Anjum 95
ENTERPRISE SYSTEMS SI-342
Using Both Together
[Link] MVC promotes using both client-side and server-side validation. This provides the best
of both worlds:
• Client-side validation for responsiveness and UX
• Server-side validation security and reliability
Even if client-side validation is disabled (e.g., user turns off JS), the server-side validation ensures
that invalid data cannot be processed.
How They Work Together in MVC
1) Model Annotations are written in the model using attributes like [𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝑑],
[𝑆𝑡𝑟𝑖𝑛𝑔𝐿𝑒𝑛𝑔𝑡ℎ], etc.
2) Client-Side: These annotations are rendered as 𝑑𝑎𝑡𝑎-𝑣𝑎𝑙 HTML attributes, and JS/jQuery
handles the validation in-browser.
3) Server-Side: When the form is submitted, the controller validates the model using
𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐼𝑠𝑉𝑎𝑙𝑖𝑑.
4) If either fails, the form is redisplayed with error messages.
Validation and Model Binding
In [Link] MVC, validation and model binding are tightly integrated features that play a crucial
role in handling form submissions, processing user input, and maintaining application integrity.
Together, they enable developers to capture data from views, validate it against business rules, and
pass it seamlessly to controller actions using strongly-typed models.
What is Model Binding?
Model Binding is the mechanism by which [Link] MVC maps HTTP request data (form fields,
query strings, route values, etc.) to the parameters of controller action methods. Instead of
manually extracting each form field or query string value, MVC allows you to define a parameter
of a customer model type in your action, and it automatically populated it based on the submitted
data.
Example:
Suppose you have a model class:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑈𝑠𝑒𝑟𝑀𝑜𝑑𝑒𝑙
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑁𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝐴𝑔𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
}
By Daud Anjum 96
ENTERPRISE SYSTEMS SI-342
And a controller action:
[𝐻𝑡𝑡𝑝𝑃𝑜𝑠𝑡]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝑆𝑢𝑏𝑚𝑖𝑡(𝑈𝑠𝑒𝑟𝑀𝑜𝑑𝑒𝑙 𝑚𝑜𝑑𝑒𝑙)
{
// 𝑀𝑜𝑑𝑒𝑙 𝐵𝑖𝑛𝑑𝑖𝑛𝑔 𝑤𝑖𝑙𝑙 𝑝𝑜𝑝𝑢𝑙𝑎𝑡𝑒 ′𝑚𝑜𝑑𝑒𝑙′ 𝑤𝑖𝑡ℎ 𝑣𝑎𝑙𝑢𝑒𝑠 𝑓𝑟𝑜𝑚 𝑡ℎ𝑒 𝑓𝑜𝑟𝑚
}
When a user submits a form with fields names 𝑁𝑎𝑚𝑒 and 𝐴𝑔𝑒, MVC will automatically bind
those values to the 𝑈𝑠𝑒𝑟𝑀𝑜𝑑𝑒𝑙 instance, which is passed to the controller.
How Model Binding Works
• It looks for matching names in the incoming request (form data, query strings, route data).
• It creates an instance of the model and assigns values to its properties.
• If any values can’t be converted to the expected types (e.g., a string into an int), model binding
flags them as errors in 𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒.
Model binding supports complex objects, collections, nested properties, and even dictionaries.
What is Validation?
Validation ensures that the data bound to models meets certain conditions or business rules before
it’s processed. [Link] MVC supports validation using data annotations, custom logic, and third-
party libraries.
The built-in validation mechanism uses attributes applied to model properties, such as:
• [Required]
• [StringLength]
• [Range]
• [RegularExpression]
• [EmailAddress]
These attributes define rules that the model must satisfy.
Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑈𝑠𝑒𝑟𝑀𝑜𝑑𝑒𝑙
{
[𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝑑(𝐸𝑟𝑟𝑜𝑟𝑀𝑒𝑠𝑠𝑎𝑔𝑒 = "𝑁𝑎𝑚𝑒 𝑖𝑠 𝑟𝑒𝑞𝑢𝑖𝑟𝑒𝑑")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑁𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
[𝑅𝑎𝑛𝑔𝑒(18, 60, 𝐸𝑟𝑟𝑜𝑟𝑀𝑒𝑠𝑠𝑎𝑔𝑒 = "𝐴𝑔𝑒 𝑚𝑢𝑠𝑡 𝑏𝑒 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 18 𝑎𝑛𝑑 60")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝐴𝑔𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
}
Integration of Validation with Model Binding
Once the Model Binder populates the model, [Link] MVC automatically validates the model
based on the data annotations defined on its properties. The result of this validation is stored in a
property called 𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒.
By Daud Anjum 97
ENTERPRISE SYSTEMS SI-342
You can check validation status in you controller like this:
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝑆𝑢𝑏𝑚𝑖𝑡(𝑈𝑠𝑒𝑟𝑀𝑜𝑑𝑒𝑙 𝑚𝑜𝑑𝑒𝑙)
{
𝑖𝑓 (𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐼𝑠𝑉𝑎𝑙𝑖𝑑)
{
// 𝑃𝑟𝑜𝑐𝑒𝑒𝑑 𝑖𝑓 𝑑𝑎𝑡𝑎 𝑖𝑠 𝑣𝑎𝑙𝑖𝑑
}
𝑒𝑙𝑠𝑒
{
// 𝑅𝑒𝑡𝑢𝑟𝑛 𝑡𝑜 𝑡ℎ𝑒 𝑣𝑖𝑒𝑤 𝑤𝑖𝑡ℎ 𝑣𝑎𝑙𝑖𝑑𝑎𝑡𝑖𝑜𝑛 𝑒𝑟𝑟𝑜𝑟𝑠
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤(𝑚𝑜𝑑𝑒𝑙);
}
}
If validation fails, error messages can be displayed on the view using HTML helpers like:
@𝐻𝑡𝑚𝑙. 𝑉𝑎𝑙𝑖𝑑𝑎𝑡𝑖𝑜𝑛𝑀𝑒𝑠𝑠𝑎𝑔𝑒𝐹𝑜𝑟(𝑚 => 𝑚. 𝑁𝑎𝑚𝑒)
Or show all messages at once using:
@𝐻𝑡𝑚𝑙. 𝑉𝑎𝑙𝑖𝑑𝑎𝑡𝑖𝑜𝑛𝑆𝑢𝑚𝑚𝑎𝑟𝑦( )
Client-Side Validation via Model Binding
[Link] MVC also supports client-side validation by generating the appropriate HTML5 data
attributes based on model annotations. This works with unobtrusive JS and jQuery Validation,
allowing real-time validation in the browser before submission.
Example:
<𝑖𝑛𝑝𝑢𝑡 𝑑𝑎𝑡𝑎-𝑣𝑎𝑙="𝑡𝑟𝑢𝑒" 𝑑𝑎𝑡𝑎-𝑣𝑎𝑙-𝑟𝑒𝑞𝑢𝑖𝑟𝑒𝑑="𝑁𝑎𝑚𝑒 𝑖𝑠 𝑟𝑒𝑞𝑢𝑖𝑟𝑒𝑑" 𝑛𝑎𝑚𝑒="𝑁𝑎𝑚𝑒" />
These are automatically rendered based on your data annotations.
Custom Model Binders and Validators
While the default model binder works well in most cases, you can create custom model binders to
handle special scenarios like file uploads, custom data formats, or complex nested objects.
Similarly, custom validation attributes can be created by extending 𝑉𝑎𝑙𝑖𝑑𝑎𝑡𝑖𝑜𝑛𝐴𝑡𝑡𝑟𝑖𝑏𝑢𝑡𝑒.
Custom Binder Example:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝐶𝑢𝑠𝑡𝑜𝑚𝐷𝑎𝑡𝑒𝑀𝑜𝑑𝑒𝑙𝐵𝑖𝑛𝑑𝑒𝑟 ∶ 𝐼𝑀𝑜𝑑𝑒𝑙𝐵𝑖𝑛𝑑𝑒𝑟
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝑜𝑏𝑗𝑒𝑐𝑡 𝐵𝑖𝑛𝑑𝑀𝑜𝑑𝑒𝑙(𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟𝐶𝑜𝑛𝑡𝑒𝑥𝑡 𝑐𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟𝐶𝑜𝑛𝑡𝑒𝑥𝑡,
𝑀𝑜𝑑𝑒𝑙𝐵𝑖𝑛𝑑𝑖𝑛𝑔𝐶𝑜𝑛𝑡𝑒𝑥𝑡 𝑏𝑖𝑛𝑑𝑖𝑛𝑔𝐶𝑜𝑛𝑡𝑒𝑥𝑡)
{
𝑠𝑡𝑟𝑖𝑛𝑔 𝑣𝑎𝑙𝑢𝑒 = 𝑐𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟𝐶𝑜𝑛𝑡𝑒𝑥𝑡. 𝐻𝑡𝑡𝑝𝐶𝑜𝑛𝑡𝑒𝑥𝑡. 𝑅𝑒𝑞𝑢𝑒𝑠𝑡. 𝐹𝑜𝑟𝑚["𝐷𝑎𝑡𝑒"];
𝑟𝑒𝑡𝑢𝑟𝑛 𝐷𝑎𝑡𝑒𝑇𝑖𝑚𝑒. 𝑃𝑎𝑟𝑠𝑒𝐸𝑥𝑎𝑐𝑡(𝑣𝑎𝑙𝑢𝑒, "𝑑𝑑 − 𝑀𝑀 − 𝑦𝑦𝑦𝑦", 𝑛𝑢𝑙𝑙);
}
}
By Daud Anjum 98
ENTERPRISE SYSTEMS SI-342
Validation and Model State
In [Link] MVC, Validation and ModelState are closely related concepts that work together to
ensure that user input is correct, complete, and adheres to defined rules before being processed or
saved. This relationship is essential in web application development to maintain data integrity,
enhance UX, and avoid runtime errors.
What is Validation?
Validation refers to the process of checking if the user input meets the defined criteria. In [Link]
MVC, validation can be done both on the client side (before the form is submitted) and on the
server side (after form submission). The framework supports:
• Declarative Validation using Data Annotations
• Custom Validation logic
• Fluent Validation (via third-party libraries)
What is ModelState?
The ModelState object in [Link] MVC holds the state of the model binding process and
contains both the values submitted by the user and validation errors (if any).
ModelState is a dictionary that contains all the fields submitted with the form and tracks whether
each field passed validation. It allows the controller to check if the user input is valid before
proceeding with any logic such as saving to a database.
You can access it in a controller action like this:
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝑆𝑢𝑏𝑚𝑖𝑡(𝑈𝑠𝑒𝑟𝑀𝑜𝑑𝑒𝑙 𝑚𝑜𝑑𝑒𝑙)
{
𝑖𝑓 (𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐼𝑠𝑉𝑎𝑙𝑖𝑑)
{
// 𝑃𝑟𝑜𝑐𝑒𝑒𝑑 𝑖𝑓 𝑑𝑎𝑡𝑎 𝑖𝑠 𝑣𝑎𝑙𝑖𝑑
}
𝑒𝑙𝑠𝑒
{
// 𝑅𝑒𝑡𝑢𝑟𝑛 𝑡𝑜 𝑡ℎ𝑒 𝑣𝑖𝑒𝑤 𝑤𝑖𝑡ℎ 𝑣𝑎𝑙𝑖𝑑𝑎𝑡𝑖𝑜𝑛 𝑒𝑟𝑟𝑜𝑟𝑠
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤(𝑚𝑜𝑑𝑒𝑙);
}
}
How Validation and ModelState Work Together
1) User submits form
2) [Link] MVC uses Model Binding to map form values to model properties
3) While binding, it automatically checks validation rules defined using data annotations
4) If any rule is violated, the error message is stored in 𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒 and the 𝐼𝑠𝑉𝑎𝑙𝑖𝑑 flag becomes
false
5) The controller checks 𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐼𝑠𝑉𝑎𝑙𝑖𝑑 to decide what action to take next
By Daud Anjum 99
ENTERPRISE SYSTEMS SI-342
Clearing ModelState Errors
Sometimes, you may want to clear existing errors from the ModelState (e.g., when reusing a model
or resetting form state):
𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒. 𝐶𝑙𝑒𝑎𝑟( )
This is rarely needed but can be helpful in some scenarios like dynamic form submissions or AJAX
responses.
Common Scenarios Where ModelState is Important
• Preventing invalid data from being saved to the database
• Returning the same view with error messages and preventing user input
• Highlighting specific fields with errors to improve UX
• Debugging form submissions in development using 𝑀𝑜𝑑𝑒𝑙𝑆𝑡𝑎𝑡𝑒 errors
MVC Membership
MVC membership refers to the user authentication and authorization system used in [Link]
MVC applications. It enables developers to manage user accounts, login functionality, roles, and
access control. While the term “Membership” originates from the [Link] Membership Provider
in Web Forms, in MVC (especially MVC 4 and onwards), this evolved into the [Link] Identity
system, which is more flexible, extensible, and built with modern security needs in mind.
Historical Context
Initially, the [Link] Membership system provided a basic way to handle user credentials and
role-based access, using SQL Server databases. However, it was rigid and hard to customize. In
response, [Link] Identity was introduced and became the standard in MVC applications,
offering improved support for:
• Claims-based authentication
• OAuth/OpenID integration (Google, Facebook, etc)
• Custom user fields
• Roles and policies
• EF integration
Core Concepts of MVC Membership
1) Authentication vs Authorization
• Authentication: Verifies who the user is (e.g., login using username/password)
• Authorization: Determines what the authenticated user is allowed to do (e.g., Admin can
access dashboard, User cannot)
By Daud Anjum 100
ENTERPRISE SYSTEMS SI-342
2) User Accounts
In MVC, a user account is typically managed using the 𝐼𝑑𝑒𝑛𝑡𝑖𝑡𝑦𝑈𝑠𝑒𝑟 class. A database table
stores usernames, passwords (hashed), and related user data. You can add custom fields like
Name, Address, etc., by extending this class.
3) Login and Registration
MVC projects with Identity typically include built-in AccountController, which handles:
• User registration
• Login/Logout
• Password recovery
• External logins (Google, Facebook, etc.)
Role-Based Membership
[Link] Identity also supports role-based access control (RBAC), allowing you to create roles
like 𝐴𝑑𝑚𝑖𝑛, 𝑀𝑎𝑛𝑎𝑔𝑒𝑟, 𝑈𝑠𝑒𝑟, etc.
Creating Roles:
𝑣𝑎𝑟 𝑟𝑜𝑙𝑒𝑀𝑎𝑛𝑎𝑔𝑒𝑟 = 𝑛𝑒𝑤 𝑅𝑜𝑙𝑒𝑀𝑎𝑛𝑎𝑔𝑒𝑟<𝐼𝑑𝑒𝑛𝑡𝑖𝑡𝑦𝑅𝑜𝑙𝑒>(𝑛𝑒𝑤
𝑅𝑜𝑙𝑒𝑆𝑡𝑜𝑟𝑒<𝐼𝑑𝑒𝑛𝑡𝑖𝑡𝑦𝑅𝑜𝑙𝑒>(𝑐𝑜𝑛𝑡𝑒𝑥𝑡));
𝑟𝑜𝑙𝑒𝑀𝑎𝑛𝑎𝑔𝑒𝑟. 𝐶𝑟𝑒𝑎𝑡𝑒(𝑛𝑒𝑤 𝐼𝑑𝑒𝑛𝑡𝑖𝑡𝑦𝑅𝑜𝑙𝑒("𝐴𝑑𝑚𝑖𝑛"));
Assigning Roles:
𝑣𝑎𝑟 𝑢𝑠𝑒𝑟𝑀𝑎𝑛𝑎𝑔𝑒𝑟 = 𝑛𝑒𝑤 𝑈𝑠𝑒𝑟𝑀𝑎𝑛𝑎𝑔𝑒𝑟<𝐴𝑝𝑝𝑙𝑖𝑐𝑎𝑡𝑖𝑜𝑛𝑈𝑠𝑒𝑟>(𝑛𝑒𝑤
𝑈𝑠𝑒𝑟𝑆𝑡𝑜𝑟𝑒<𝐴𝑝𝑝𝑙𝑖𝑐𝑎𝑡𝑖𝑜𝑛𝑈𝑠𝑒𝑟>(𝑐𝑜𝑛𝑡𝑒𝑥𝑡));
𝑢𝑠𝑒𝑟𝑀𝑎𝑛𝑎𝑔𝑒𝑟. 𝐴𝑑𝑑𝑇𝑜𝑅𝑜𝑙𝑒(𝑢𝑠𝑒𝑟𝐼𝑑, "𝐴𝑑𝑚𝑖𝑛");
Authorizing Roles in Controllers:
[𝐴𝑢𝑡ℎ𝑜𝑟𝑖𝑧𝑒(𝑅𝑜𝑙𝑒𝑠 = "𝐴𝑑𝑚𝑖𝑛")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐴𝑑𝑚𝑖𝑛𝑃𝑎𝑛𝑒𝑙( )
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤( );
}
This ensures that only users with the “Admin” role can access that action or view.
Claims-Based Identity
In addition to roles, [Link] Identity supports claims-based identity, allowing more granular
access control.
• A claim is a key-value pair (e.g., 𝐴𝑔𝑒: 25, 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡: 𝐻𝑅)
• Claims can be used for fine-grained authorization rules, like "Allow access only to users from
HR department".
By Daud Anjum 101
ENTERPRISE SYSTEMS SI-342
Membership Storage
By default, MVC stores membership data in a SQL Server database using EF Code First. This
includes tables like:
• 𝐴𝑠𝑝𝑁𝑒𝑡𝑈𝑠𝑒𝑟𝑠
• 𝐴𝑠𝑝𝑁𝑒𝑡𝑅𝑜𝑙𝑒𝑠
• 𝐴𝑠𝑝𝑁𝑒𝑡𝑈𝑠𝑒𝑟𝑅𝑜𝑙𝑒𝑠
• 𝐴𝑠𝑝𝑁𝑒𝑡𝑈𝑠𝑒𝑟𝐶𝑙𝑎𝑖𝑚𝑠
• 𝐴𝑠𝑝𝑁𝑒𝑡𝑈𝑠𝑒𝑟𝐿𝑜𝑔𝑖𝑛𝑠
These are created during application startup or via EF migrations.
Security Best Practices in MVC Membership
1) Password are never stored in plain text – they’re hashed using a secure hashing algorithm (e.g.,
PBKDF2).
2) Use SSL (HTTPS) to encrypt user data during transmission.
3) Always use the built-in Identity features for token-based operations like password resets or
email confirmation.
4) Avoid writing custom login logic unless absolutely necessary.
External Authentication Providers
[Link] Identity in MVC can be integrated with third-party providers using OAuth/OpenID
Connect, such as:
• Google
• Facebook
• Microsoft
• Twitter
This is done using the OWIN middleware, which allows users to sign in with their existing
credentials from these platforms.
Example:
𝑎𝑝𝑝. 𝑈𝑠𝑒𝐺𝑜𝑜𝑔𝑙𝑒𝐴𝑢𝑡ℎ𝑒𝑛𝑡𝑖𝑐𝑎𝑡𝑖𝑜𝑛(𝑐𝑙𝑖𝑒𝑛𝑡𝐼𝑑: "𝑥𝑥𝑥", 𝑐𝑙𝑖𝑒𝑛𝑡𝑆𝑒𝑐𝑟𝑒𝑡: "𝑦𝑦𝑦");
Customizing Membership
You can:
• Extend the 𝐴𝑝𝑝𝑙𝑖𝑐𝑎𝑡𝑖𝑜𝑛𝑈𝑠𝑒𝑟 class to add custom fields
• Override password policies (e.g., minimum length, complexity)
• Customize login and registration UI
• Create custom authorization filters for fine control
Using Identity with Dependency Injection
[Link] Core MVC fully supports dependency injection, so services like 𝑈𝑠𝑒𝑟𝑀𝑎𝑛𝑎𝑔𝑒𝑟 and
𝑆𝑖𝑔𝑛𝐼𝑛𝑀𝑎𝑛𝑎𝑔𝑒𝑟 are injected into controllers:
By Daud Anjum 102
ENTERPRISE SYSTEMS SI-342
𝑝𝑟𝑖𝑣𝑎𝑡𝑒 𝑟𝑒𝑎𝑑𝑜𝑛𝑙𝑦 𝑈𝑠𝑒𝑟𝑀𝑎𝑛𝑎𝑔𝑒𝑟<𝐴𝑝𝑝𝑙𝑖𝑐𝑎𝑡𝑖𝑜𝑛𝑈𝑠𝑒𝑟> _𝑢𝑠𝑒𝑟𝑀𝑎𝑛𝑎𝑔𝑒𝑟;
𝑝𝑟𝑖𝑣𝑎𝑡𝑒 𝑟𝑒𝑎𝑑𝑜𝑛𝑙𝑦 𝑆𝑖𝑔𝑛𝐼𝑛𝑀𝑎𝑛𝑎𝑔𝑒𝑟<𝐴𝑝𝑝𝑙𝑖𝑐𝑎𝑡𝑖𝑜𝑛𝑈𝑠𝑒𝑟> _𝑠𝑖𝑔𝑛𝐼𝑛𝑀𝑎𝑛𝑎𝑔𝑒𝑟;
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑐𝑜𝑢𝑛𝑡𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟(𝑈𝑠𝑒𝑟𝑀𝑎𝑛𝑎𝑔𝑒𝑟<𝐴𝑝𝑝𝑙𝑖𝑐𝑎𝑡𝑖𝑜𝑛𝑈𝑠𝑒𝑟> 𝑢𝑠𝑒𝑟𝑀𝑎𝑛𝑎𝑔𝑒𝑟,
𝑆𝑖𝑔𝑛𝐼𝑛𝑀𝑎𝑛𝑎𝑔𝑒𝑟<𝐴𝑝𝑝𝑙𝑖𝑐𝑎𝑡𝑖𝑜𝑛𝑈𝑠𝑒𝑟> 𝑠𝑖𝑔𝑛𝐼𝑛𝑀𝑎𝑛𝑎𝑔𝑒𝑟)
{
_𝑢𝑠𝑒𝑟𝑀𝑎𝑛𝑎𝑔𝑒𝑟 = 𝑢𝑠𝑒𝑟𝑀𝑎𝑛𝑎𝑔𝑒𝑟;
_𝑠𝑖𝑔𝑛𝐼𝑛𝑀𝑎𝑛𝑎𝑔𝑒𝑟 = 𝑠𝑖𝑔𝑛𝐼𝑛𝑀𝑎𝑛𝑎𝑔𝑒𝑟;
}
This makes testing and maintenance easier.
Authorization and Security
In enterprise-grade web applications, authorization and security are essential pillars that protect
sensitive resources, enforce user roles, and guard against malicious attacks. [Link] MVC, as a
powerful web development framework, provides a structured and extensible approach to
implementing these security measures. The core aspects of security in [Link] MVC revolve
around authentication, authorization, role management, data protection, and defence against web
vulnerabilities.
Authorization in [Link] MVC
Authorization in MVC is commonly implemented using the [Authorize] attribute. It can be applied
at the controller or action method level to restrict access.
Basic Usage:
[𝐴𝑢𝑡ℎ𝑜𝑟𝑖𝑧𝑒]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐷𝑎𝑠ℎ𝑏𝑜𝑎𝑟𝑑( )
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤( );
}
Only authenticated users can access this action.
Role-Based Authorization:
[𝐴𝑢𝑡ℎ𝑜𝑟𝑖𝑧𝑒(𝑅𝑜𝑙𝑒𝑠 = "𝐴𝑑𝑚𝑖𝑛")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐴𝑑𝑚𝑖𝑛𝑃𝑎𝑛𝑒𝑙( )
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤( );
}
Only users in the “Admin” role can access this action.
Multiple Roles:
[𝐴𝑢𝑡ℎ𝑜𝑟𝑖𝑧𝑒(𝑅𝑜𝑙𝑒𝑠 = "𝐴𝑑𝑚𝑖𝑛, 𝑀𝑎𝑛𝑎𝑔𝑒𝑟")]
Users in either the “Admin” or “Manager” roles can access.
By Daud Anjum 103
ENTERPRISE SYSTEMS SI-342
Policy-Based Authorization ([Link] Core MVC)
In [Link] Core MVC (modern version), a more flexible and scalable policy-based authorization
model is used. It allows you to define complex rules and use them in place of roles.
Example:
𝑠𝑒𝑟𝑣𝑖𝑐𝑒𝑠. 𝐴𝑑𝑑𝐴𝑢𝑡ℎ𝑜𝑟𝑖𝑧𝑎𝑡𝑖𝑜𝑛(𝑜𝑝𝑡𝑖𝑜𝑛𝑠 =>
{
𝑜𝑝𝑡𝑖𝑜𝑛𝑠. 𝐴𝑑𝑑𝑃𝑜𝑙𝑖𝑐𝑦("𝐻𝑅𝑃𝑜𝑙𝑖𝑐𝑦", 𝑝𝑜𝑙𝑖𝑐𝑦 =>
𝑝𝑜𝑙𝑖𝑐𝑦. 𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝐶𝑙𝑎𝑖𝑚("𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡", "𝐻𝑅"));
});
Then apply:
[𝐴𝑢𝑡ℎ𝑜𝑟𝑖𝑧𝑒(𝑃𝑜𝑙𝑖𝑐𝑦 = "𝐻𝑅𝑃𝑜𝑙𝑖𝑐𝑦")]
Authentication Methods
[Link] MVC supports various authentication techniques:
• Forms Authentication (older version)
• [Link] Identity Authentication (standard for MVC 5+)
• OAuth/OpenID (external login providers)
• Token-Based Authentication (for APIs and SPAs)
With [Link] Identity, cookies are used to maintain authenticated sessions. In APIs, JWT (JSON
Web Tokens) are often used.
Secure Password Storage
[Link] Identity uses industry-standard algorithms (e.g., PBKDF2) to hash and salt passwords
before storing them. This ensures that even if the database is compromised, raw passwords aren't
exposed.
Data Protection
Sensitive data such as passwords, connection strings, or user tokens should never be stored in plain
text. [Link] provides tools like:
• Data Protection API (DPAPI)
• Encryption libraries
• Secure AppSettings / User Secrets ([Link] Core)
Preventing Common Security Threats
[Link] MVC includes mechanisms to mitigate several common web vulnerabilities:
1) Cross-Site Scripting (XSS)
Automatically encodes output in Razor views.
@𝑀𝑜𝑑𝑒𝑙. 𝑈𝑠𝑒𝑟𝑁𝑎𝑚𝑒 // 𝐻𝑇𝑀𝐿 𝑒𝑛𝑐𝑜𝑑𝑒𝑑
2) Cross-Site Request Forgery (CSRF)
Prevented using anti-forgery tokens in forms:
@𝐻𝑡𝑚𝑙. 𝐴𝑛𝑡𝑖𝐹𝑜𝑟𝑔𝑒𝑟𝑦𝑇𝑜𝑘𝑒𝑛( )
By Daud Anjum 104
ENTERPRISE SYSTEMS SI-342
3) SQL Injection
Prevented by using parameterized queries or EF LINQ queries.
𝑣𝑎𝑟 𝑢𝑠𝑒𝑟 = 𝑑𝑏. 𝑈𝑠𝑒𝑟𝑠. 𝐹𝑖𝑟𝑠𝑡𝑂𝑟𝐷𝑒𝑓𝑎𝑢𝑙𝑡(𝑢 => 𝑢. 𝐸𝑚𝑎𝑖𝑙 == 𝑒𝑚𝑎𝑖𝑙);
4) Insecure Direct Object Reference
Prevent access to internal IDs by validating ownership or access rights before serving data.
Transport Layer Security (TLS/SSL)
It is highly recommended to enforce HTTPS using 𝑤𝑒𝑏. 𝑐𝑜𝑛𝑓𝑖𝑔 settings or middleware in
[Link] Core to ensure encrypted communication between the client and server.
Enforcing HTTPS:
[𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝐻𝑡𝑡𝑝𝑠]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝑆𝑒𝑐𝑢𝑟𝑒𝑃𝑎𝑔𝑒( )
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝑉𝑖𝑒𝑤( );
}
Two-Factor Authentication (2FA)
[Link] Identity supports 2FA through:
• Email verification
• SMS code delivery
• Authenticator apps (e.g., Google Authenticator)
This adds an additional layer of security for user logins.
Security Best Practices in [Link] MVC
• Use the latest framework version to ensure all patches are included
• Enable account lockout after repeated failed login attempts
• Store secrets securely (e.g., not in plain 𝑤𝑒𝑏. 𝑐𝑜𝑛𝑓𝑖𝑔)
• Validate and sanitize all user input
• Keep EF and Identity libraries updated
11. Introduction to Service Oriented Architecture
Service-Oriented Architecture (SOA) is an architectural pattern used in software design that forces
on building applications by assembling loosely coupled, reusable, and interoperable services. Each
service in SOA is a self-contained unit of functionality that can be accessed remotely and
independently, typically over a network. SOA is especially well-suited for ES, where different
components and applications need to communicate across departments, platforms, or even
organizations.
By Daud Anjum 105
ENTERPRISE SYSTEMS SI-342
At its core, SOA promotes the idea of modularity and service abstraction. Instead of creating one
large monolithic application, the software is divided into smaller functional units or “services”
such as customer management, inventory, billing, etc. These services interact with each other
through well-defined interfaces using standard communication protocols, such as HTTP, SOAP,
or REST. This allows for flexibility in deployment, versioning, and scaling.
One of the primary goals of SOA is to facilitate integration between heterogeneous systems. In
large enterprises, it’s common to find a variety of systems built using different technologies (e.g.,
Java, .NET, Python, or legacy platforms like COBOL). SOA bridges this gap by enabling these
systems to communicate through services, without needing to rewrite or tightly couple them.
SOA also encourages reusability. A well-designed service, once created, can be used by multiple
applications or modules without modification. For instance, a “Payment Service” might be used
not only by a billing application but also be e-commerce, CRM, and accounting systems. This
redundance, accelerates development, and ensures consistency across platforms.
Another essential characteristic of SOA is loose coupling, which refers to the separation of service
logic from the consumers that use it. This means changes in one service (as long as the interface
remains the same) do not necessarily require changes in the consumers. This leads to systems that
are easier to maintain and evolve.
SOA also promotes discoverability and standardization. Services can be registered in a service
registry where other applications can discover them. This enables dynamic binding and runtime
discovery of services, which is useful in scalable, cloud-based, or distributed environments.
In terms of governance, SOA often centralized management of services, including security,
monitoring, and version control. This is crucial in enterprise settings where data security,
compliance, and performance monitoring are mandatory.
To implement SOA, various technologies and standards are commonly used. Some of the most
prominent include:
• Web Services (SOAP-based)
• RESTful services
• WSDL (Web Services Description Language)
• UDDI (Universal Description, Discovery, and Integration)
• XML and JSON for data exchange
SOAP
SOAP, or Simple Object Access Protocol, is a standardized protocol for exchanging structured
information in the implementation of web services within SOA. Developed by Microsoft and later
standardized by the W3C, SOAP plays a crucial role in enabling communication between
distributed and heterogeneous systems over a network, particularly the Internet.
By Daud Anjum 106
ENTERPRISE SYSTEMS SI-342
Purpose and Design
SOAP is designed to allow different applications – possibly built using different programming
languages and running on different platforms – to communicate with each other. It is based on
XML, which means that the entire communication message is encapsulated in a structured XML
document. This XML-based messaging format allows SOAP to be platform-independent and
language-agnostic.
The SOAP protocol is both extensible and neutral. Extensibility means that it can be extended with
additional features such as security, transactions, or routing. Neutrality implies that SOAP can
operate over any transport protocol such as HTTP, SMTP, FTP, or even TCP, though HTTP is the
most commonly used protocol for SOAP messages.
Structure of a SOAP Message
A typical SOAP message is made up of the following parts:
1) Envelope: This is the root element of a SOAP message. It defines the start and end of the
message and identifies it as a SOAP message.
2) Header (optional): Contains metadata or auxiliary information like authentication, transaction
management, or security tokens. It is intended for intermediaries and not necessarily the end
applications.
3) Body: This contains the actual SOAP message intended for the ultimate receiver. It holds the
request or response data and follows a strict XML schema.
4) Fault (optional): Used to carry error any status information if the service fails to process the
message.
Example SOAP Request
Here’s a simplified example of a SOAP request to a web service;
<𝑠𝑜𝑎𝑝:𝐸𝑛𝑣𝑒𝑙𝑜𝑝𝑒 𝑥𝑚𝑙𝑛𝑠:𝑠𝑜𝑎𝑝="ℎ𝑡𝑡𝑝://𝑤𝑤𝑤. 𝑤3. 𝑜𝑟𝑔/2003/05/𝑠𝑜𝑎𝑝-𝑒𝑛𝑣𝑒𝑙𝑜𝑝𝑒">
<𝑠𝑜𝑎𝑝:𝐻𝑒𝑎𝑑𝑒𝑟/>
<𝑠𝑜𝑎𝑝:𝐵𝑜𝑑𝑦>
<𝑚:𝐺𝑒𝑡𝑊𝑒𝑎𝑡ℎ𝑒𝑟 𝑥𝑚𝑙𝑛𝑠:𝑚="ℎ𝑡𝑡𝑝://𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚/𝑤𝑒𝑎𝑡ℎ𝑒𝑟">
<𝑚:𝐶𝑖𝑡𝑦>𝑁𝑒𝑤 𝑌𝑜𝑟𝑘 </𝑚:𝐶𝑖𝑡𝑦>
</𝑚:𝐺𝑒𝑡𝑊𝑒𝑎𝑡ℎ𝑒𝑟>
</𝑠𝑜𝑎𝑝:𝐵𝑜𝑑𝑦>
</𝑠𝑜𝑎𝑝:𝐸𝑛𝑣𝑒𝑙𝑜𝑝𝑒>
SOAP vs REST
SOAP is often compared to REST (Representational State Transfer), another web service protocol.
Unlike REST, which uses standard HTTP methods and is lightweight, SOAP is heavier and more
rigid due to its reliance on XML and extensive protocol rules. However, SOAP provides built-in
support for features like security (WS-Security), transactions, and messaging reliability, which
makes it more suitable for enterprise-level applications, especially in banking, insurance, and
government systems where standards compliance and data security are critical.
By Daud Anjum 107
ENTERPRISE SYSTEMS SI-342
Key Features of SOAP
• Platform and Language Independence: SOAP can be implemented in any programming
language and runs on any platform.
• Protocol Independence: SOAP can be transported over multiple protocols, although HTTP is
most common.
• Extensibility: Additional protocols and features can be layered via standards like WS-Security,
WS-Addressing, etc.
• Formal Contract via WSDL: SOAP services are described using WSDL, which provides a
machine-readable description of the service’s methods, inputs, and outputs.
Advantages of SOAP
1) Reliable message delivery through WS-ReliableMessaging
2) Strict standards and specifications ensure consistency
3) Rich support for tools and frameworks across enterprise environments
4) Strong security standards via WS-Security
WSDL
WSDL, short for Web Services Description Language, is an XML-based language used to describe
the functionalities offered by a web service. It serves as a contract between the web service
provider and consumer, specifying the interface, location, and methods of a web service in a
machine-readable format. WSDL is a W3C standard and plays a vital role in SOAP-based web
services within SOA.
Purpose and Importance
WSDL provides all the necessary details required for a client to interact with a web service. It tells
the client what operations the service provides, what parameters those operations require, and what
data types are used. This eliminates the need for human-readable documentation and enables
automated tools to generate code for accessing web services.
In SOA, where services may be scattered across different platforms and languages, WSDL acts as
a blueprint that standardizes communication and ensures that both the client and service provider
adhere to the agreed format.
Structure of a WSDL Document
A typical WSDL document consists of the following main elements:
1) Types: Defines the data types (usually in XML Schema Definition – XSD) used by the web
service. It helps in specifying complex data structures.
2) Message: Describes the input and output messages used by the service operations. Each
message can have one or more parts (parameters).
By Daud Anjum 108
ENTERPRISE SYSTEMS SI-342
3) PortType: Also called the interface, this element defines a collection of operations (similar to
methods in a class) supported by the service and the format of their messages.
4) Binding: Specifies the communication protocols and message formats used to access
operations defined in the PortType. It binds abstract definitions to actual protocol details (like
HTTP, SOAP).
5) Service: Contains the endpoint (URL or address) where the service is located. It maps the
bindings to a physical address.
6) Port: Defines a single endpoint for a specific binding.
Example WSDL Snippet
Here is a simplified example of a WSDL structure for a weather service:
< 𝑑𝑒𝑓𝑖𝑛𝑖𝑡𝑖𝑜𝑛𝑠 𝑛𝑎𝑚𝑒 = "𝑊𝑒𝑎𝑡ℎ𝑒𝑟𝑆𝑒𝑟𝑣𝑖𝑐𝑒"
𝑥𝑚𝑙𝑛𝑠 = "ℎ𝑡𝑡𝑝://𝑠𝑐ℎ𝑒𝑚𝑎𝑠. 𝑥𝑚𝑙𝑠𝑜𝑎𝑝. 𝑜𝑟𝑔/𝑤𝑠𝑑𝑙/"
𝑥𝑚𝑙𝑛𝑠: 𝑠𝑜𝑎𝑝 = "ℎ𝑡𝑡𝑝://𝑠𝑐ℎ𝑒𝑚𝑎𝑠. 𝑥𝑚𝑙𝑠𝑜𝑎𝑝. 𝑜𝑟𝑔/𝑤𝑠𝑑𝑙/𝑠𝑜𝑎𝑝/"
𝑥𝑚𝑙𝑛𝑠: 𝑡𝑛𝑠 = "ℎ𝑡𝑡𝑝://𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚/𝑤𝑒𝑎𝑡ℎ𝑒𝑟"
𝑡𝑎𝑟𝑔𝑒𝑡𝑁𝑎𝑚𝑒𝑠𝑝𝑎𝑐𝑒 = "ℎ𝑡𝑡𝑝://𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚/𝑤𝑒𝑎𝑡ℎ𝑒𝑟" >
< 𝑚𝑒𝑠𝑠𝑎𝑔𝑒 𝑛𝑎𝑚𝑒 = "𝐺𝑒𝑡𝑊𝑒𝑎𝑡ℎ𝑒𝑟𝑅𝑒𝑞𝑢𝑒𝑠𝑡" >
< 𝑝𝑎𝑟𝑡 𝑛𝑎𝑚𝑒 = "𝑐𝑖𝑡𝑦" 𝑡𝑦𝑝𝑒 = "𝑥𝑠𝑑: 𝑠𝑡𝑟𝑖𝑛𝑔"/>
</𝑚𝑒𝑠𝑠𝑎𝑔𝑒 >
< 𝑚𝑒𝑠𝑠𝑎𝑔𝑒 𝑛𝑎𝑚𝑒 = "𝐺𝑒𝑡𝑊𝑒𝑎𝑡ℎ𝑒𝑟𝑅𝑒𝑠𝑝𝑜𝑛𝑠𝑒" >
< 𝑝𝑎𝑟𝑡 𝑛𝑎𝑚𝑒 = "𝑡𝑒𝑚𝑝𝑒𝑟𝑎𝑡𝑢𝑟𝑒" 𝑡𝑦𝑝𝑒 = "𝑥𝑠𝑑: 𝑠𝑡𝑟𝑖𝑛𝑔"/>
</𝑚𝑒𝑠𝑠𝑎𝑔𝑒 >
< 𝑝𝑜𝑟𝑡𝑇𝑦𝑝𝑒𝑠 𝑛𝑎𝑚𝑒 = "𝑊𝑒𝑎𝑡ℎ𝑒𝑟𝑃𝑜𝑟𝑡𝑇𝑦𝑝𝑒" >
< 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛 𝑛𝑎𝑚𝑒 = "𝐺𝑒𝑡𝑊𝑒𝑎𝑡ℎ𝑒𝑟" >
< 𝑖𝑛𝑝𝑢𝑡 𝑚𝑒𝑠𝑠𝑎𝑔𝑒 = "𝑡𝑛𝑠: 𝐺𝑒𝑡𝑊𝑒𝑎𝑡ℎ𝑒𝑟𝑅𝑒𝑞𝑢𝑒𝑠𝑡"/>
< 𝑜𝑢𝑡𝑝𝑢𝑡 𝑚𝑒𝑠𝑠𝑎𝑔𝑒 = "𝑡𝑛𝑠: 𝐺𝑒𝑡𝑊𝑒𝑎𝑡ℎ𝑒𝑟𝑅𝑒𝑠𝑝𝑜𝑛𝑠𝑒"/>
</𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛 >
</𝑝𝑜𝑟𝑡𝑇𝑦𝑝𝑒 >
< 𝑏𝑖𝑛𝑑𝑖𝑛𝑔 𝑛𝑎𝑚𝑒 = "𝑊𝑒𝑎𝑡ℎ𝑒𝑟𝐵𝑖𝑛𝑑𝑖𝑛𝑔" 𝑡𝑦𝑝𝑒 = "𝑡𝑛𝑠: 𝑊𝑒𝑎𝑡ℎ𝑒𝑟𝑃𝑜𝑟𝑡𝑇𝑦𝑝𝑒" >
< 𝑠𝑜𝑎𝑝: 𝑏𝑖𝑛𝑑𝑖𝑛𝑔 𝑠𝑡𝑦𝑙𝑒 = "𝑟𝑝𝑐" 𝑡𝑟𝑎𝑛𝑠𝑝𝑜𝑟𝑡 = "ℎ𝑡𝑡𝑝://𝑠𝑐ℎ𝑒𝑚𝑎𝑠. 𝑥𝑚𝑙𝑠𝑜𝑎𝑝. 𝑜𝑟𝑔/
𝑠𝑜𝑎𝑝/ℎ𝑡𝑡𝑝"/>
< 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛 𝑛𝑎𝑚𝑒 = "𝐺𝑒𝑡𝑊𝑒𝑎𝑡ℎ𝑒𝑟" >
< 𝑠𝑜𝑎𝑝: 𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛 𝑠𝑜𝑎𝑝𝐴𝑐𝑡𝑖𝑜𝑛 = "𝐺𝑒𝑡𝑊𝑒𝑎𝑡ℎ𝑒𝑟"/>
< 𝑖𝑛𝑝𝑢𝑡 >
< 𝑠𝑜𝑎𝑝: 𝑏𝑜𝑑𝑦 𝑢𝑠𝑒 = "𝑙𝑖𝑡𝑒𝑟𝑎𝑙"/>
</𝑖𝑛𝑝𝑢𝑡 >
< 𝑜𝑢𝑡𝑝𝑢𝑡 >
< 𝑠𝑜𝑎𝑝: 𝑏𝑜𝑑𝑦 𝑢𝑠𝑒 = "𝑙𝑖𝑡𝑒𝑟𝑎𝑙"/>
</𝑜𝑢𝑡𝑝𝑢𝑡 >
</𝑜𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛 >
</𝑏𝑖𝑛𝑑𝑖𝑛𝑔 >
By Daud Anjum 109
ENTERPRISE SYSTEMS SI-342
< 𝑠𝑒𝑟𝑣𝑖𝑐𝑒 𝑛𝑎𝑚𝑒 = "𝑊𝑒𝑎𝑡ℎ𝑒𝑟𝑆𝑒𝑟𝑣𝑖𝑐𝑒" >
< 𝑝𝑜𝑟𝑡 𝑛𝑎𝑚𝑒 = "𝑊𝑒𝑎𝑡ℎ𝑒𝑟𝑃𝑜𝑟𝑡" 𝑏𝑖𝑛𝑑𝑖𝑛𝑔 = "𝑡𝑛𝑠: 𝑊𝑒𝑎𝑡ℎ𝑒𝑟𝐵𝑖𝑛𝑑𝑖𝑛𝑔" >
< 𝑠𝑜𝑎𝑝: 𝑎𝑑𝑑𝑟𝑒𝑠𝑠 𝑙𝑜𝑐𝑎𝑡𝑖𝑜𝑛 = "ℎ𝑡𝑡𝑝://𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚/𝑤𝑒𝑎𝑡ℎ𝑒𝑟𝑆𝑒𝑟𝑣𝑖𝑐𝑒"/>
</𝑝𝑜𝑟𝑡 >
</𝑠𝑒𝑟𝑣𝑖𝑐𝑒 >
</𝑑𝑒𝑓𝑖𝑛𝑖𝑡𝑖𝑜𝑛𝑠 >
WSDL and Tools
WSDL files are typically used by development tools to auto-generate client-side code. For
instance, tools like:
• wsimport (Java)
• Add Service Reference (Visual Studio - .NET)
• SoapUI for testing
These tools parse the WSDL file and produce the necessary stubs or proxy classes that allow client
applications to interact with the web service easily.
Benefits of WSDL
• Automation: Enables code generation tools to build client stubs automatically.
• Interoperability: Promotes communication between services built on different platforms or
languages.
• Self-Describing: Acts as both documentation and specification.
• Standardization: Based on open standards (W3C), ensuring wide support.
Service Contract
A service contract is a formal and well-defined agreement between a service provider and a service
consumer. It outlines what the service does, how it can be accessed, what inputs it expects, and
what outputs it returns. Much like a legal contract, it defines the rules and expectations for both
sides involved in communication. This contract is essential to ensuring interoperability, loose
coupling, and discoverability in distributed systems.
What is a Service Contract?
A service contract describes the interface of a service in technical terms. It includes:
• Service name and description
• Operations (methods) the service exposes
• Input and output parameters for each operation
• Data types used (often XML or JSON)
• Communication protocols (e.g., HTTP, SOAP)
By Daud Anjum 110
ENTERPRISE SYSTEMS SI-342
• Security requirements (authentication, authorization)
• Message formats and fault handling mechanisms
The contract is typically defined using machine-readable metadata like WSDL for SOAP-based
services or OpenAPI/Swagger for RESTful services.
Importance of Service Contract
In SOA, systems are often distributed, platform-independent, and loosely coupled. Service
contracts make this possible by acting as the common point of understanding between different
systems.
• Standardization: Contracts define services in a standardized format, enabling different systems
to understand and interact with them without tight integration.
• Loose Coupling: The consumer does not need to know how to service is implemented, only
what the contract specifies.
• Autonomy and Interoperability: Systems developed in different programming languages or
hosted on different platforms can communicate effectively using contracts.
• Version Control: Contracts enable versioning so that older systems can continue to interact
with older versions of services while newer clients can use updated versions.
Types of Contracts in SOA
While “service contract” is the general term, contracts can be further broken down into specialized
categories:
1) Data Contract
Defines the data types and structures exchanged between the service and client. In .NET WCF,
[𝐷𝑎𝑡𝑎𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡] and [𝐷𝑎𝑡𝑎𝑀𝑒𝑚𝑏𝑒𝑟] attributes are used to define these structures.
2) Message Contract
Specifies how the message is structured at a lower level, including headers, body, and message
formats.
3) Operation Contract
Declares a specific operation (method) that the service provides. In .NET WCF, this is
represented using the [𝑂𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡] attribute.
4) Fault Contract
Describes the faults or exceptions that the service may return during execution.
These components work together to define the full behaviour and expectations of the service.
Service Contract Example (WCF in .NET)
In a .NET WCF (Windows Communication Foundation) service, the service contract can be
defined like this:
By Daud Anjum 111
ENTERPRISE SYSTEMS SI-342
[𝑆𝑒𝑟𝑣𝑖𝑐𝑒𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡𝑒𝑟𝑓𝑎𝑐𝑒 𝐼𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑜𝑟𝑆𝑒𝑟𝑣𝑖𝑐𝑒
{
[𝑂𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡]
𝑖𝑛𝑡 𝐴𝑑𝑑 (𝑖𝑛𝑡 𝑎, 𝑖𝑛𝑡 𝑏);
[𝑂𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡]
𝑖𝑛𝑡 𝑆𝑢𝑏𝑡𝑟𝑎𝑐𝑡 (𝑖𝑛𝑡 𝑎, 𝑖𝑛𝑡 𝑏);
}
In this example:
• The [ServiceContract] attribute defines that this interface is a service.
• The [OperationContract] attributes specify which methods are exposed to clients.
The WSDL for this service would be automatically generated to represent the contract in XML.
Benefits of Using Service Contracts
• Improved maintainability: Since consumers rely on the contract, the provider can change
internal implementation without affecting clients.
• Enhanced security: Contracts can define security policies and protocols that must be followed.
• Reusability and discoverability: Services with clear contracts can be reused across multiple
applications or teams.
• Versioning and compatibility: Contracts support multiple versions for backward compatibility.
Data Contract
In the context of SOA and particularly in WCF or .NET services, a Data Contract is a formal
agreement between a service and a client that defines how data will be structured, serialized, and
exchanged during communication. It plays a critical role in ensuring that the data passed between
systems is consistent, predictable, and correctly interpreted.
What is a Data Contract?
A Data Contract defines the structure and format of the data types used in the communication
between a service and its clients. It specifies:
• What data is sent
• How it is organized
• How it should be serialized and deserialized
Serialization is the process of converting an object into a format that can be transported over a
network (typically XML or JSON), and deserialization is the reverse process. A data contract
ensures that both the sender and the receiver agree on the format of this data.
By Daud Anjum 112
ENTERPRISE SYSTEMS SI-342
Why is a Data Contract Needed?
In distributed systems where different services may run on different platforms or be written in
different programming languages, it's important to have a standardized way of defining data. The
data contract ensures:
• Interoperability between diverse systems
• Consistency in data formats
• Validation of data structures
• Documentation of data expectations for developers
Without a clearly defined data contract, the client might misinterpret the service's response or send
data that the service cannot process.
Data Contracts in .NET / WCF
In .NET, the concept of a data contract is implemented using the [𝐷𝑎𝑡𝑎𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡] can
[𝐷𝑎𝑡𝑎𝑀𝑒𝑚𝑏𝑒𝑟] attributes. These attributes are used to decorate classes and their fields or
properties that should be included in the serialization process.
Example:
[𝐷𝑎𝑡𝑎𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑆𝑡𝑢𝑑𝑒𝑛𝑡
{
[𝐷𝑎𝑡𝑎𝑀𝑒𝑚𝑏𝑒𝑟]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝐼𝐷 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
[𝐷𝑎𝑡𝑎𝑀𝑒𝑚𝑏𝑒𝑟]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑁𝑎𝑚𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
// 𝑁𝑜𝑡 𝑚𝑎𝑟𝑘𝑒𝑑 𝑎𝑠 𝐷𝑎𝑡𝑎𝑀𝑒𝑚𝑏𝑒𝑟 – 𝑡ℎ𝑖𝑠 𝑓𝑖𝑒𝑙𝑑 𝑤𝑖𝑙𝑙 𝑛𝑜𝑡 𝑏𝑒 𝑠𝑒𝑟𝑖𝑎𝑙𝑖𝑧𝑒𝑑
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝐴𝑔𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
}
In this example:
• The class 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 is marked with [𝐷𝑎𝑡𝑎𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡], indicating it will be serialized.
• Only properties marked with [𝐷𝑎𝑡𝑎𝑀𝑒𝑚𝑏𝑒𝑟] (𝐼𝐷 and 𝑁𝑎𝑚𝑒) will be included in the serialized
data.
• 𝐴𝑔𝑒 will be ignored unless explicitly marked.
This approach allows for control and flexibility over what gets sent and received in service calls.
Key Features of Data Contracts
1) Explicit Serialization Control: You choose which members are serialized and which are not.
2) Support for Complex Types: Data contracts can represent complex hierarchical data using
nested classes and collections.
3) Forward and Backward Compatibility: Fields can be added to or removed from the data
contract in newer versions without breaking older clients.
4) Strong Typing: .NET enforces data types, making the data contract safe and predictable.
By Daud Anjum 113
ENTERPRISE SYSTEMS SI-342
Data Contracts vs XML Schema
Although both data contracts and XML Schema define data formats, data contracts are code-first
and designed to be more natural to developers in object-oriented languages like C#. They are more
tightly integrated with the .NET serialization system. In contrast, XML Schema (XSD) is schema-
first, primarily used in traditional SOAP-based web services where XML format is strictly defined.
Versioning in Data Contracts
One powerful feature of data contracts is their ability to support versioning. This is useful when
the service evolves over time, and new members are added to the data structure.
For example, a service might start by sending only a 𝑁𝑎𝑚𝑒, but later add a 𝑃ℎ𝑜𝑛𝑒𝑁𝑢𝑚𝑏𝑒𝑟. If
properly designed, older clients will simply ignore 𝑃ℎ𝑜𝑛𝑒𝑁𝑢𝑚𝑏𝑒𝑟, while newer clients can take
advantage of it.
To support versioning, developers must:
• Avoid removing existing [𝐷𝑎𝑡𝑎𝑀𝑒𝑚𝑏𝑒𝑟] fields.
• Provide default values for new fields.
• Use the 𝐼𝑠𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝑑 property to specify optional or mandatory members.
Data Contract Attributes
• [𝐷𝑎𝑡𝑎𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡]: Declares the class or struct as a data contract
• [𝐷𝑎𝑡𝑎𝑀𝑒𝑚𝑏𝑒𝑟]: Marks specific properties or fields for serialization
• 𝐼𝑠𝑅𝑒𝑞𝑢𝑖𝑟𝑒𝑑: Specifies whether the member is mandatory
• 𝑂𝑟𝑑𝑒𝑟: Sets the serialization order of members
• 𝑁𝑎𝑚𝑒 and 𝑁𝑎𝑚𝑒𝑠𝑝𝑎𝑐𝑒: Customize the output format
When Not to Use Data Contracts
While data contracts are essential in WCF and other SOAP services, they are not commonly used
in RESTful services or Web APIs. In REST, data is usually transmitted using JSON and standard
C# classes without attributes, relying on frameworks like [Link] Web API or [Link] Core
for automatic serialization.
XML
XML (Extensible Markup Language) is a markup language designed to store and transport data in
a structured and human-readable format. It is platform-independent and language-neutral, which
makes it an ideal medium for communication between systems in SOA and other distributed
computing environments. XML is one of the foundational technologies enabling interoperability
between heterogeneous systems, including web services, databases, and enterprise applications.
By Daud Anjum 114
ENTERPRISE SYSTEMS SI-342
Role of XML in SOA
In SOA, different services may be developed in different programming languages and may run on
different platforms. To allow these services to communicate reliably, a common format is required
for data exchange, and XML serves that purpose.
For example:
• SOAP messages are formatted using XML.
• WSDL documents that describe web services are written in XML.
• Configuration files and data payloads in many enterprise systems use XML.
XML ensures that the structure and semantics of the data remain consistent and interpretable
across all services, regardless of the underlying technologies.
Basic Structure of XML
XML documents are made up of elements enclosed in tags, similar to HTML, but with stricter
rules. Here’s a simple XML example:
< 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 >
< 𝐼𝐷 > 101 </𝐼𝐷 >
< 𝑁𝑎𝑚𝑒 > 𝐴𝑖𝑠ℎ𝑎 </𝑁𝑎𝑚𝑒 >
< 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 > 𝐶𝑜𝑚𝑝𝑢𝑡𝑒𝑟 𝑆𝑐𝑖𝑒𝑛𝑐𝑒 </𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 >
</𝑆𝑡𝑢𝑑𝑒𝑛𝑡 >
In this XML:
• < 𝑆𝑡𝑢𝑑𝑒𝑛𝑡 > is the root element
• < 𝐼𝐷 >, < 𝑁𝑎𝑚𝑒 >, and < 𝐷𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 > are child elements
• Values like 101 and 𝐴𝑖𝑠ℎ𝑎 are element content
Key Features of XML
1) Self-Descriptive
XML tags describe the data they contain. For example, < 𝑁𝑎𝑚𝑒 > 𝐴𝑙𝑖 </𝑁𝑎𝑚𝑒 > clearly
indicates that “Ali” is a name.
2) Hierarchical Structure
XML supports a tree-like structure with nested elements, making it easy to represent complex
data relationships.
3) Platform and Language Independence
XML is plain text, so it can be created, parsed, and manipulated by virtually any programming
language or platform.
4) Validation via DTD or XSD
XML documents can be validated for correctness using:
• DTD (Document Type Definition)
• XSD (XML Schema Definition)
5) Support for Unicode
XML supports all characters in the Unicode standard, enabling globalized applications and
systems to exchange data in multiple languages.
By Daud Anjum 115
ENTERPRISE SYSTEMS SI-342
6) Extensibility
As the name implies, XML is “extensible”. Developers can define their own tags and structures
based on applications needs.
Well-Formed vs. Valid XML
• Well-formed XML follows the basic syntax rules: proper nesting of tags, one root element,
case sensitivity, etc.
• Valid XML is well-formed and conforms to a schema or DTD, ensuring the document meets
specific structural requirements.
XML Parsing
There are two main ways to read XML in software applications:
• DOM (Document Object Model)
Loads the entire XML document into memory and represents it as a tree. Suitable for smaller
documents.
• SAX (Simple API for XML)
Parses XML sequentially without loading the whole document into memory. Better for large
datasets.
Most modern programming languages, including C#, Java, and Python, provide robust libraries to
parse and manipulate XML data.
Benefits of Using XML in Enterprise Systems
• Standardization: XML is a W3C standard, ensuring long-term compatibility and support.
• Data Integrity: Well-defined structure and schema validation ensure reliable data transmission.
• Loose Coupling: Systems communicating through XML do not need to know about each
other's internal structures.
• Security Integration: XML works with security standards such as XML Encryption and XML
Signature to ensure confidentiality and authenticity.
WCF Bindings
In Windows Communication Foundation (WCF), a binding defines how a WCF service
communicates with the outside world. It specifies the transport protocol, encoding method, and
security mechanisms used for communication between the client and the service. Think of
bindings as the “communication rules” that ensure both the service provider and the consumer
understand each other.
Bindings are a critical part of WCF’s configuration-driven architecture and are especially
important in SOA, where services need to be loosely coupled, interoperable, and flexible in how
they communicate.
By Daud Anjum 116
ENTERPRISE SYSTEMS SI-342
Purpose of Bindings in WCF
Bindings determine the following:
• How the message is transported (e.g., HTTP, TCP, Named Pipes)
• How the message is encoded (e.g., text, binary, MTOM)
• What security mechanisms are used (e.g., SSL, Windows authentication)
• Transaction flow and reliability
The idea is to decouple the service logic from communication details so that the sam eservice logic
can support multiple endpoints with different bindings.
Structure of a Binding
A WCF binding is made up of a stack of binding elements, where each element adds a specific
capability (like security, reliability, or transport). These layers are:
1) Transport Layer – Specifies the transport protocol (e.g., HTTP, TCP)
2) Encoding Layer – Specifies how messages are encoded (e.g., text, binary)
3) Protocol Layer – Adds features like security, reliability, and transactions
Types of Built-in WCF Bindings
WCF provides several predefined bindings for common scenarios. Below are the most commonly
used ones:
1) BasicHttpBinding
It is designed for interoperability with ASMX web services and other platforms. It uses SOAP
1.1 over HTTP/HTTPS. Text encoding in by default but no advanced features like reliable
messaging or transactions are available.
Use Case: When communication with legacy systems or non-WCF clients is required.
2) WSHttpBinding
It uses SOAP 1.2 over HTTP/HTTPS. It supports WS-* specifications like WS-Security, WS-
ReliableMessaging, and WS-AtomicTransaction. It is more secure and reliable than
BasicHttpBinding.
Use Case: For secure, interoperable enterprise applications that need richer features.
3) NetTcpBinding
It uses the TCP protocol for communication. It uses binary encoding for efficient performance.
It provides high throughput and supports reliability, transactions, and security.
Use Case: For intranet WCF-to-WCF communication where both client and service are .NET-
based.
4) NetNamedPipeBinding
It uses Named Pipes for communication on the same machine. It provides high performance
and low overhead. It is secure by default but only usable for local inter-process communication
(IPC).
Use Case: For communication between WCF services and clients on the same computer.
By Daud Anjum 117
ENTERPRISE SYSTEMS SI-342
5) NetMsmqBinding
It uses Microsoft Message Queuing (MSMQ). It supports queued messaging – useful when the
service does not need to be online at the same time as the client. It ensures delivery even in
disconnected or unreliable networks.
Use Case: For asynchronous, durable messaging scenarios.
6) WSDualHttpBinding
It allows for two-way communication using HTTP. It enables callbacks from service to client.
Use Case: When duplex communication is needed between client and server (e.g., in chat
applications).
7) WebHttpBinding
It is designed for RESTful services using HTTP verbs (GET, POST, PUT, DELETE). Messages
are formatted in JSON or XML, not SOAP in this binding. It is lightweight and suited for
mobile/web clients.
Use Case: For creating REST APIs with WCF.
Custom Bindings
While WCF provides several predefined bindings, you can also create a custom binding by
combining specific binding elements. This is useful when none of the predefined bindings meets
all the communication requirements of your application.
A custom binding allows full control over:
• Transport (HTTP, TCP, MSMQ, etc.)
• Encoding (Text, Binary, MTOM)
• Protocols (Security, Transactions, Reliable Messaging)
Example use case: A service that requires a custom combination of binary encoding over HTTPS
with message-level security.
Binding Configuration in WCF
Bindings can be configured either:
• Declaratively in a configuration file (usually 𝑤𝑒𝑏. 𝑐𝑜𝑛𝑓𝑖𝑔 or 𝑎𝑝𝑝. 𝑐𝑜𝑛𝑓𝑖𝑔)
• Programmatically in code using C#
Example (Declarative Configuration):
< 𝑏𝑖𝑛𝑑𝑖𝑛𝑔 >
< 𝑏𝑎𝑠𝑖𝑐𝐻𝑡𝑡𝑝𝐵𝑖𝑛𝑑𝑖𝑛𝑔 >
< 𝑏𝑖𝑛𝑑𝑖𝑛𝑔 𝑛𝑎𝑚𝑒 = "𝑏𝑎𝑠𝑖𝑐𝐻𝑡𝑡𝑝" 𝑚𝑎𝑥𝐵𝑢𝑓𝑓𝑒𝑟𝑆𝑖𝑧𝑒 =
"65536" 𝑚𝑎𝑥𝑅𝑒𝑐𝑒𝑖𝑣𝑒𝑀𝑒𝑠𝑠𝑎𝑔𝑒𝑆𝑖𝑧𝑒 = "65536"/>
</𝑏𝑎𝑠𝑖𝑐𝐻𝑡𝑡𝑝𝐵𝑖𝑛𝑑𝑖𝑛𝑔 >
</𝑏𝑖𝑛𝑑𝑖𝑛𝑔𝑠 >
By Daud Anjum 118
ENTERPRISE SYSTEMS SI-342
Choosing the Right Binding
Binding Protocol Encoding Best For
BasicHttpBinding HTTP Text Simple
interoperability with
old systems
WSHttpBinding HTTP Text Secure, reliable
enterprise
communication
NetTcpBinding TCP Binary Fast intranet
Communication
NetNamedPipeBinding Named Pipes Binary On-machine
processes
NetMsmqBinding MSMQ Binary Queued, offline
messaging
WebHttpBinding HTTP JSON/XML RESTful web
services
ABC of WCF
In WCF, the “ABC” stands for the three core components that define every WCF service:
• A – Address
• B – Binding
• C – Contract
Together, these three elements define how, where, and what a WCF service communicates.
Understanding the ABCs is essential for configuring, hosting, and consuming WCF services
effectively.
A – Address (Where the Service is Located)
The Address in WCF specifies where the service can be found. It is a URL that uniquely identifies
the service’s location on the network. Clients use this address to send messages to the service.
An address can take many forms, depending on the transport protocol being used. For example:
• HTTP: ℎ𝑡𝑡𝑝://𝑙𝑜𝑐𝑎𝑙ℎ𝑜𝑠𝑡: 8080/𝑀𝑦𝑆𝑒𝑟𝑣𝑖𝑐𝑒
• TCP: 𝑛𝑒𝑡. 𝑡𝑐𝑝://𝑙𝑜𝑐𝑎𝑙ℎ𝑜𝑠𝑡: 8081/𝑀𝑦𝑆𝑒𝑟𝑣𝑖𝑐𝑒
• Named Pipes: 𝑛𝑒𝑡. 𝑝𝑖𝑝𝑒://𝑙𝑜𝑐𝑎𝑙ℎ𝑜𝑠𝑡/𝑀𝑦𝑆𝑒𝑟𝑣𝑖𝑐𝑒
• MSMQ: 𝑛𝑒𝑡. 𝑚𝑠𝑚𝑞://𝑙𝑜𝑐𝑎𝑙ℎ𝑜𝑠𝑡/𝑝𝑟𝑖𝑣𝑎𝑡𝑒/𝑀𝑦𝑄𝑢𝑒𝑢𝑒
An address typically includes:
• Schema (e.g., ℎ𝑡𝑡𝑝, 𝑛𝑒𝑡. 𝑡𝑐𝑝)
• Hostname (e.g., 𝑙𝑜𝑐𝑎𝑙ℎ𝑜𝑠𝑡, 𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚)
• Port number
• Service path
By Daud Anjum 119
ENTERPRISE SYSTEMS SI-342
This enables WCF services to be hosted in various environments: IIS, self-hosting in console apps,
Windows services, etc.
B – Binding (How to Service Communicates)
The Binding defines how the communication will happen between the client and the service. It
specifies the details of the transport protocol (e.g., HTTP, TCP), encoding (e.g., text, binary), and
other communication aspects like:
• Security requirements
• Message patterns (e.g., request-reply, one-way, duplex)
• Transactions
• Reliability
WCF provides a range of built-in bindings, such as:
1) 𝐵𝑎𝑠𝑖𝑐𝐻𝑡𝑡𝑝𝐵𝑖𝑛𝑑𝑖𝑛𝑔 – for SOAP 1.1 interoperability
2) 𝑊𝑆𝐻𝑡𝑡𝑝𝐵𝑖𝑛𝑑𝑖𝑛𝑔 – for SOAP 1.2 with WS-* standards
3) 𝑁𝑒𝑡𝑇𝑐𝑝𝐵𝑖𝑛𝑑𝑖𝑛𝑔 – for high-performance intranet scenarios
4) 𝑊𝑒𝑏𝐻𝑡𝑡𝑝𝐵𝑖𝑛𝑑𝑖𝑛𝑔 – for RESTful services using JSON/XML
5) 𝑁𝑒𝑡𝑀𝑠𝑚𝑞𝐵𝑖𝑛𝑑𝑖𝑛𝑔 – for queued messaging with MSMQ
A binding is a collection of binding elements that can be combined to form a custom
communication stack. This modularity gives WCF great flexibility.
C – Contract (What the Service Does)
The Contract defines what operations the service performs. In WCF, a contract is typically an
interface decorated with WCF-specific attributes such as [𝑆𝑒𝑟𝑣𝑖𝑐𝑒𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡] and
[𝑂𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡].
There are different types of contracts in WCF:
• Service Contract – Defines the operations the service exposes.
• Data Contract – Defines the structure of the data exchanged.
• Message Contract – Allows full control over SOAP message format.
• Fault Contract – Describes error handling in services.
Example of a Service Contract:
[𝑆𝑒𝑟𝑣𝑖𝑐𝑒𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡𝑒𝑟𝑓𝑎𝑐𝑒 𝐼𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑜𝑟
{
[𝑂𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡]
𝑖𝑛𝑡 𝐴𝑑𝑑(𝑖𝑛𝑡 𝑎, 𝑖𝑛𝑡 𝑏);
[𝑂𝑝𝑒𝑟𝑎𝑡𝑖𝑜𝑛𝐶𝑜𝑛𝑡𝑟𝑎𝑐𝑡]
𝑖𝑛𝑡 𝑆𝑢𝑏𝑡𝑟𝑎𝑐𝑡(𝑖𝑛𝑡 𝑎, 𝑖𝑛𝑡 𝑏);
}
This contract allows clients to understand what methods are available and how to call them.
By Daud Anjum 120
ENTERPRISE SYSTEMS SI-342
Putting It All Together: ABC in Action
To successfully communicate with a WCF service, the client must know:
• The Address to locate the service (A)
• The Binding to use for communication (B)
• The Contract to know what operations are supported (C)
In essence:
“Address tells you where the service is,
Binding tells you how to talk to it, and
Contract tells you what it does.”
Here’s how a WCF endpoint is defined in configuration:
< 𝑠𝑦𝑠𝑡𝑒𝑚. 𝑠𝑒𝑟𝑣𝑖𝑐𝑒𝑀𝑜𝑑𝑒𝑙 >
< 𝑠𝑒𝑟𝑣𝑖𝑐𝑒𝑠 >
< 𝑠𝑒𝑟𝑣𝑖𝑐𝑒 𝑛𝑎𝑚𝑒 = "𝑀𝑦𝐴𝑝𝑝. 𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑜𝑟𝑆𝑒𝑟𝑣𝑖𝑐𝑒" >
< 𝑒𝑛𝑑𝑝𝑜𝑖𝑛𝑡
𝑎𝑑𝑑𝑟𝑒𝑠𝑠 = "ℎ𝑡𝑡𝑝://𝑙𝑜𝑐𝑎𝑙ℎ𝑜𝑠𝑡: 8080/𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑜𝑟𝑆𝑒𝑟𝑣𝑖𝑐𝑒"
𝑏𝑖𝑛𝑑𝑖𝑛𝑔 = "𝑏𝑎𝑠𝑖𝑐𝐻𝑡𝑡𝑝𝐵𝑖𝑛𝑑𝑖𝑛𝑔"
𝑐𝑜𝑛𝑡𝑟𝑎𝑐𝑡 = "𝑀𝑦𝐴𝑝𝑝. 𝐼𝐶𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑜𝑟" />
</𝑠𝑒𝑟𝑣𝑖𝑐𝑒 >
</𝑠𝑒𝑟𝑣𝑖𝑐𝑒𝑠 >
</𝑠𝑦𝑠𝑡𝑒𝑚. 𝑠𝑒𝑟𝑣𝑖𝑐𝑒𝑀𝑜𝑑𝑒𝑙 >
This snippet shows a service hosted at a specific address, using a basic HTTP binding and
implementing a contract.
RESTful Services
RESTful Services (also called REST APIs) are a type of web service that follows the principles of
REST (Representational State Transfer) – an architectural style defined by Roy Fielding in his
doctoral dissertation in 2000. Unlike traditional SOAP-based services, RESTful services are
lightweight, stateless, and highly scalable, making them ideal for web and mobile applications.
REST is not a protocol but rather a set of architectural constraints. RESTful services use standard
HTTP methods (GET, POST, PUT, DELETE) to perform operations on resources, and they
typically return data in JSON or XML format.
Key Principles of REST
1) Statelessness
Each REST request from the client to the server must contain all the information needed to
understand and process the request. The server does not share anything about the latest client
request – no session state is retained.
By Daud Anjum 121
ENTERPRISE SYSTEMS SI-342
2) Client-Server Architecture
REST separates the UI (client) from the data storage (server), allowing each to evolve
independently.
3) Uniform Interface
RESTful APIs use a consistent, standardized way to interact with resources using URIs and
HTTP methods.
4) Resource-Based
Every piece of data is treated as a resource, identified by a URI. For example:
ℎ𝑡𝑡𝑝𝑠://𝑎𝑝𝑖. 𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚/𝑢𝑠𝑒𝑟𝑠/1 represets the user with ID 1.
5) Representation of Resources
Resources are represented using standard formats such as JSON or XML. Clients interact with
these representations.
6) Stateless Communication
No context is stored on the server between requests, making REST scalable and suitable for
cloud-based architectures.
HTTP Methods in REST
RESTful services use standard HTTP methods to perform CRUD operations:
HTTP Method Operation Description
GET Read Retrieve a resource or
collection
POST Create Add a new resource
PUT Update Update an existing resource
DELETE Delete Remove a resource
Benefits of RESTful Services
1) Simplicity
RESTful services use simple URIs and standard HTTP methods, making them easier to
understand and use than SOAP-based services.
2) Performance
REST can be cached using HTTP caching mechanisms, improving performance.
3) Scalability
Statelessness and a uniform interface allow REST services to scale better across distributed
systems.
4) Flexibility
REST supports multiple formats (JSON, XML, YAML), enabling integration with various
types of clients (web, mobile, desktop).
5) Lightweight
REST messages are smaller in size (especially when using JSON), reducing network
bandwidth usage.
By Daud Anjum 122
ENTERPRISE SYSTEMS SI-342
REST vs SOAP
Feature REST SOAP
Protocol Architectural Style Protocol
Format JSON, XML XML only
Lightweight Yes No
Flexibility High Low
Performance Fast Slower due to XML parsing
Standards No strict standards Strict standards
Best for Web/Mobile apps Enterprise-level secure apps
Typical RESTful Service Architecture
1) Client Application
Sends HTTP requests to the REST API (could be a web app, mobile app, or another service).
2) REST API
Receives the request, processes the logic, interacts with the database, and returns a response
(usually JSON).
3) Database
Stores data that the REST API interacts with.
Consuming REST services (CRUD operations) using jQuery AJAX and JSON
One of the key benefits of RESTful services is their ease of consumption by client-side
technologies. In modern web applications, jQuery is commonly used to make AJAX requests to
REST APIs and handle JSON data responses. This allows for seamless communication between
the front-end and back-end, enabling real-time updates and interactive web applications without
needing to reload the page.
By combining jQuery AJAX and JSON, developers can implement complete CRUD functionality
over REST APIs. This approach is widely used in dynamic applications, dashboards, admin panels,
and single-page applications (SPAs).
Understanding the Components
1) jQuery AJAX
AJAX allows web pages to send and receive data from servers asynchronously. jQuery
simplifies AJAX calls using the $. 𝑎𝑗𝑎𝑥( ) method or its shorthand methods like $. 𝑔𝑒𝑡( ),
$. 𝑝𝑜𝑠𝑡( ), $. 𝑝𝑢𝑡( ), and $. 𝑑𝑒𝑙𝑒𝑡𝑒( ).
2) JSON
JSON ia a lightweight, human-readable data format used to represent structured data. REST
APIs commonly use JSON to exchange data between the client and server because of its
simplicity and compatibility with JS.
By Daud Anjum 123
ENTERPRISE SYSTEMS SI-342
Performing CRUD Operations
Create (POST)
To create a new resource, you send a 𝑃𝑂𝑆𝑇 request using AJAX.
$. 𝑎𝑗𝑎𝑥({
𝑢𝑟𝑙: "ℎ𝑡𝑡𝑝𝑠://𝑎𝑝𝑖. 𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚/𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠",
𝑡𝑦𝑝𝑒: "𝑃𝑂𝑆𝑇",
𝑐𝑜𝑛𝑡𝑒𝑛𝑡𝑇𝑦𝑝𝑒: "𝑎𝑝𝑝𝑙𝑖𝑐𝑎𝑡𝑖𝑜𝑛/𝑗𝑠𝑜𝑛",
𝑑𝑎𝑡𝑎: 𝐽𝑆𝑂𝑁. 𝑠𝑡𝑟𝑖𝑛𝑔𝑖𝑓𝑦({ 𝑛𝑎𝑚𝑒: "𝐴𝑙𝑖", 𝑟𝑜𝑙𝑒: "𝐷𝑒𝑣𝑒𝑙𝑜𝑝𝑒𝑟" }),
𝑠𝑢𝑐𝑐𝑒𝑠𝑠: 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛(𝑟𝑒𝑠𝑝𝑜𝑛𝑠𝑒) {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔("𝐶𝑟𝑒𝑎𝑡𝑒𝑑: ", 𝑟𝑒𝑠𝑝𝑜𝑛𝑠𝑒);
},
𝑒𝑟𝑟𝑜𝑟: 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 (𝑒𝑟𝑟𝑜𝑟) {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑒𝑟𝑟𝑜𝑟("𝐸𝑟𝑟𝑜𝑟: ", 𝑒𝑟𝑟𝑜𝑟);
}
});
Read (GET)
To retrieve data (like a list of users), you use a 𝐺𝐸𝑇 request.
$. 𝑎𝑗𝑎𝑥({
𝑢𝑟𝑙: "ℎ𝑡𝑡𝑝𝑠://𝑎𝑝𝑖. 𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚/𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠",
𝑡𝑦𝑝𝑒: "𝐺𝐸𝑇",
𝑠𝑢𝑐𝑐𝑒𝑠𝑠: 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛(𝑑𝑎𝑡𝑎) {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔("𝐸𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠: ", 𝑑𝑎𝑡𝑎);
},
𝑒𝑟𝑟𝑜𝑟: 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 (𝑒𝑟𝑟𝑜𝑟) {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑒𝑟𝑟𝑜𝑟("𝐸𝑟𝑟𝑜𝑟: ", 𝑒𝑟𝑟𝑜𝑟);
}
});
Update (PUT)
To update a resource, you send a 𝑃𝑈𝑇 request with the updated data.
$. 𝑎𝑗𝑎𝑥({
𝑢𝑟𝑙: "ℎ𝑡𝑡𝑝𝑠://𝑎𝑝𝑖. 𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚/𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠/1",
𝑡𝑦𝑝𝑒: "𝑃𝑈𝑇",
𝑐𝑜𝑛𝑡𝑒𝑛𝑡𝑇𝑦𝑝𝑒: "𝑎𝑝𝑝𝑙𝑖𝑐𝑎𝑡𝑖𝑜𝑛/𝑗𝑠𝑜𝑛",
𝑑𝑎𝑡𝑎: 𝐽𝑆𝑂𝑁. 𝑠𝑡𝑟𝑖𝑛𝑔𝑖𝑓𝑦({ 𝑛𝑎𝑚𝑒: "𝐴𝑙𝑖 𝑈𝑝𝑑𝑎𝑡𝑒𝑑", 𝑟𝑜𝑙𝑒: "𝑀𝑎𝑛𝑎𝑔𝑒𝑟" }),
𝑠𝑢𝑐𝑐𝑒𝑠𝑠: 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛(𝑟𝑒𝑠𝑝𝑜𝑛𝑠𝑒) {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔("𝑈𝑝𝑑𝑎𝑡𝑒𝑑: ", 𝑟𝑒𝑠𝑝𝑜𝑛𝑠𝑒);
},
𝑒𝑟𝑟𝑜𝑟: 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 (𝑒𝑟𝑟𝑜𝑟) {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑒𝑟𝑟𝑜𝑟("𝐸𝑟𝑟𝑜𝑟: ", 𝑒𝑟𝑟𝑜𝑟);
}
});
By Daud Anjum 124
ENTERPRISE SYSTEMS SI-342
Delete (DELETE)
To delete a resource, you send a 𝐷𝐸𝐿𝐸𝑇𝐸 request to the resource’s URI.
$. 𝑎𝑗𝑎𝑥({
𝑢𝑟𝑙: "ℎ𝑡𝑡𝑝𝑠://𝑎𝑝𝑖. 𝑒𝑥𝑎𝑚𝑝𝑙𝑒. 𝑐𝑜𝑚/𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒𝑠/1",
𝑡𝑦𝑝𝑒: "𝐷𝐸𝐿𝐸𝑇𝐸",
𝑠𝑢𝑐𝑐𝑒𝑠𝑠: 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛(𝑟𝑒𝑠𝑝𝑜𝑛𝑠𝑒) {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑙𝑜𝑔("𝐷𝑒𝑙𝑒𝑡𝑒𝑑: ", 𝑟𝑒𝑠𝑝𝑜𝑛𝑠𝑒);
},
𝑒𝑟𝑟𝑜𝑟: 𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 (𝑒𝑟𝑟𝑜𝑟) {
𝑐𝑜𝑛𝑠𝑜𝑙𝑒. 𝑒𝑟𝑟𝑜𝑟("𝐸𝑟𝑟𝑜𝑟: ", 𝑒𝑟𝑟𝑜𝑟);
}
});
Benefits of Using jQuery AJAX with REST and JSON
• Asynchronous Communication: Keeps the web page responsive by sending/receiving data in
the background.
• Reduced Server Load: Only data is exchanged, not entire pages.
• Simplified Code: jQuery’s syntax simplifies AJAX compared to raw JS.
• JSON Compatibility: JSON is easy to parse and generate in JS, making it ideal for front-end
development.
• Real-time Updates: CRUD operations can instantly reflect in the UI without full page reloads.
Security Considerations
When using jQuery AJAX:
1) Always validate input data on the server side
2) Implement proper authentication and authorization (e.g., using tokens)
3) Use HTTPS to encrypt data transmission
4) Protect against Cross-Site Request Forgery (CSRF) and Cross-Site Scripting (XSS)
Introduction of Web API
In the world of enterprise software and modern application development, seamless communication
between different software systems is essential. This is where Web APIs (Application
Programming Interfaces) play a crucial role. A Web API is a framework for building HTTP-based
services that can be accessed by a wide range of clients including browsers, mobile devices,
desktop applications, and other servers. It allows applications to expose their functionality over
the web using standard web protocols.
The primary purpose of a Web API is to allow client applications to interact with server-side
applications in a stateless, lightweight manner using HTTP methods like GET, POST, PUT,
DELETE, etc. This interaction is often built using RESTful principles, where resources are
identified using URIs and manipulated using standard HTTP verbs.
By Daud Anjum 125
ENTERPRISE SYSTEMS SI-342
What is [Link] Web API?
[Link] Web API is a framework provided by Microsoft that allows developers to build Web
APIs on top of the .NET platform. It is part of the broader [Link] ecosystem but is focused
specifically on enabling the development of RESTful services. It is ideal for exposing data and
services to a variety of clients like browsers, mobile apps, IoT devices, and more.
Unlike traditional [Link] MVC, which returns HTML views to be rendered in browsers, Web
API returns data in formats like JSON or XML, which is ideal for client-side applications that
consume and display data independently.
Key Features of Web API
1) RESTful Nature: [Link] Web API supports the development of RESTful services, which
are easy to use, scalable, and stateless.
2) HTTP-based Communication: Web APIs communicate over HTTP, making it accessible from
any client that supports HTTP, including JS in web browsers.
3) Format Flexibility: Responses can be automatically formatted in JSON or XML depending on
the client’s request headers.
4) Routing: Similar to [Link] MVC, Web API uses routing to map HTTP requests to controller
actions.
5) Model Binding and Validation: Web API supports automatic binding of HTTP request data to
.NET objects and supports validation using data annotations.
6) Security: Supports various authentication methods such as token-based authentication, OAuth,
and basic authentication.
Use Cases of Web API
• Mobile Application Backends: Mobile apps often consume data from a centralized Web API
for login, fetching user data, and other tasks.
• Single Page Applications (SPAs): Frameworks like Angular, React, and Vue communicate with
the backend via Web APIs.
• Third-party Integration: APIs allow external developers to integrate with services (e.g.,
payment gateways, shipping APIs).
• IoT and Smart Devices: Devices can send and receive data via REST APIs for real-time
monitoring and control.
Benefits of Using Web APIs
• Interoperability: APIs can be consumed by different types of clients regardless of their
technology stack.
• Scalability: Stateless nature of RESTful services makes them highly scalable.
• Ease of Development and Maintenance: Web APIs are modular and easy to test and debug.
• Extensibility: Can be easily extended or updated without breaking existing client integrations.
By Daud Anjum 126
ENTERPRISE SYSTEMS SI-342
Example of Web API using CRUD
A Web API CRUD example demonstrates how to perform Create, Read, Update, and Delete
operations over HTTP using a RESTful API. This example is usually built using [Link] Web
API and helps understand how client applications (e.g., front-end apps, mobile apps) can interact
with server-side databases via standard HTTP methods.
Sample Scenario: Managing Products
Let’s build a simple Web API that manages a list of products using CRUD operations. Each
product will have the following properties:
• 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐼𝑑 (int)
• 𝑁𝑎𝑚𝑒 (string)
• 𝑃𝑟𝑖𝑐𝑒 (decimal)
1) Model Class: 𝑃𝑟𝑜𝑑𝑢𝑐𝑡. 𝑐𝑠
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑃𝑟𝑜𝑑𝑢𝑐𝑡
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝑖𝑛𝑡 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐼𝑑 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑟𝑖𝑛𝑔 𝑁𝑎𝑚𝑒 {𝑔𝑒𝑡; 𝑠𝑒𝑡; }
𝑝𝑢𝑏𝑙𝑖𝑐 𝑑𝑒𝑐𝑖𝑚𝑎𝑙 𝑃𝑟𝑖𝑐𝑒 { 𝑔𝑒𝑡; 𝑠𝑒𝑡; }
}
2) In-Memory Data Store
We’ll use a static list to simulate a database:
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑎𝑡𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑆𝑡𝑜𝑟𝑒
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑎𝑡𝑖𝑐 𝐿𝑖𝑠𝑡<𝑃𝑟𝑜𝑑𝑢𝑐𝑡> 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠 = 𝑛𝑒𝑤 𝐿𝑖𝑠𝑡<𝑃𝑟𝑜𝑑𝑢𝑐𝑡>( )
{
𝑛𝑒𝑤 𝑃𝑟𝑜𝑑𝑢𝑐𝑡 { 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐼𝑑 = 1, 𝑁𝑎𝑚𝑒 = "𝐿𝑎𝑝𝑡𝑜𝑝", 𝑃𝑟𝑖𝑐𝑒 = 1200 },
𝑛𝑒𝑤 𝑃𝑟𝑜𝑑𝑢𝑐𝑡 { 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐼𝑑 = 2, 𝑁𝑎𝑚𝑒 = "𝑀𝑜𝑢𝑠𝑒", 𝑃𝑟𝑖𝑐𝑒 = 25 }
};
}
By Daud Anjum 127
ENTERPRISE SYSTEMS SI-342
3) API Controller 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑠𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟. 𝑐𝑠
[𝑅𝑜𝑢𝑡𝑒("𝑎𝑝𝑖/[𝑐𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟]")]
[𝐴𝑝𝑖𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟]
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟 ∶ 𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟𝐵𝑎𝑠𝑒
{
// 𝐺𝐸𝑇 𝑎𝑝𝑖/𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠
[𝐻𝑡𝑡𝑝𝐺𝑒𝑡]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡<𝐼𝐸𝑛𝑢𝑚𝑒𝑟𝑎𝑏𝑙𝑒<𝑃𝑟𝑜𝑑𝑢𝑐𝑡>> 𝐺𝑒𝑡( )
{
𝑟𝑒𝑡𝑢𝑟𝑛 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑆𝑡𝑜𝑟𝑒. 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠;
}
// 𝐺𝐸𝑇 𝑎𝑝𝑖/𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠/1
[𝐻𝑡𝑡𝑝𝐺𝑒𝑡("{𝑖𝑑}")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡<𝑃𝑟𝑜𝑑𝑢𝑐𝑡> 𝐺𝑒𝑡(𝑖𝑛𝑡 𝑖𝑑)
{
𝑣𝑎𝑟 𝑝𝑟𝑜𝑑𝑢𝑐𝑡 = 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑆𝑡𝑜𝑟𝑒. 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠. 𝐹𝑖𝑟𝑠𝑡𝑂𝑟𝐷𝑒𝑓𝑎𝑢𝑙𝑡 (𝑝 =>
𝑝. 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐼𝑑 == 𝑖𝑑 );
𝑖𝑓 (𝑝𝑟𝑜𝑑𝑢𝑐𝑡 == 𝑛𝑢𝑙𝑙) 𝑟𝑒𝑡𝑢𝑟𝑛 𝑁𝑜𝑡𝐹𝑜𝑢𝑛𝑑 ( );
𝑟𝑒𝑡𝑢𝑟𝑛 𝑝𝑟𝑜𝑑𝑢𝑐𝑡;
}
// 𝑃𝑂𝑆𝑇 𝑎𝑝𝑖/𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠
[𝐻𝑡𝑡𝑝𝑃𝑜𝑠𝑡]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡<𝑃𝑟𝑜𝑑𝑢𝑐𝑡> 𝑃𝑜𝑠𝑡([𝐹𝑟𝑜𝑚𝐵𝑜𝑑𝑦] 𝑃𝑟𝑜𝑑𝑢𝑐𝑡 𝑛𝑒𝑤𝑃𝑟𝑜𝑑𝑢𝑐𝑡)
{
𝑛𝑒𝑤𝑃𝑟𝑜𝑑𝑢𝑐𝑡. 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐼𝑑 = 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑆𝑡𝑜𝑟𝑒. 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠. 𝑀𝑎𝑥 (𝑝 =>
𝑝. 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐼𝑑 ) + 1;
𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑆𝑡𝑜𝑟𝑒. 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠. 𝐴𝑑𝑑 (𝑛𝑒𝑤𝑃𝑟𝑜𝑑𝑢𝑐𝑡 );
𝑟𝑒𝑡𝑢𝑟𝑛 𝐶𝑟𝑒𝑎𝑡𝑒𝐴𝑡𝐴𝑐𝑡𝑖𝑜𝑛 (𝑛𝑎𝑚𝑒𝑜𝑓(𝐺𝑒𝑡 ), 𝑛𝑒𝑤 { 𝑖𝑑 =
𝑛𝑒𝑤𝑃𝑟𝑜𝑑𝑢𝑐𝑡. 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐼𝑑 }, 𝑛𝑒𝑤𝑃𝑟𝑜𝑑𝑢𝑐𝑡 );
}
// 𝑃𝑈𝑇 𝑎𝑝𝑖/𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠/1
[𝐻𝑡𝑡𝑝𝑃𝑢𝑡("{𝑖𝑑}")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐼𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝑃𝑢𝑡(𝑖𝑛𝑡 𝑖𝑑, [𝐹𝑟𝑜𝑚𝐵𝑜𝑑𝑦] 𝑃𝑟𝑜𝑑𝑢𝑐𝑡 𝑢𝑝𝑑𝑎𝑡𝑒𝑑𝑃𝑟𝑜𝑑𝑢𝑐𝑡)
{
𝑣𝑎𝑟 𝑝𝑟𝑜𝑑𝑢𝑐𝑡 = 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑆𝑡𝑜𝑟𝑒. 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠. 𝐹𝑖𝑟𝑠𝑡𝑂𝑟𝐷𝑒𝑓𝑎𝑢𝑙𝑡 (𝑝 =>
𝑝. 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐼𝑑 == 𝑖𝑑 );
𝑖𝑓 (𝑝𝑟𝑜𝑑𝑢𝑐𝑡 == 𝑛𝑢𝑙𝑙) 𝑟𝑒𝑡𝑢𝑟𝑛 𝑁𝑜𝑡𝐹𝑜𝑢𝑛𝑑 ( );
𝑝𝑟𝑜𝑑𝑢𝑐𝑡. 𝑁𝑎𝑚𝑒 = 𝑢𝑝𝑑𝑎𝑡𝑒𝑑𝑃𝑟𝑜𝑑𝑢𝑐𝑡. 𝑁𝑎𝑚𝑒;
𝑝𝑟𝑜𝑑𝑢𝑐𝑡. 𝑃𝑟𝑖𝑐𝑒 = 𝑢𝑝𝑑𝑎𝑡𝑒𝑑𝑃𝑟𝑜𝑑𝑢𝑐𝑡. 𝑃𝑟𝑖𝑐𝑒;
𝑟𝑒𝑡𝑢𝑟𝑛 𝑁𝑜𝐶𝑜𝑛𝑡𝑒𝑛𝑡( );
}
By Daud Anjum 128
ENTERPRISE SYSTEMS SI-342
// 𝐷𝐸𝐿𝐸𝑇𝐸 𝑎𝑝𝑖/𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠/1
[𝐻𝑡𝑡𝑝𝐷𝑒𝑙𝑒𝑡𝑒("{𝑖𝑑}")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐼𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐷𝑒𝑙𝑒𝑡𝑒(𝑖𝑛𝑡 𝑖𝑑)
{
𝑣𝑎𝑟 𝑝𝑟𝑜𝑑𝑢𝑐𝑡 = 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑆𝑡𝑜𝑟𝑒. 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠. 𝐹𝑖𝑟𝑠𝑡𝑂𝑟𝐷𝑒𝑓𝑎𝑢𝑙𝑡 (𝑝 =>
𝑝. 𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐼𝑑 == 𝑖𝑑 );
𝑖𝑓 𝑝𝑟𝑜𝑑𝑢𝑐𝑡 == 𝑛𝑢𝑙𝑙) 𝑟𝑒𝑡𝑢𝑟𝑛 𝑁𝑜𝑡𝐹𝑜𝑢𝑛𝑑 ( );
(
𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑆𝑡𝑜𝑟𝑒. 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠. 𝑅𝑒𝑚𝑜𝑣𝑒(𝑝𝑟𝑜𝑑𝑢𝑐𝑡 );
𝑟𝑒𝑡𝑢𝑟𝑛 𝑁𝑜𝐶𝑜𝑛𝑡𝑒𝑛𝑡( );
}
}
Explanation
• The controller handles all HTTP requests for the /api/products route.
• Each action method corresponds to one of the CRUD operations.
• 𝐻𝑡𝑡𝑝𝐺𝑒𝑡 is used to retrieve data.
• 𝐻𝑡𝑡𝑝𝑃𝑜𝑠𝑡 is used to add a new product.
• 𝐻𝑡𝑡𝑝𝑃𝑢𝑡 updates an existing product.
• 𝐻𝑡𝑡𝑝𝐷𝑒𝑙𝑒𝑡𝑒 removes a product.
Testing the Web API
This API can be tested using:
• Postman or cURL for manual requests.
• A front-end app using jQuery AJAX or Fetch API.
• Unit tests in [Link] projects.
MVC Routing
MVC routing refers to the process by which incoming HTTP request are mapped to the appropriate
controller actions in an [Link] MVC applications, the routing system in MVC plays a vital role
in directly traffic to the correct endpoint based on the URL structure, without the need for
traditional physical file parts.
This approach promotes clean URLs, separation of concerns, and extensibility, which are all
important principles in enterprise-level, service-oriented architectures.
How Routing Works in MVC
At the core of the MVC routing system is the Route Table, which is configured during the
application startup. This table contains a collection of route definitions, each of which tells the
MVC framework how to match incoming URLs to controllers and actions.
By Daud Anjum 129
ENTERPRISE SYSTEMS SI-342
A typical route is defined in the 𝑅𝑜𝑢𝑡𝑒𝐶𝑜𝑛𝑓𝑖𝑔. 𝑐𝑠 file found in the 𝐴𝑝𝑝_𝑆𝑡𝑎𝑟𝑡 folder (in older
MVC applications):
𝑝𝑢𝑏𝑙𝑖𝑐 𝑐𝑙𝑎𝑠𝑠 𝑅𝑜𝑢𝑡𝑒𝐶𝑜𝑛𝑓𝑖𝑔
{
𝑝𝑢𝑏𝑙𝑖𝑐 𝑠𝑡𝑎𝑡𝑖𝑐 𝑣𝑜𝑖𝑑 𝑅𝑒𝑔𝑖𝑠𝑡𝑒𝑟𝑅𝑜𝑢𝑡𝑒(𝑅𝑜𝑢𝑡𝑒𝐶𝑜𝑙𝑙𝑒𝑐𝑡𝑖𝑜𝑛 𝑟𝑜𝑢𝑡𝑒𝑠)
{
𝑟𝑜𝑢𝑡𝑒𝑠. 𝐼𝑔𝑛𝑜𝑟𝑒𝑅𝑜𝑢𝑡𝑒("{𝑟𝑒𝑠𝑜𝑢𝑟𝑐𝑒}. 𝑎𝑥𝑑/{*𝑝𝑎𝑡ℎ𝐼𝑛𝑓𝑜}");
𝑟𝑜𝑢𝑡𝑒𝑠. 𝑀𝑎𝑝𝑅𝑜𝑢𝑡𝑒(
𝑛𝑎𝑚𝑒: "𝐷𝑒𝑓𝑎𝑢𝑙𝑡",
𝑢𝑟𝑙: "{𝑐𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟}/{𝑎𝑐𝑡𝑖𝑜𝑛}/{𝑖𝑑}",
𝑑𝑒𝑓𝑎𝑢𝑙𝑡𝑠 ∶ 𝑛𝑒𝑤 { 𝑐𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟 = "𝐻𝑜𝑚𝑒", 𝑎𝑐𝑡𝑖𝑜𝑛 = "𝐼𝑛𝑑𝑒𝑥", 𝑖𝑑 =
𝑈𝑟𝑙𝑃𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟. 𝑂𝑝𝑡𝑖𝑜𝑛𝑎𝑙 }
);
}
}
This example sets up a default route that maps any URL of the form /𝑐𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟/𝑎𝑐𝑡𝑖𝑜𝑛/𝑖𝑑 to
the appropriate controller and action method. If no controller or action is specified in the URL, the
default 𝐻𝑜𝑚𝑒𝐶𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟 and 𝐼𝑛𝑑𝑒𝑥 action is used.
Components of a Route
A route consists of:
• URL pattern: Defines the structure of the URL (e.g., {𝑐𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟}/{𝑎𝑐𝑡𝑖𝑜𝑛}/{𝑖𝑑})
• Defaults: Provides fallback values if parts of the URL are missing
• Constraints: Optionally restricts the values allowed (e.g., only numeric IDs)
• Data Tokens: Additional data passed along with the request
Custom Routes
You can define multiple custom routes to handle different patterns. For example:
𝑟𝑜𝑢𝑡𝑒𝑠. 𝑀𝑎𝑝𝑅𝑜𝑢𝑡𝑒(
𝑛𝑎𝑚𝑒: "𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝐷𝑒𝑡𝑎𝑖𝑙𝑠",
𝑢𝑟𝑙: "𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠/𝑑𝑒𝑡𝑎𝑖𝑙𝑠/{𝑖𝑑}",
𝑑𝑒𝑓𝑎𝑢𝑙𝑡𝑠: 𝑛𝑒𝑤 { 𝑐𝑜𝑛𝑡𝑟𝑜𝑙𝑙𝑒𝑟 = "𝑃𝑟𝑜𝑑𝑢𝑐𝑡𝑠", 𝑎𝑐𝑡𝑖𝑜𝑛 = "𝐷𝑒𝑡𝑎𝑖𝑙𝑠" }
);
Attribute Routing
In addition to convention-based routing (configured in 𝑅𝑜𝑢𝑡𝑒𝐶𝑜𝑛𝑓𝑖𝑔. 𝑐𝑠), attribute routing allows
you to define routes directly on controller actions using attributes:
[𝑅𝑜𝑢𝑡𝑒("𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑠/{𝑖𝑑}")]
𝑝𝑢𝑏𝑙𝑖𝑐 𝐴𝑐𝑡𝑖𝑜𝑛𝑅𝑒𝑠𝑢𝑙𝑡 𝐷𝑒𝑡𝑎𝑖𝑙𝑠(𝑖𝑛𝑡 𝑖𝑑)
{
// 𝑎𝑐𝑡𝑖𝑜𝑛 𝑐𝑜𝑑𝑒
}
By Daud Anjum 130
ENTERPRISE SYSTEMS SI-342
To use attribute routing, it must be enabled in the 𝑅𝑜𝑢𝑡𝑒𝐶𝑜𝑛𝑓𝑖𝑔. 𝑐𝑠:
𝑟𝑜𝑢𝑡𝑒𝑠. 𝑀𝑎𝑝𝑀𝑣𝑐𝐴𝑡𝑡𝑟𝑖𝑏𝑢𝑡𝑒𝑅𝑜𝑢𝑡𝑒𝑠 ( );
Role in Service Oriented Architecture
Routing in MVC makes it easier to expose RESTful services and integrate multiple services under
a unified URL structure. Clean, well-defined routes improve API design, usability, and
documentation. It also helps in creating service endpoints for various clients (web apps, mobile
apps, etc.) using a standard, predictable pattern.
By Daud Anjum 131