Unit of Work Pattern in Unity

preview_player
Показать описание

Data concurrency and data access bottlenecks are common problems that arise when dealing with persistence. In this video I'll show you how the Unit of Work design pattern (with the repository pattern) can solve both of these issues in your Unity project.

00:00 Introduction
01:24 The Repository Pattern
02:24 Sign up for the Level_2 Game Dev Newsletter
02:50 Repositories in Unity
04:48 Player Shop Example
06:23 Problems with Persistence
08:56 Unit of Work
10:38 Conclusion

💬 Join Our Community

❤️ My Favorite Unity Assets *

* Disclosure: These are affiliate links, which means I'll receive a commission if you use them to make a purchase.

* Disclosure: These are affiliate links, which means I'll receive a commission if you use them to make a purchase.
Рекомендации по теме
Комментарии
Автор

Very nice presentation. It's nice to see you exploring areas that other game dev channels don't cover.

harrysadlermusic
Автор

With everything you've learned in the past 2 years, is there any chance you could redo the video you posted on the 10th of december on 2019. The Unity Code Review, using the same code. But apply everything you learned in the past 2 years (if anything changed). Would be cool to see how your refactoring evolved over that period.

houbeenie
Автор

Hey Charles, love the new format and the new content you've been pushing out. I also think there's a bit of room for improvement compared to your older videos. I think the videos might be a bit too short since they rush through some key things. For example, you say "All our problems are solved" but you don't explain how or why they are now solved. I think it's because the unit of work makes sure all the repositories are updated in tandem every time they are changed, but its not super clear in the video, why delegating the save fixes everything.

thebebinator
Автор

Im not sure if this is way to go, its web dev pattern and orm-s wich use it are known from being slow. If you build for example server side of game then you can get way better result with one instance that will do direct queries in async way by prepare statements / stored procedures without repo / uow.

AvalancheOnline
Автор

Didn’t know your channel before, but credit for the relaunch. This high level concept stuff, understanding how lower level coding concepts fit into overall projects, is valuable. Thanks.

matt
Автор

Great video! There seems to be some heavy compression at points in the video though, noticed it mostly at 8:26. It hurts a bit when the visual presentation is so well done haha.

Momonyaro
Автор

I tend to create a base class that inherits from scriptable objects for persistence (during gameplay) that knows how to interact with a separate JSON serialize/deserialize system and then a system that can either save locally or push those JSON files to a backend server.

justadude
Автор

I'm using Phaser for HTML 5 games, but this is still very helpful. As a beginner who's past the point of just syntax and applying the concepts from zero to real deployed game, learning about structuring data and systems is very insightful. Thanks!

BrianDriscoll
Автор

Interesting pattern. I wish at 10:03 instead of just calling save on the data context you showed first changing the in memory values. when the video jumps straight to the conclusion, i was left puzzled. We put an abstraction layer between the service layer and the context.. but the abstract was just delegating with no additional steps. I had to rewatch parts of the video to realize you mean to put more logic in this layer but didn't for speed in the video.

jmguillemette
Автор

This channel is exactly what I needed. Most GameDev channels are good for design or marketing but few have good coding topics.
I can literally see how my code gets worth by making games and especially GameJams. It's a whole other thing working as a normal developer outside the gaming industry. Often when I see GameDev code I want to punch the creator...and also often I recognize my code is the same crap and being a solo dev I have no one to blame for.

I'll definitely watch your videos regularly to see common patterns used in GameDev.

PS: Sorry for ranting, but today is day 3 of refactoring my own code xD

KeyboardKrieger
Автор

Entity framework screaming all over the place, this is HIGH QUALITY CONTENT

eddyeffy
Автор

Unit of work sounds alot like Transaction in enteprise software. All the changes made within transaction will be persisted in the end, and if any error occurs then in general all the changes are dropped and database remains unchanged.

marko
Автор

you should change the description from:
"Download the project at... "
to:
"Become a Patreon to be able to download the project at... "
It is misleading information! I thought it would be a github link, but your post is locked.
Nice video though.
Cheers.

pliniomourao
Автор

Loved the content but I still fell pretty lost as you never showed where the other persistance objects where referenced, also how did you populate the shops Repository with the shop and ids. So many other questions were raised by the end of the video that it feels a bit incomplete

kevonboxill
Автор

Huh. I've been using he repository patterm without even knowing it. Was something I came up with myself. I call it an 'interpreter' that interpretes the raw data from the source. Most of the time this is essentially read only (server data) but I also use it for player preferences and other save files where he interpreter is also responsible for saving the data if it changes.

3:42 nice yeah. Mine are abstract too. In an early version they were interfaces, but I was using scriptable objects. You can serialize interfaces with Odin Inspector but this breaks unity cloud build for some reason (something about the AOT compilation process).

I wound up using Abstract serializable classes to base the scriptable objects from.

justinwhite
Автор

Hello Charles,
how about retreiving the sprites and other resources of the shop items?
Let's say I want to download song item. They will have a cover and the actual audio, the repositories will download the items, how about the cover and the music clip to play? Should I store it in the item as a url string and after I will have another web request to download it?
Thanks! Amazing content btw, I just subscribed to Patreon! Can't wait to see more!!

christianstamati
Автор

This is great. Is this your first design pattern in unity video ?

box
Автор

DataContext reminds of wpf :P cool tutorial as always :D

FuzzyDPozzy
Автор

Amazing video. I think Unit of work is a pattern I should explore outside of the Unity too. When working with data bases and object relational mappers, it seems like this is always properly implemented, but I never thought about designing such a system myself. Learned a lot in this video.

fmproductions
Автор

This video is top notch! I NEED MORE OF THIS IN MY LIFE! Love it!

olon