DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Data Engineering
  3. Databases
  4. C# Design Patterns in the Wild

C# Design Patterns in the Wild

Anders Abel user avatar by
Anders Abel
·
Jun. 12, 12 · Interview
Like (0)
Save
Tweet
Share
5.28K Views

Join the DZone community and get the full member experience.

Join For Free

Design patterns are often presented as standard solutions that can be used for commonly occurring problems. I think that it is equally important to be able to recognize patterns in existing code. Being able to identify a pattern helps you understand the code better. It also helps by raising the abstraction level. It can be used to describe the architecture of a system without having to describe each part in detail.

The architecture is Transaction Script based. The transaction scripts are located in the Logic namespace. The same classes are used both as Data Transfer Objects and as Query Objects. A custom Object-Relational Mapper is used for database access.


Without the patterns as a common basis for communication the same description would be considerably longer.

Let’s have a look at some patterns in C# development.

One of the most basic patterns is the Observer Pattern. In C# it is built into the language. A common task is to hook up an event handler, e.g. in an ASP.NET page.

protected void Page_Load(object sender, EventArgs e)
{
    SomeButton.Click += SomeButton_Click;
}
 
protected void SomeButton_Click(object sender, EventArgs e)
{
    Response.Redirect("AnotherPage.aspx");
}

In this code the button is the subject being observed by the page. The observer pattern is built into the language.

Another example in the .NET environment is the LINQ to SQL data context.

using(DBContext context = new DBContext())
{
    var query = from p in context.Persons
                where p.ID = personID
                select p;
 
    Person person = query.Single();
 
    person.Active = true;
 
    context.SubmitChanges();
}

In this short piece of code, three different patterns are present. The DBContext is a Repository. It is an object-oriented representation of the database model. To query from the database a query object is created. It is created using built in language support for queries through LINQ. The last pattern is Unit of Work, that keeps track of changed objects and writes them to the database. It is the DBContext that is the unit of work, so that class has dual roles – it is both a Repository and a Unit of Work. In real code this is quite common with the same class participating in different patterns, with different roles in each pattern.

Database Design

Published at DZone with permission of Anders Abel, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Integrate AWS Secrets Manager in Spring Boot Application
  • How To Best Use Java Records as DTOs in Spring Boot 3
  • Java REST API Frameworks
  • Detecting Network Anomalies Using Apache Spark

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: