Sellthrough System Implemented Using MEF + mvvm light + WCF Ria Service

Source: Internet
Author: User
Tags create domain

On csdn, I promised to share a project using Silverlight + mvvm + MEF. It's been over a month and I haven't started yet...

I tried to refer to a lot recently.ArticleAnd examples. Today we will share with you a simple distributed through system and sales management.

CodeDownload: http://www.n-pei.com/download/Nicholas.SilverlightSellThrough.zip

Mvvm framework used:

Http://mvvmlight.codeplex.com/

If you have no idea about MEF, we suggest you look at it here:

Http://mef.codeplex.com/

Introduction to the development environment:

Vs2010 flagship English version.

Ms SQL server2008.

What are the most basic primary brands of a retail company? The first is the product. Yes, it is the foundation. Then there are shops, sales, price, dealer, and regions around it (mainly province and city ). OK. With these basic ideas, we can look at their relationships.

 

Data Module Creation (Entity Data Model)

First, we need to create an empty ADO. NET Entity Data Model, and then add the above model.

 

If you haven't had time to learn Entity Framework 4, I suggest you start learning the basics from here.

After the above sellthrough. edmx is created, we will add the model and their relationships.

Right-click the blank area in design mode and click Add to view the page for adding a model. We will add a model named products.

 

Next, add attribute fields for the model products. It should mainly include code, name, launchdate, createuser, and createdate. After adding the model to the above:

The next step is to create other tables, all of which are centered on products. The specific creation process is not described here.

After creating models, we need to create a correspondence between models. The following example describes how to create a 1: n relationship between a province and a city:

Right-click the blank area of the entity model and choose 'add-> association', for example:

 

Select the first entity and the second entity. The default value is 1: N. Then, select OK.

The final Entity Data Model is as follows:

 

What about the database? We have no databases. You still need to find a blank space, right-click, and select 'generate database from model... ', Such:

 

Follow the prompts to perform step-by-step operations to generate sellthrough. edmx. SQL. When selecting a database, note that because we need to create a new database, select new connection:

 

Enter the database name 'shellpass', and prompt whether to add a new database. Select Yes:

 

After the preceding operations are completed, the database script is automatically generated. For example:

 

Next, open the script in SQL Server manage studio and execute the script. The database is OK.

 

Domain Service.

After creating the database, we will create domain services.

Click this project, select Add, find the domain service control, name it sellthroughservice, and click Next:

 

Select these entities and click OK to create domain service.

Here, the work on the server is basically complete.

Next, let's take a look at how to design the client structure.

 

Nicolas. silverlightsellthrough. Data is an extension of the server data source. the WCF Ria service in the data. Web project is connected to this project. It also includes some extended verification methods.

For example, for product, we need to have the field verification function. These functions are written in Nicholas. silverlightsellthrough. Data.

 

Nicholas. silverlightsellthrough. Common stores some common methods and interfaces.

For example, the model interface isellthroughmodel. CS.

Viewmodeltypes. CS defines all viewmodels in constant mode. This facilitates the operations on the viewmodel part of the project team.

Nicholas. silverlightsellthrough. Model is the model part defined for the mvvm mode.

 

Currently, we first define the addnewproduct method, removeproduct, and savechangeasync to add, delete, and modify products.

Nicholas. silverlightsellthrough. viewmodel: The viewmodel section in the mvvm mode is defined in this project.

Currently, I have defined only three viewmodels:

 

They are used to display all products, add products, and modify products.

The last one is Nicholas. silverlightsellthrough: Obviously, it is the part where the view is stored in mvvm. You can put all the views in the view folder.

 

After learning about the entire architecture,

I would like to introduce you to several mvvm lightConcept:

1. relaycommand:

Silverlight 4 has the command concept, making the mvvm mode easier. You only need to define your relaycommand in viewmodel, and then bind the relaycommand defined in viewmodel through command in view.

2. Messager

There is a Messager class in mvvm light toolkit, which enables viewmodel to communicate with the view. In this project, we define the appmessages class as a common communication class.

 

  Using  System;
Using System. IO;
Using System. Windows. Media. imaging;
Using Galasoft. mvvmlight. messaging;
Using Nicolas. silverlightsellthrough. Data. Web;

