jump to navigation

Winform – Model-View-Presenter – tutorial Part III the Model January 28, 2009

Posted by wesaday in Programming.
Tags: , , ,
trackback

This is the third part of the MVP tutorial series. In this part we are going to discuss…

The Model

The Model provides a “model” for the data that is going to be exposed to the View. Add a new class and call it “Model”. Then have the class implement the IModel interface.

using Common.Lib.Interfaces;

namespace Common.Lib.Models

{

    public class Model : IModel

    {

       

    }

}

Now if you were to compile the project, you get an error because we said that we would implement the interface but have not yet. Right click on the IModel interface text in the editor then select Implement Interface, then select Implement Interface on the sub menu.

 

Implement  interface

Implement interface

Once you do that, Visual Studio will write a skeleton implementation for all of the methods in the IModel interface. In this case, Visual Studio will create a method named GetData taking a string as a parameter and returning a DataSet. Go ahead and delete the throwing of the not implemented exception. So you should end up with something like this:

using Common.Lib.Interfaces;

namespace Common.Lib.Models

{

    public class Model : IModel

    {

 

        #region IModel Members

 

        public System.Data.DataSet GetData(string filename)

        {

 

        }

 

        #endregion

    }

}

To fill the method in, we add the following code:

            string connectionString =

                @”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\csvfiles\;Extended Properties=’text;HDR=Yes'”;

            string commandText = string.Format(“Select * from {0}”, filename);

            OleDbConnection conn = new OleDbConnection(connectionString);

            OleDbCommand cmd = new OleDbCommand(commandText, conn);

 

            conn.Open();

            OleDbDataAdapter da = new OleDbDataAdapter(commandText, conn);

            DataSet JobData = new DataSet();

            da.Fill(JobData);

            return JobData;

This is going to some of the hard work for us. Of course you are free to get the data from any source that you want to. Basically all we are doing here is using OLEDB to open a file in the csvfiles directory on the C drive and filling a DataSet with the data in the file. Of courses for production code you would check for errors and take appropriate action but this is just an example. That’s all there is to do for the Model.

This is a zip with the project so far. Rename the .doc to .zip before you open. This is a wordpress requirement.  modelszip

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: