Question Saving Hierarchically Recursive object Iteratively

md.nasir6

New member
Joined
Feb 24, 2021
Messages
2
Programming Experience
5-10
I have database table object which is:

C#:
[Table("CampaignRuleSetup")]
public class CampaignRuleSetup
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
    public int? AdditionalFieldId { get; set; }
    public int? FieldType { get; set; }
    public int? CustomLookupId { get; set; }
    public string Values { get; set; }

    [ForeignKey("AdditionalFieldId")]
    public virtual CampaignAdditionalField CampaignAdditionalField { get; set; }

    [ForeignKey("CustomLookupId")]
    public virtual CustomLookup CustomLookup { get; set; }
}

And I have a view model whcih is :

C#:
public class TreeviewItemViewModel
{
    public TreeviewItemViewModel()
    {
        Children = new List<TreeviewItemViewModel>();
    }

    public string Text { get; set; }
    public RuleSetupModel Values { get; set; }
   
    // Children
    public List<TreeviewItemViewModel> Children { get; set; }
}

public class RuleSetupModel
{
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Name { get; set; }
    public int? AdditionalFieldId { get; set; }
    public int? FieldType { get; set; }
    public int? CustomLookupId { get; set; }
    public string Values { get; set; }
    public string LabelText { get; set; }
    public int TreeLevel { get; set; }
    public RuleSetupModel Parent { get; set; }
}

Now I need to save TreeviewItemViewModel to the nth Level. But my below method only goes to level 3. How can I go to nth Level to save child and parent objects

Repository method to save and update object:

C#:
public int SaveOrUpdateCampaignRuleSetup(CampaignRuleSetup model)
{
    using (var context = new URNCustomizedFieldDataContext())
    {
        if (model.Id > 0) // update
        {
            var item = context.CampaignRuleSetups.FirstOrDefault(x => x.Id == model.Id);

            if (item == null)
                return 0;

            item.Name = model.Name;
            item.AdditionalFieldId = model.AdditionalFieldId;
            item.FieldType = model.FieldType;
            item.CustomLookupId = model.CustomLookupId;
            item.Values = model.Values;

            context.SaveChanges();
        }
        else // insert
        {
            context.CampaignRuleSetups.Add(model);
            var save = context.SaveChanges();
        }

        return model.Id;
    }
}

Save function to save objects

C#:
public bool SaveOrUpdateCampaignRuleSetup(TreeviewItemViewModel viewModel)
{  
    using (TransactionScope transactionScope = new TransactionScope())
    {
        try
        {          
            // Level 1
            var parentModel = new CampaignRuleSetup
            {
                Id = viewModel.Values.Id,
                Name = viewModel.Values.Name,
                AdditionalFieldId = viewModel.Values.AdditionalFieldId,
                FieldType = viewModel.Values.FieldType,
                CustomLookupId = viewModel.Values.CustomLookupId,
                Values = viewModel.Values.Values
            };

            var parentId = _dataRepository.SaveOrUpdateCampaignRuleSetup(parentModel);         

            // Level 2
            foreach (var child in viewModel.Children)
            {
                var childModel = new CampaignRuleSetup
                {
                    Id = child.Values.Id,
                    ParentId = parentId,
                    Name = child.Values.Name,
                    AdditionalFieldId = child.Values.AdditionalFieldId,
                    FieldType = child.Values.FieldType,
                    CustomLookupId = child.Values.CustomLookupId,
                    Values = child.Values.Values
                };

                var childId = _dataRepository.SaveOrUpdateCampaignRuleSetup(childModel);
           
                // Level 3
                foreach (var grandChild in child.Children)
                {
                    var grandChildModel = new CampaignRuleSetup
                    {
                        Id = grandChild.Values.Id,
                        ParentId = childId,
                        Name = grandChild.Values.Name,
                        AdditionalFieldId = grandChild.Values.AdditionalFieldId,
                        FieldType = grandChild.Values.FieldType,
                        CustomLookupId = grandChild.Values.CustomLookupId,
                        Values = grandChild.Values.Values
                    };

                    _dataRepository.SaveOrUpdateCampaignRuleSetup(grandChildModel);
                }
            }              

            transactionScope.Complete();
        }
        catch (Exception ex)
        {
            transactionScope.Dispose();        
            return false;
        }
    }

    return true;
}
 
Last edited by a moderator:
Any particular reason why you need to save the hierarchy iteratively as opposed to recursively?
 
Careful how you word things. I pick up on stuff like this and grill people for it, when its often not what they meant.

Your UI is not for getting data from. That's what models are for.
 
I am getting data this way from UI
Really? Looks like you have the entire object available to you:
public bool SaveOrUpdateCampaignRuleSetup(TreeviewItemViewModel viewModel)
 

Latest posts

Back
Top Bottom