Python for the .NET Developer Transcripts
Chapter: Database access and ORMs in Python
Lecture: Unit-of-work design pattern

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We're almost able to start doing queries. The last thing we have to do is be able to call this factory to get the unit of work.
0:08 But we want to have a little bit more control than just making this a field they can work with. So we're going to go down here and define a function
0:14 called create_session and it's going to return. Well, let's hold off and see what it returns for just a second. Let's also put that here.
0:26 And let's say a factory. 'Cause that's we're really using. In the end, we're going to say session = __factory() like this.
0:35 And you would think we're going to return session but there's actually two little improvements we can make really quick.
0:42 I think this says, None can't be called or something like that. Um, but it is. We can go and put a fairly complicated type annotation
0:50 on this thing, like this. We can say this is a nullable optional function that can be called and it takes no argument
0:59 but it returns a session. Like that. We put that down here then oh it's happy again and actually, this will know that we're returning.
1:10 At first, if we return it like this it's going to return a session. OK, we're not going to stick with that. So this is really good, or pretty much
1:17 like we could call this the way it is now but there's two things we want to change. One, there's a weird expiring sort of thing
1:24 when you do an insert and you get an object back so I want to say, Expire on commit is false. Right, this makes data a little more portable
1:31 outside of just doing a query and then throwing it away. We're doing an enter and then throwing it away. And the other things is, I would like
1:37 to be able to model the using DB context... thing we have in C#. I would like to be able to use a with statement
1:46 on this session to say this with statement this context manager, defines what the unit of work is. Doesn't work that way with SQLAlchemy
1:54 but remember, Python is flexible so, we can fix that really easily. Let's go fix it by adding a file called context_session.
2:02 so a DB session that can be used within a context manager. I'm just going to drop this in. So, we have a session
2:09 and the way it works is we create one of these classes and it takes the session and then it upgrades it into this thing
2:15 that can be used in a context manager. In C#, the way you do that is you implement IDisposable. Here, we implement an __enter__
2:22 which returns some object that has an __exit__. And, this object is itself a thing that has an __exit__
2:29 that takes the exception details if one happens. Doesn't really matter, but it says Look, if there's an exception, we're going to rollback
2:35 whatever and then either eat it or we could raise it again. I probably should raise it here. I'm not sure. Actually, I'm just going to let it pass.
2:43 I think it re-throws it and just calls it on the way out. But either way, regardless whether there's an exception or not we're going to close it. Ok.
2:51 So we can return one of these, which will let us write cleaner code. Like that, we're going to import at the top and then the way it works is we
2:59 allocate a new one like that. So we can just say, later like this, with create_session as unit of work and then unit of work stuff right
3:10 uh session dot you know query or whatever. So this context session gives us this design pattern which I think is just perfect.
3:20 Okay, now we can create these sessions create these unit of work right, session is the unit of work name in SQLAlchemy nomenclature.
3:28 Our database is ready to start doing things.

Talk Python's Mastodon Michael Kennedy's Mastodon