New Posts New Posts RSS Feed: Saving an entity using ApplicationRepository
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Saving an entity using ApplicationRepository

 Post Reply Post Reply
Author
pponzano View Drop Down
Senior Member
Senior Member
Avatar

Joined: 28-Apr-2011
Location: Italy
Posts: 165
Post Options Post Options   Quote pponzano Quote  Post ReplyReply Direct Link To This Post Topic: Saving an entity using ApplicationRepository
    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
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post 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;
        }

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down