This question is locked. New answers and comments are not allowed.
I've had great luck with OpenAccess in a web environment & I am having some issues doing it in a WinForms environment. I am using the ScopeFactory method to get my IObjectScope from the thread. My object scope is managed from a static class:
I am using SQL Server CE as my database, and am having an issue getting the data saved. I might be just overlooking this problem and thought I'd post this to see if someone can help me.
I have a form with a button that opens a form to create a new quarter. From this quarter form, here is my code to add a new quarter:
The Save function on Quarter Service adds the object to the object scope. Now when the form closes & returns to the parent form, I refresh a grid of "Quarters" where my recently created Quarter shows up. It appears it's saved in the DB, but when I close the application & view the DB it is empty. It seems to me that it should be added.
I don't know if my approach is way off as the ScopeFactory example doesn't show how to work with transactions (opening/committing) & I don't know what the practice is to handle these transactions for a WinForms app.
If anyone has any suggestions or sees something wrong with my approach, any help is greatly appreciated. As I said, I'm gotten OA to work great in my web application but this my first WinForms attempt with OA.
Thanks,
Mark
| public class ScopeContextManager |
| { |
| private static IObjectScope _scope; |
| static ScopeContextManager() |
| { |
| } |
| /// <summary> |
| /// The current scope. |
| /// </summary> |
| private static IObjectScope CurrentScope |
| { |
| get |
| { |
| if (_scope == null || !_scope.Transaction.IsActive) |
| { |
| _scope = GetCurrentScope(); |
| } |
| return _scope; |
| } |
| } |
| /// <summary> |
| /// Returns the current scope for the application. |
| /// </summary> |
| /// <returns>A <c>IObjectScope</c> object.</returns> |
| public static IObjectScope GetCurrentScope() |
| { |
| return GetScopePerThread(); |
| } |
| /// <summary> |
| /// Begins the transaction for the current scope if the transaction is not active. |
| /// </summary> |
| public static void BeginTransaction() |
| { |
| if (!CurrentScope.Transaction.IsActive) |
| { |
| CurrentScope.Transaction.Begin(); |
| } |
| } |
| /// <summary> |
| /// Commits the transaction for the current scope if transaction is still active. |
| /// </summary> |
| public static void CommitTransaction() |
| { |
| if (CurrentScope.Transaction.IsActive) |
| { |
| CurrentScope.Transaction.Commit(); |
| } |
| } |
| /// <summary> |
| /// Rolls back the transaction for the current scope if transaction is active. |
| /// </summary> |
| public static void RollbackTransaction() |
| { |
| if (CurrentScope.Transaction.IsActive) |
| { |
| CurrentScope.Transaction.Rollback(); |
| } |
| } |
| /// <summary> |
| /// Gets the object scope for the thread. |
| /// </summary> |
| /// <returns></returns> |
| private static IObjectScope GetScopePerThread() |
| { |
| IObjectScope scope = null; |
| string key = Thread.CurrentContext.ContextID.ToString(); |
| LocalDataStoreSlot slot = Thread.GetNamedDataSlot(key); |
| if (slot != null) |
| { |
| scope = (IObjectScope)Thread.GetData(slot); |
| } |
| if (scope == null) |
| { |
| scope = ObjectScopeProvider.GetNewObjectScope(); |
| if (slot == null) { |
| slot = Thread.AllocateNamedDataSlot(key); |
| } |
| Thread.SetData(slot, scope); |
| } |
| return scope; |
| } |
| } |
I am using SQL Server CE as my database, and am having an issue getting the data saved. I might be just overlooking this problem and thought I'd post this to see if someone can help me.
I have a form with a button that opens a form to create a new quarter. From this quarter form, here is my code to add a new quarter:
| if (String.IsNullOrEmpty(txtQuarterName.Text)) |
| { |
| MessageBox.Show("Quarter name is required."); |
| return; |
| } |
| try { |
| ScopeContextManager.BeginTransaction(); |
| Quarter quarter = new Quarter(); |
| quarter.Name = txtQuarterName.Text; |
| quarter.StartDate = dtpQuarterStartDate.Value; |
| quarter.EndDate = dtpQuarterEndDate.Value; |
| quarter.Description = txtQuarterDescription.Text; |
| QuarterService quarterService = new QuarterService(); |
| quarterService.Save(quarter); |
| ScopeContextManager.CommitTransaction(); |
| this.Close(); |
| } |
| catch(Exception x) { |
| ScopeContextManager.RollbackTransaction(); |
| MessageBox.Show(x.Message); |
| } |
The Save function on Quarter Service adds the object to the object scope. Now when the form closes & returns to the parent form, I refresh a grid of "Quarters" where my recently created Quarter shows up. It appears it's saved in the DB, but when I close the application & view the DB it is empty. It seems to me that it should be added.
I don't know if my approach is way off as the ScopeFactory example doesn't show how to work with transactions (opening/committing) & I don't know what the practice is to handle these transactions for a WinForms app.
If anyone has any suggestions or sees something wrong with my approach, any help is greatly appreciated. As I said, I'm gotten OA to work great in my web application but this my first WinForms attempt with OA.
Thanks,
Mark