Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gordon Saxby 1313 posts 1551 karma points
    Apr 01, 2016 @ 09:18
    Gordon Saxby
    0

    Using API - is this right

    Sorry, probable "stupid question" alert! Is the code below the correct approach if I want to add topics via code?

                var unitofWorkManager = new UnitOfWorkManager(ContextPerRequest.Db);
                Topic topic = null;
                using (var unitOfWork = unitofWorkManager.NewUnitOfWork())
                {
                    try
                    {
                        var newTopic = new Topic
                                       {
                                           Category = forumCategory, 
                                           Name = "Title", 
                                           MemberId = intMemberId
                                       };
                        topic = Dialogue.Logic.Services.ServiceFactory.TopicService.Add(newTopic);
    
                        // Commit the transaction
                        unitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        // Roll back database changes 
                        unitOfWork.Rollback();
                        // Log the error
                    }
                }
    

    I have read this https://github.com/YodasMyDad/Dialogue#servicefactory--unitofwork which is where the code sample comes from, but I just wanted to check I needed the "unitofwork" stuff.

Please Sign in or register to post replies

Write your reply to:

Draft