Contact and ResultModel Classes

This lesson is part of our C# Contacts Management System Development Course with a MS Access Database backend and Windows Forms as the user interface. In this particular class we want to create our model classes.

What are our model classes?

Well these are our data objects. They are classes that represent our business object. The business object in this system we are developing is a Contact object. We have to create a class from which the contact object will be instantiated.

Here are the Model classes we create for this system:

No. Model Class Usage
1. Contact To represent a single contact. It's what we will store in database.
2. ResultModel Will hold a List of contacts as well as message. Represents a resultset we pass around including both message and the database result.

Video Tutorial(Recommended)

https://www.youtube.com/watch?v=oXN6-e9Gks8

/Model/Contact.cs

This is our main model class. It's our data object. Our system is a Contacts Management System. Well it is this class instances that we are managing in our system.

A single Contact will have the following properties:

  1. ID - Autogenerated at the database level.
  2. Name - Name of the contact.
  3. Phone 1 and Phone 2 - Mobile phone numbers.
  4. Email - Contact's email address.
  5. Remarks - Additional remarks about the contact.

Here's the full code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ContactManager.Data.Model
{
    class Contact
    {
        public int Id { get; set; }
        public String Name { get; set; }
        public String Phone1 { get; set; }
        public String Phone2 { get; set; }
        public String Email { get; set; }
        public String Remarks { get; set; }
    }
}

/Model/ResultModel.cs

This class's instance will be used to hold a resultset that we can pass from our repository class to our user interface or other classes.

The advantage it provides us is it's capability to hold both or either of our message and list of contacts. Thus we have consistency in whatever object we are returning. For example if we have an error while attempting to retrieve our data, we simply return the error as a message property of this class's instance.

If we have a list of contacts, we simply return it as a property of this class's instance.

Here's the full code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ContactManager.Data.Model
{
    class ResultModel
    {
        public string Message { get; set; }
        public List<Contact> Contacts { get; set; }
    }
}

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *