c# - Transactions across several DAL methods from the one method in the BLL -


How can you go about calling a number of ways in the business logic level with a method in the data lock layer so that all SQL order lived in a SQL transaction?

Every method of DAL in BLL can be called separately from different places, so there is no guarantee that the data layer methods are always part of a transaction. We need this functionality, if the database becomes offline between a long-running process, then there is no commitment. The business layer is organizing different data layer method calls based on the results of each previous call. We want to commit to the very end of the whole process (at the professional level).

OK, firstly, you have to follow a nuclear unit of work that you specify Your BLL will have to make a single method (for example) customers, order and order items. So you will arrange everything in a TransactionScope systematically to all of you who are using the statement. TransactionScope is the secret weapon here. I'm working on some code that luckily enough I below now :) is:

  throw public static int InsertArtist (artist Artist) {if (artist == null) New ArgumentNullException ("Artist"); Int Artist = 0; Using (TransactionScope scope = new TransactionScope ()) {// insert master artist / * we any child instances where ArtistID required * / artistid = SiteProvider.Artist.InsertArtist (New ArtistDetails (0, artistid in artist.BandName use Variable plug, artist.redited)); // Child Join ArtistArtistGenre artist.ArtistArtistGenres.ForEach (item = & gt; {var artistartistgenre = new ArtistArtistGenreDetails (0, artistid, item.ArtistGenreID); SiteProvider.Artist.InsertArtistArtistGenre (artistartistgenre);}); // Child Join ArtistLink artist.ArtistLinks.ForEach (item = & gt; {var artistlink = new ArtistLinkDetails (0, artistid, item.LinkURL); SiteProvider.Artist.InsertArtistLink (artistlink);}); // Child Join ArtistProfile artist.ArtistProfiles.ForEach (item = & gt; {var artistprofile = new ArtistProfileDetails (0, artistid, item.Profile); SiteProvider.Artist.InsertArtistProfile (artistprofile);}); {Var festivalartist = new FestivalArtistDetails (0, item.FestivalID, artistid, item.AvailableFromDate, item.AvailableToDate, item.DateAdded), SiteProvider.Festival.InsertFestivalArtist (// child FestivalArtist artist.FestivalArtists.ForEach (item = & gt insert Fasnstist );}); BizObject.PurgeCacheItems (String.Format (ARTISTARTISTGENRE_ALL_KEY, string. Empty, String.Empty)); BizObject.PurgeCacheItems (String.Format (ARTISTLINK_ALL_KEY, string. Empty, String.Empty)); BizObject.PurgeCacheItems (String.Format (ARTISTPROFILE_ALL_KEY, string.opt, string.optim)); BizObject.PurgeCacheItems (String.Format (FESTIVALARTIST_ALL_KEY, string.opt, string.optim)); BizObject.PurgeCacheItems (String.Format (ARTIST_ALL_KEY, string. Empty, String.Empty)); // The whole transaction - everything or anything scope. complete (); } Return artist; }  

Hopefully, you will receive the essence basically, this is a successful or unsuccessful job, even if any different database (either in the example above, artist and artistist two Can be hosted in different DB stores, but the TranscensCancoscope will take less care about that, it works at the COM + level and Atomic

Edit: You can potentially You will know that the initial reception of Transaction Scope The start up (on the app) start-up can be a bit noticeable (i.e., in the example given above, if the first call is made, it can take 2-3 seconds to complete), though In between, a simple point between call almost are instantaneous (i.e. typically 250-750 ms) (unwieldy) option (for me and my clients) is closed between contact transaction, which is the initial 'loading' Latency

Just wanted to show that it does not come easily without compromising (though in the initial stage)


Comments