Ask Ayende: Life without repositories, are they worth living?
Join the DZone community and get the full member experience.
Join For FreeWith regards to my quests against repositories, Matt asks:
For example, you dismiss the repository pattern, but what are the alternatives? For example, in an ASP.NET web application you have controllers. I do NOT want to see this code in my controllers:
var sessionFactory = CreateSessionFactory(); using (var session = sessionFactory.OpenSession()) { using (var transaction = session.BeginTransaction()) { // do a large amount of work // save entities session.SaveOrUpdate(myEntity); transaction.Commit(); } }That is ugly, repetitive code. I want in my service methods to Get, update, save, and not have to worry about the above.
This is a straw dummy. Set up the alternative as nasty and unattractive as possible, then call out the thing you have just set up as nasty and unattractive. It is a good tactic, except that this isn’t the alternative at all.
If you go with the route that Matt suggested, you are going to get yourself into problems. Serious ones. But that isn’t what I recommend. I talked about this scenario specifically in this post. This is how you are supposed to set things up. In a way that doesn’t get in the way of the application. Everything is wired in the infrastructure, and we can just rely on that to be there. And in your controller, you have a Session property that get the current property, and that is it.
For bonus points, you can move your transaction handling there as well, so you don’t need to handle that either. It makes the code so much easier to work with, because you don’t care about all those external concerns, they are handled elsewhere.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Merge GraphQL Schemas Using Apollo Server and Koa
-
How To Use Pandas and Matplotlib To Perform EDA In Python
-
Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
-
Top 10 Engineering KPIs Technical Leaders Should Know
Comments