Skip to content
frwsoftware edited this page Sep 5, 2017 · 1 revision

Basic Usage Scenario

In order to start working you need to create your own empty Windows Forms project and reference the necessary libraries (it is possible to take the demonstration template for the basics). Next, you need to create your own data model. Each entity of the data model is a c # class (POCOs - Plain old CLR objects). Field names, data types, and relationships are described using annotations.

Simple dto

[JDisplayName("Student")]
[JEntity]
public class StudentDto
{
    //primary key with autoincrement 
    [JDisplayName("Id")]
    [JPrimaryKey, JAutoIncrement]
    public string StudentDtoId { get; set; }

    //name field 
    [JDisplayName("Last name")]
    [JNameProperty, JRequired]
    public string LastName { get; set; }

    //simple field 
    [JDisplayName("First name")]
    public string FirstName { get; set; }

    //many to one relationship
    [JDisplayName("Main teacher")]
    [JManyToOne]
    public TeacherDto MainTeacher { get; set; }

    //many to many relationship
    [JDisplayName("Teachers")]
    [JManyToMany]
    public IList<TeacherDto> Teachers { get; set; }
}

Complex dto

[JDisplayName("Teacher")]
[JEntity]
public class TeacherDto
{
    //primary key with autoincrement 
    [JDisplayName("Id")]
    [JPrimaryKey, JAutoIncrement]
    public string TeacherDtoId { get; set; }

    //name field 
    [JDisplayName("Last name")]
    [JNameProperty, JRequired]
    public string LastName { get; set; }

    //simple field
    [JDisplayName("First name")]
    public string FirstName { get; set; }

    //field with custom json name (to load and save)
    [JDisplayName("Address "), JsonProperty("streetAndTown")]
    public string Address { get; set; }

    //read only field (Does not appear in list and property windows)
    [JReadOnly]
    public string PostCode { get; set; }

    //Does not save and does not load onto disk file
    [JsonIgnore]
    public string Country { get; set; }

    //Datetime field with current datetime initialization 
    [JDisplayName("Creation date")]
    [JInitCurrentDate]
    public DateTime CreatedDate { get; set; }

    //Another datetime field
    [JDisplayName("Last modification date")]
    public DateTimeOffset LastUpdatedTime { get; set; }

    //Url (Href in list windows)
    [JUrl]
    public string PersonalPage { get; set; }

    //Field with image in the column header instead of text
    [JDisplayName("Description"), JHeaderImage("book_open")]
    public string Description { get; set; }

    //Text field (will be edit in new window)
    [JText]
    public string Information { get; set; }

    //Field with list of attachment documents 
    [JDisplayName("Attachment"), JHeaderImage("attachment")]
    [JAttachments]
    public List<JAttachment> Attachments { get; set; }

    //Many to one relationship
    [JDisplayName("Favorit Student")]
    [JManyToOne]
    public StudentDto FavoritStudent { get; set; }

    //One to many relationship
    [JDisplayName("Controled Group")]
    [JOneToMany]
    public IList<StudentDto> ControledGroup { get; set; }

    //Many to many  relationship
    [JDisplayName("Students")]
    [JManyToMany]
    public IList<StudentDto> Students { get; set; }

    //Complex field
    [JDisplayName("Full name")]
    [JReadOnly, JsonIgnore]
    public string FullName
    {
        get
        {
            return LastName +  (FirstName != null ?  (" " + FirstName) : "");
        }
    }

    //Complex field
    [JDisplayName("Favoirt student last name")]
    [JReadOnly, JsonIgnore]
    public string FavoritStudentLastName
    {
        get
        {
            return FavoritStudent != null ? FavoritStudent.LastName : null;
        }
    }
}

The class of the main application window must be inherited from the BaseMainAppForm class. Create a menu item to call up a list of each entity (you can use a short entry). After that, you already get a fully functional application, in which for each entity there will be full-function lists for data viewing (including filtering and sorting) and online editing of fields, as well as forms for complete editing (adding, updating) of each record. Full-featured editing works for both simple fields and one-to-many, many-to-many, many-to-many relationship fields. The composition and layout of the windows, as well as the settings of the lists (composition and order of the columns, etc.) are saved in the user settings between application starts. Writing business logic. Note that to implement simple operations of adding, updating, deleting data, you do not need to write any business logic at all.

