Comments (6)
The command looks ok, but where is the following code from?
Did you notice that the SqlException is handled without being rethrown?
public void Execute(ICommand command){ if (command.RequiresTransaction && _transaction==null) throw new Exception($"Error") try{ command.Execute(_connection, _transaction) } Catch(SqlClient.SqlException ex Debug.WriteLine($"Error: {ex.Message}") } }
from dapper.unitofwork.
Since the transaction is explicitly started, if not committed it gets rolled back. There is no need to issue a rollback in a catch block. Of course if you have modified the original code, things may not work as expected :-)
The exception handler is meant to catch sql transient exceptions and issue retries.
from dapper.unitofwork.
Hi,
the rollback is handled automatically.
Can you please add the whole AddPersonCommand class?
If you have correctly inherited from ICommand, the Execute method should be:
void Execute(IDbConnection connection, IDbTransaction transaction)
Example here: https://github.com/outmatic/Dapper.UnitOfWork/blob/master/src/Dapper.UnitOfWork/Dapper.UnitOfWork.Example/Data/Commands/AddCustomerCommand.cs
from dapper.unitofwork.
It's all VB
Imports System.Threading
Imports Dapper
Imports RepositoryTest.DataMine
Public Class AddPersonCommand
Implements ICommand, IAsyncCommand
Private Const statement As String = "INSERT INTO People(Name, Age) VALUES(@Name, @Age)"
Private ReadOnly Property _Person As Person
Public ReadOnly Property RequiresTransaction As Boolean Implements IAsyncCommand.RequiresTransaction, ICommand.RequiresTransaction
Get
Return True
End Get
End Property
Public Sub New(ByVal Person As Person)
_Person = Person
End Sub
Public Sub Execute(connection As IDbConnection, transaction As IDbTransaction) Implements ICommand.Execute
connection.Execute(statement, _Person, transaction)
End Sub
Public Function ExecuteAsync(connection As IDbConnection, transaction As IDbTransaction, Optional cancelationToken As CancellationToken = Nothing) As Task Implements IAsyncCommand.ExecuteAsync
Return connection.ExecuteAsync(statement, _Person, transaction)
End Function
End Class
from dapper.unitofwork.
Understood, since your example had only one annotation for the mandatory Commit i thought the Rollback was automatic as you said, as if it doesn't need an extra Try...Catch block, it got me confused, that's why i was wondering what did i do wrong. And sorry for the late answer even after taking your time, i was driving back home.
UnitOfWork(Repository) Execute method, reverted back to its inital state.
Public Sub Execute(command As ICommand) Implements IUnitOfWork.Execute
If command.RequiresTransaction And IsNothing(_transaction) Then
Throw New Exception($"Error")
End If
command.Execute(_connection, _transaction)
End Sub
And the implementation of the repository within the Main method of a console application:
Using uow As UnitOfWork = repository.Create(True)
Try
uow.Execute(New AddPersonCommand(New Person With {.Name = "Jane Smith", .Age = 24}))
uow.Execute(New AddPersonCommand(New Person With {.Name = "Jane Smith", .Age = 24}))
uow.Commit()
Catch ex As Exception
uow.RollBack()
End Try
End Using
I also noticed that your code implements an exception handler, is he responsible for rolling back the transaction and/or avoiding the commit ?
from dapper.unitofwork.
Yeah, i work with more complex and bulk data and debug is the only thing that keeps me on track(most of times). I was using an old implementation and not much of eficient as i expected, was just a hack apparently and now i'm replacing the code i have because i'm about to add more functionality and right now Dapper is the only thing working with my models. Thanks for the feedback man.
from dapper.unitofwork.
Related Issues (5)
- Simple Example HOT 2
- Dapper.UnitOfWork HOT 3
- Is it deadlock safe ? HOT 3
- Creating UoW for postgres connection. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dapper.unitofwork.