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