Examples of operations.

        ///////////////////////////////////////////////////
        //Simple operations
        ///////////////////////////////////////////////////

        //Find by primary key
        object pk = "1";
        TeacherDto teacher1 = Dm.Instance.Find<TeacherDto>(pk);
        //or
        StudentDto student1 = (StudentDto)Dm.Instance.Find(typeof(StudentDto), pk);

        //Getting all entity data
        IList<TeacherDto> list = Dm.Instance.FindAll<TeacherDto>();
        //or 
        IList teachers = Dm.Instance.FindAll(typeof(TeacherDto));

        //Note: do not use list directly to Add or Remove elements. Use SaveObject and DeleteObject.

        //Find by params. 
        TeacherDto teacherWithSpecName = Dm.Instance.FindAll<TeacherDto>().FirstOrDefault(c => c.LastName == "Brown");
        //or
        IEnumerable filteredList = Dm.Instance.FindByParams(typeof(TeacherDto),
            new Dictionary<string, object> { { "FirstName", "John" } });

        //Create, insert, update

        //Create new entity
        //recomended way
        TeacherDto teacher2 = Dm.Instance.EmptyObject<TeacherDto>();
        //or    dto = (JExampleDto)Dm.Instance.EmptyObject(typeof(JExampleDto));
        teacher2.LastName = "Brown";
        Dm.Instance.SaveObject(teacher2);

        //not recomended way
        TeacherDto teacher3 = new TeacherDto();
        teacher3.LastName = "Brown";
        Dm.Instance.SaveObject(teacher3);

        //Remove entity
        Dm.Instance.DeleteObject(teacher2);

        //Remove all entities
        Dm.Instance.DeleteAllObjects(typeof(TeacherDto));

        ///////////////////////////////////////////////////
        //Working with relationship
        ///////////////////////////////////////////////////

        //Many to one relationship
        //get
        string favoritStudentFirstName = teacher2.FavoritStudent.FirstName;
        //set 
        teacher2.FavoritStudent = student1;
        Dm.Instance.SaveObject(teacher2);
        //unset (set null)
        teacher2.FavoritStudent = null;
        Dm.Instance.SaveObject(teacher2);

        //One to many relationship
        //Set
        StudentDto student10 = Dm.Instance.Find<StudentDto>("10");
        StudentDto student11 = Dm.Instance.Find<StudentDto>("11");
        StudentDto student12 = Dm.Instance.Find<StudentDto>("12");
        teacher2.ControledGroup.Add(student10);
        teacher2.ControledGroup.Add(student11);
        teacher2.ControledGroup.Add(student12);
        Dm.Instance.SaveObject(teacher2);
        //unset
        teacher2.ControledGroup.Remove(student12);
        Dm.Instance.SaveObject(teacher2);
        //unset all
        teacher2.ControledGroup.Clear();
        Dm.Instance.SaveObject(teacher2);

        /////////////////////////////////
        //Many to many  relationship
        //same as one to many relationship
        //set
        teacher2.Students.Add(student12);
        Dm.Instance.SaveObject(teacher2);
        //unset
        teacher2.Students.Remove(student12);
        Dm.Instance.SaveObject(teacher2);
        //unset all
        teacher2.Students.Clear();
        Dm.Instance.SaveObject(teacher2);

Extention

Extension of the list

Extension of the list functionality (adding non-standard columns, non-standard column layout, context menu items). It is solved by creating a list class inherited from SimpleListWindow and adding the necessary functionality to it. The list class must be registered in its application manager class, inherited from the AppManager class.

Extension of the data model.

Create your annotations. A special handler for editing such fields is called in its application manager class, inherited from the AppManager class and in its data manager class inherited from the Dm class. Connect to existing databases. Use this data instead of or in parallel with the built-in storage system. It is implemented in the data manager class inherited from the Dm class.