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 Video Library
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
View Events Video Library
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
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • What Does Synchronization With Asyncio Look Like
  • Common Performance Management Mistakes
  • Single Responsibility Principle: The Most Important Rule in the Software World
  • How To Get Closer to Consistency in Microservice Architecture

Trending

  • New Free Tool From Contrast Security Makes API Security Testing Fast and Easy
  • Decoding the Differences: Continuous Integration, Delivery and Deployment
  • Continuous Integration vs. Continuous Deployment
  • Using Open Source for Data Integration and Automated Synchronizations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Building Microservices Through Event-Driven Architecture, Part 4: Repositories

Building Microservices Through Event-Driven Architecture, Part 4: Repositories

A developer and architect gives a tutorial on how to apply the principles of event-driven design to building out a microservices architecture.

Gora LEYE user avatar by
Gora LEYE
·
Dec. 04, 19 · Tutorial
Like (9)
Save
Tweet
Share
25.04K Views

Join the DZone community and get the full member experience.

Join For Free

construction workers

learn all about repositories.
you may also like: building microservices with event-driven architecture, part 1: application-specific business rules

during this journey, i will implement the command side of repositories. repositories belong to interface adapters of uncle bob clean architecture.

in this step, i will start implementing the command side of the infrastructure, i will focus on how to persist data.

i will use entityframeworkcore and sql server.

logcorner.edusync.speech.application.usecases use an ispeechrepository as a dependency, i think that the most obvious way is to start by implementing ispeechrepository, and to continue by implementing its dependencies, etc. since i know where i am going, i will proceed as follows:

  • unitofwork: i will use the unitofwork pattern, it will help me to treat aggregateroot as a unit for data changes.
  • repository: i will use a repository pattern, aggregateroots are the only objects my code loads from the repository.

let us create a generic repository irepository and apply aggregateroot constraints.

my irepository will look like this :

screenshot of code

t is an aggregateroot and identifier is the type of aggregateroot identifier: int, guid, etc.

ispeechrepository implements irepository<speech, guid> where speech is an aggregateroot and guid is the type of speech.id.

screenshot of code

it is not possible to create a repository for a non-aggregateroot entity: mediafile for example.

screenshot of code

iunitofwork.commit persist (save or update) the entire aggregate (aggregateroot and related entities).

unitofwork

let's start by testing iunitofwork, it will result in the implementation of unitofwork. the latter will need a class that inherits from dbcontext. ( databasecontext in my case).

test 1

when saving iunitofwork.commit should save aggregate root and dbcontext.savechanges called only once screenshot of code

implementation of unitofwork.

screenshot of code

let us create a databasecontext class that inherits from dbcontext.

screenshot of code

the final implementation of unitofwork first test.

screenshot of code

implement unitofwork.dispose.

test 2

when disposing of unitofwork. dispose of should be called only once. screenshot of code

let us implement unitofwork.dispose().

screenshot of code

at this stage, the solution compiles, all the tests pass and the code coverage of logcorner.edusync.speech.infrastructure is 100%.

screenshot test 3

verify that createasync can be called on repository and should trigger dbset.addasync. screenshot of code

the repository can only be instantiated with an aggregateroot, so let us create a class that inherits from aggregateroot<guid> for testing purposes.

screenshot of code

the final implementation of the repository will look like this.

screenshot of code test 4

verify that createasync can be called on speechrepository and fire repository.createasync only once.

the goal of this test is to implement speechrepository, so i verify that when speechrepository.createasync is called then repository.createasync is called only once.

screenshot of code

here is the final implementation of the speechrepository.

screenshot of code

the implementation of this section differs according to the orm used (ef, nhibernate, or others). for example, we can create classes specific to the repository (speechdao) and apply a mapping between speechdao and speech.

speechdao can be seen as a duplicate class (properties ) of the speech class of the domain.

knowing that ef allows me to do more simple, without creating repository-specific classes and then apply to map between duplicate classes, by providing an ientitytypeconfiguration interface.

i can use it to point domain objects to database tables without applying additional mapping between speechdao and speech like this:

screenshot of code screenshot of code

we can note here, how valueobjects are managed.

screenshot of code

the last thing you need to know is that efcore needs a parameterless constructor because it uses reflection to do its thing.

screenshot of code screenshot of code

if you do not want to update domain classes and introduces private parameterless contractors, you should create repository-specific classes and then apply them to the map between duplicate classes. this repository-specific classes should have parameterless contractors.

database

i design the sql server database using ssdt, it will help on my devops pipeline

screenshot import database screenshot mediafile screenshot configure

configure logcorner.edusync.speech.presentation to target the sql database.

screenshot of code

finally, finish the configuration of the dependency injection.

screenshot of code symbol screenshot api testing

the entire application can now be tested using postman ( use the registerspeech or develop branch ).

screenshot of code

the source code is available here: registerspeechrepository

best regards.


further reading

building microservices through event-driven architecture, part 2: domain objects and business rules

building microservices through event-driven architecture, part 3: presenters, views, and controllers

microservices architectures: event-driven approach

Repository (version control) Architecture microservice Database

Published at DZone with permission of Gora LEYE, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • What Does Synchronization With Asyncio Look Like
  • Common Performance Management Mistakes
  • Single Responsibility Principle: The Most Important Rule in the Software World
  • How To Get Closer to Consistency in Microservice Architecture

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

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: