How to call the Function in MVVM pattern design

DPlug 41 Reputation points
2020-12-19T19:34:42.22+00:00

I have a byte array to image converter method that converts image stored as byte array in SQL server database to image type for display in the image control. I am confuse on how to call the method to display the image. I am using mvvm pattern. Here is my student service class:
public List<Student> GetAll()
{
List<Student> StudentList = new List<Student>();
try
{
StudentCmd.Parameters.Clear();
StudentCmd.CommandText = "spStudent_GetAll";

                StudentConn.Open();
                var StudentReader = StudentCmd.ExecuteReader();
                if (StudentReader.HasRows)
                {
                    student = null;
                    while (StudentReader.Read())
                    {
                        student = new Student
                        {
                            Id = (int)StudentReader["Id"],
                            LastName = (string)StudentReader["LastName"],
                            FirstName = (string)StudentReader["FirstName"],
                            Email = (string)StudentReader["Email"],
                            Gender = (string)StudentReader["Gender"],
                            DateofBirth = (DateTime)StudentReader["DateofBirth"],
                            DateRegistered = (DateTime)StudentReader["DateRegistered"],
                            MobileNumber = (string)StudentReader["MobileNumber"],
                            Notes = (string)StudentReader["Notes"],
                            Religion = (string)StudentReader["Religion"],
                            Image = (byte[])StudentReader["Image"],
                            SpecialAtttention = (string)StudentReader["SpecialAttention"]
                        };

                        StudentList.Add(student);
                    }
                }
                StudentReader.Close();
            }
            catch (SqlException ex)
            {

                throw ex;
            }
            finally
            {
                StudentConn.Close();
            }
            return StudentList;
        }

And this is my byte array to image converter method:
private BitmapImage GetBitmapImageFromBytes(byte[] bytes)
{
BitmapImage btm;
using (MemoryStream stream = new MemoryStream(bytes))
{
btm = new BitmapImage();
btm.BeginInit();
btm.StreamSource = stream;
btm.CacheOption = BitmapCacheOption.OnLoad;
btm.EndInit();
btm.Freezenter code heree();
}
return btm;
}
This is my student model:

    public class Student : INotifyPropertyChanged
    {
       public event PropertyChangedEventHandler PropertyChanged;

        public int Id { get; set; }
        public int GuradianID { get; set; }

        public string LastName { get; set; }
        public string FirstName { get; set; }
        public string Email { get; set; }
        public string Gender { get; set; }
        public DateTime DateofBirth { get; set; }

        /// <summary>
        /// Date enrolled in school
        /// </summary>
        public DateTime DateRegistered { get; set; }
        public string MobileNumber { get; set; }

        /// <summary>
        /// List of student addresses if he/she has more than one
        /// </summary>
        public List<Address> Addresses { get; set; } = new List<Address>();
        public string Notes { get; set; }
        public string Religion { get; set; }
        public byte[] Image { get; set; }

        /// <summary>
        /// Determine student who needs extra lessons
        /// </summary>
        public string SpecialAtttention { get; set; }

How do I call this method to convert byte array to image for display to user?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,907 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,221 Reputation points
    2020-12-20T11:21:43.33+00:00

    The call to the function should be made from the viewmodel.

    <copied>

    ViewModel is the main point of MVVM application. The primary responsibility of the ViewModel is to provide data to the view, so that view can put that data on the screen.

    <end>

    https://www.tutorialspoint.com/mvvm/mvvm_responsibilities.htm#:~:text=ViewModel%20Responsibilities,data%20and%20change%20the%20data.

    It does not mean that the function, the method, should be in the VM, but rather a call from the VM is made to the method that typically would be in the data access layer (DAL) if truly trying to implement SoC.

    https://en.wikipedia.org/wiki/Separation_of_concerns#:~:text=In%20computer%20science%2C%20separation%20of,code%20of%20a%20computer%20program.

    MVVM is a form of SoC called SoD.

    https://www.codeproject.com/Articles/228214/Understanding-Basics-of-UI-Design-Pattern-MVC-MVP


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.