Show / Hide Table of Contents

    IDataPacker.AddModel([NotNull]this IDataPacker packer, [NotNull]string key, [NotNull]TModel value) Method

    .NET Standard 2.x

    Adds a model object as an element to the IDataPacker object.

    Namespace: SnapObjects.Data

    Assembly: SnapObjects.Data.dll

    Syntax

    public static IDataIncludeSetter<TModel> AddModel<TModel>(
    [NotNull]this IDataPacker packer,
    [NotNull]string key,
    [NotNull]TModel value) where TModel : class
    

    Type Parameters

    TModel

    The type of the model object.

    Parameters

    key System.String

    The key of the element, which is the identifier of the element in the IDataPacker object.

    value TModel

    The model object, which is the value of the element to be added to the IDataPacker object.

    Returns

    SnapObjects.Data.IDataIncludeSetter<TModel>

    Returns an IDataIncludeSetter<TModel> object which can be used to indicate which embedded property of the model object should be included in the new added element.

    Examples

    The following code example adds a model object to the IDataPacker object and gets the number of elements in IDataPacker.

    using Appeon.ApiDoc.Models.School;
    using SnapObjects.Data;
    using System;
    using System.Data;
    
    namespace Appeon.ApiDoc.IDataPackerExamples
    {
        public class AddModelExample
        {
            private readonly SchoolContext _context;
    
            public AddModelExample(SchoolContext dataContext)
            {
                // Sets the data context.
                _context = dataContext;
            }
    
            public void Example()
            {
                var packer = new DataPacker();
    
                // Creates a model object "course"
                var course = new Course()
                {
                    CourseID = 1000,
                    Title = "Chiness",
                    Credits = 2,
                    DepartmentID = 1
                };
    
                // Adds the "course" model object to the current IDataPacker
                packer.AddModel("Model", course);
    
                // Gets and shows the number of elements of IDataPacker
                Console.WriteLine("The element count: {0}", packer.Count.ToString());
    
                // Gets and shows the data values in JSON format
                Console.WriteLine(packer.GetTextString(DataFormat.Json));
    
                /* This code example produces the following output:
                
                The element count: 1
                {"Model":{"CourseID":1000,"Title":"Chiness","Credits":2,
                "DepartmentID":1}}
                */
            }
        }
    }
    

    Example Refer To

    Model Class: Course

    Applies to

    .NET Standard

    2.x

    Back to top Generated by Appeon