Namespace Nicolas. silverlightsellthrough. Common
{
/// <Summary>
/// Class that defines all messages used in this application
/// </Summary>
Public Static Class Appmessages
{
Enum Messagetypes
{
Raiseerror,
Pleaseconfirm,
Editproduct,
Submitchanges,
Statusupdate,
Cancelchanges,
Expriedproduct
}

Public Static Class Raiseerrormessage
{
Public Static Void Send (exception ex)
{
Messenger. Default. Send < Exception > (EX, messagetypes. raiseerror );
}

Public Static Void Register ( Object Recipient, Action < Exception > Action)
{
Messenger. Default. Register < Exception > (Recipient, messagetypes. raiseerror, action );
}
}

Public Static Class Pleaseconfirmmessage
{
Public Static Void Send (dialogmessage)
{
Messenger. Default. Send < Dialogmessage > (Dialogmessage, messagetypes. pleaseconfirm );
}

Public Static Void Register ( Object Recipient, Action < Dialogmessage > Action)
{
Messenger. Default. Register < Dialogmessage > (Recipient, messagetypes. pleaseconfirm, action );
}
}

Public Static Class Statusupdatemessage
{
Public Static Void Send (dialogmessage)
{
Messenger. Default. Send < Dialogmessage > (Dialogmessage, messagetypes. statusupdate );
}

Public Static Void Register ( Object Recipient, Action < Dialogmessage > Action)
{
Messenger. Default. Register < Dialogmessage > (Recipient, messagetypes. statusupdate, action );
}
}


Public Static Class Editproductmessage
{
Public Static Void Send (products product)
{
Messenger. Default. Send < Products > (Product, messagetypes. editproduct );
}

Public Static Void Register ( Object Recipient, Action < Products > Action)
{
Messenger. Default. Register < Products > (Recipient, messagetypes. editproduct, action );
}
}

Public Static Class Submitchangesmessage
{
Public Static Void Send ()
{
Messenger. Default. Send < Boolean > ( True , Messagetypes. submitchanges );
}

Public Static Void Register ( Object Recipient, Action < Boolean > Action)
{
Messenger. Default. Register < Boolean > (Recipient, messagetypes. submitchanges, action );
}
}

Public Static Class Cancelchangesmessage
{
Public Static Void Send ()
{
Messenger. Default. Send < Boolean > ( True , Messagetypes. cancelchanges );
}

Public Static Void Register ( Object Recipient, Action < Boolean > Action)
{
Messenger. Default. Register < Boolean > (Recipient, messagetypes. cancelchanges, action );
}
}

Public Static Class Expriedproduct
{
Public Static Void Send ()
{
Messenger. Default. Send < Boolean > ( True , Messagetypes. expriedproduct );
}

Public Static Void Register ( Object Recipient, Action < Boolean > Action)
{
Messenger. Default. Register < Boolean > (Recipient, messagetypes. expriedproduct, action );
}
}
}
}



 

 

3. icleanup interface:

When a view is displayed, the cleanup method is called to clear the data:

 

In ViewShow all products:

Insert several product data records in the database:

 

The purpose is to display the data in the DataGrid in mvvm mode.

The first step is to add the getallproductsasync Method to the model:

 
Public void getallproductsasync ()
{
Explain mquery <Products> (context. getproductsquery (), getallproductscomplete );
}

 

Step 2: Add allproductsviewmodel to the viewmodel project:

 

The public commands section uses the relaycommand method, submitcommand, and cancelcommand mentioned above. It serves to add several

The submit button and the cancel button are written in the background events.

The load products code is in the viewmodel constructor:

 

Note that there is a selectionchangedcommand. It can be seen from the name that it is mainly used to let the view page know which product is selected currently.

The third and last step is to add a view:

Create a view and add a DataGrid. Data Binding is as follows:

 

Note that there is a trigger in the middle. When the selectionchanged event occurs, run the selectionchangedcommand method in viewmodel.

So far, we have not used MEF, and it is our turn to debut. It makesProgramThe scalability is really very good.

In the View background code. CS file, you must first use MEF to import allproductsviewmodel To The View:

 
[Import (viewmodeltypes. allproductsviewmodel)]
Public object viewmodel
{
Set
{
Datacontext = value;
}
}

 

Then, use compositioninitializer In the constructor to load the viewmodel to the page.

If (! Viewmodelbase. isindesignmodestatic)
{
// Use MEF to load the view Model
Compositioninitializer. satisfyimports (this );
}

 

Do not forget to use the cleanup interface to remove the current viewmodel from the Messager pipeline.

Run the following command to check the effect:

 

The trigger is used to set the selected product. We can use this trigger to show its details when you select a row of data, and we can edit it, that is, editproduct.

It is also the code for adding the model, the code for modelview, And the last view.

Effect:

 

After modification, save the information and you can synchronously update it to the server.

The page is added:

 

You can refer to the providedSource code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.