开发者

OData Services and Silverlight Client Side using different object than the Server Side, can I do it?

I am working on WP7 app (based on ODataWPNorthwindSample from MSDN)using the ODATA v2 library with DataServiceState class In this example I have MainViewModel.cs class where

> DataServiceCollection<Customer> customers;
> 
>   public DataServiceCollection<Customer> Customers 
>         {
>             get { return this.customers; }
> 
>             private set
>             {
>                 this.customers = value;
>                 
>                 this.customers.LoadCompleted +=
> this.OnCustomersLoaded;
>                 this.customers.CollectionChanged += (o, e) => {
> this.NotifyPropertyChanged("Custome开发者_JAVA百科rsTitle"); };
> 
>                 this.NotifyPropertyChanged("Customers");
>                 this.NotifyPropertyChanged("CustomersTitle");
>             }
>         }

Where the Customer class has some attributes(very short version of it)is:

> public partial class Customer :
> global::System.ComponentModel.INotifyPropertyChanged
>     {
>         
>        
> [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design",
> "1.0.0")]
>         public static Customer CreateCustomer(string customerID,
> string companyName)
>         {
>             Customer customer = new Customer();
>             customer.CustomerID = customerID;
>             customer.CompanyName = companyName;
>             return customer;
>         }
> 
> [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design",
> "1.0.0")]
>         public string Address
>         {
>             get
>             {
>                 return this._Address;
>             }
>             set
>             {
>                 this.OnAddressChanging(value);
>                 this._Address = value;
>                 this.OnAddressChanged();
>                 this.OnPropertyChanged("Address");
>             }
>         }
>        
> [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design",
> "1.0.0")]
>         private string _Address;
>         partial void OnAddressChanging(string value);
>

     partial void OnAddressChanged();

For some reason I need to add one more attribute “AddressLocal” which will be used only on the client side and bind it to the XAML. The value of the “AddressLocal” I calculate based on “Address” attribute. So, if “Address” is changed “AddressLocal” will change also.

>  public string AddressLocal
>     {
>         get
>         {
>             return this._ AddressLocal;
>         }
>         set
>         {
>             this.On AddressLocal Changing(value);
>             this._AddressLocal = value;
>             this.OnAddressLocal Changed();
>             this.OnPropertyChanged("AddressLocal ");
>         }
>     }
>     
>     private string _AddressLocal;
>     partial void OnAddressLocal Changing(string value);
>     partial void OnAddressLocal Changed();

But this attribute does not exist in the database on the server and I don’t need them there, but when I call

> App.ViewModel.Context.BeginSaveChanges(OnSaveCompleted, null);
> 
> void OnSaveCompleted(IAsyncResult asyncResult)
>         {
>             Deployment.Current.Dispatcher.BeginInvoke(() =>
>             {
>                 try
>                 {
>                     App.ViewModel.Context.EndSaveChanges(asyncResult);
>                     this.StatusMessage.Text = "Successfully saved
> changes";
>                 }

It is give me an exceptions, because this DataServiceCollection customers; is different from the Objects Customers in the Context. I know it is probably happened because I did mistake in app architecture. My first thought was: to create an Oblect "CustomerLocal" and inherit from the Customer, but this does not work. Do you have any idea?


If you make your additional property internal the SaveChanges should ignore it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