Print Page | Close Window

Saving an entity using ApplicationRepository

Printed From: IdeaBlade
Category: Cocktail
Forum Name: Community Forum
Forum Discription: A professional application framework using Caliburn.Micro and DevForce
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3517
Printed Date: 07-May-2025 at 10:04am


Topic: Saving an entity using ApplicationRepository
Posted By: pponzano
Subject: Saving an entity using ApplicationRepository
Date Posted: 05-Jul-2012 at 6:49am
Hello,
I'm using an applicationrepository (and IApplicationRepository) to have access to DB, I was wondering if it's correct to have

public OperationResult CreateDashBoard(DBOARD dashboard, Action<IEnumerable> onSuccess = null, Action<Exception> onFail = null)

   {

       entityManagerProvider.Manager.AddEntity(dashboard);

 

       return AlwaysCompletedOperationResult.Instance;

   }



and (to perform the save)

public OperationResult SaveAsync(Action<EntitySaveOperation> onSuccess )

     {

         SaveOptions saveOptions = new SaveOptions();

 

         entityManagerProvider.Manager.SaveChangesAsync(saveOptions,onSuccess);

 

         return AlwaysCompletedOperationResult.Instance;

     }


My Entity DBOARD also has a relation to LAYOUT... in my view I've a dropdown form where to choose the layout.....since DBOARD <-> LAYOUT is a MXN relation (I can have more dashboard with the same layout) how do I define this realation when saving?
Thanks



Replies:
Posted By: mgood
Date Posted: 12-Jul-2012 at 1:05am
Sorry about the delayed response. I just got back from vacation. Your CreateDashBoard method is not a create method if you have to pass the entity as a parameter. That would mean you have to create the entity outside of the method, which kind of defeats the purpose of the method, don't you think? Also, if the creation isn't asynchronous, just make the method synchronous.


public
 DBOARD CreateDashBoard()

   {

       var dashboard = new DBOARD();

       // Optional initialization of the newly created entity.

       entityManagerProvider.Manager.AddEntity(dashboard);

 

       return dashboard;

   }


As for the save method. If you return AlwaysCompletedOperationResult, the caller won't have any idea when the save is actually completed and whether it was successful or not. Also, if you don't override the default SaveOptions, don't bother with it. The save method should look like this.

public OperationResult<SaveResult> SaveAsync(Action<SaveResult> onSuccess = null, Action<Exception> onFail = null)

     {

         return entityManagerProvider.Manager

                   .SaveChangesAsync(op => op.OnComplete(onSuccess, onFail))

                   .AsOperationResult();

     }


Finally, to relate entities, you simple add them to the respective navigation property/collection before you save, or set the applicable foreign key property to the correct key (not applicable for many-to-many).

For example as seen in TempHire, the following StaffingResource method relates a newly created address entity to the StaffingResource by simply adding it to the Addresses collection.

        public Address AddAddress(AddressType type)
        {
            Address address = Address.Create(type);
            Addresses.Add(address);
 
            return address;
        }




Print Page | Close Window