Tuesday, November 9, 2010

Eco Silverlight–client and server side–how is it supposed to work

The question on the forum was “So what is on the Eco Service side? What are the steps going to be to make a SL Eco project work? Is there a description anywhere of what the plans are for this to work?”

And this is the answer as it stands today.

There is no Silverlight wizard available (yet) so you do need some hacking to get started – just copy stuff from here…

(Everything here is for Silverlight 4)

#1 Pick New Solution and create a new EcoProject in dll using the standard wizard, also choose to have a Server for persistence – I took Windows application below:

Enterprise core objects

#2 Right-click the solution and pick “Add new Project”  - Pick a Silverlight Application (standard on all options you need to set):

image

#3 Right—click solution and pick “Add new project” again – this time pick a Silverlight Class Library – Name it SLEcoSpaceAndModel

#4 Delete the default Class1 in SLEcoSpaceAndModel and add “Existing Item” – we are going to add a link to the EcoProject1.ecomdl file. Make sure it is a link and not a copy:

image

When we have gotten this far you can test that the “Modlr plugin – CodeGen function” has been extended to find ecomdl file links in Silverlight projects. This shows by modeling a few classes and clicking update code:

Enterprise core objects Silverlight and server side

You will find that the generated code is also maintained as links in the SLEcoSpaceAndModel project – this is only due to the fact that this project contains a file link to the EcoProject1.ecomdl file.

We want to have it this way since we cannot reuse the EcoProject1.Model assembly in a Silverlight application since it is NOT a Silverlight project (EcoProject1.Model is not SL) – and we do not want it to be a Silverlight project since we want to let our server side code – and possibly other consumers – share a normal .net assembly (not Silverlight).

#5 We now need an EcoSpace instance in Silverlight – I will add it in the SLEcoSpaceAndModelAssembly – you could add it in an assembly of its own – but there is not much point unless you will have multiple Silverlight frontends that all are going to use a common model.

I suggest that you just copy much of this code into a new file:

   1: namespace SLEcoSpaceAndModel
   2: {
   3:     using System;
   4:     using System.Collections;
   5:     using System.Collections.Generic;
   6:     using System.Linq;
   7:     using Eco.Handles;
   8:     using Eco.Linq;
   9:     using Eco.Services;
  10:     using Eco.UmlCodeAttributes;
  11:     using EcoProject1;
  12:  
  13:     [EcoSpace]
  14:     [UmlTaggedValue("Eco.InitializeNullableStringsToNull", "true")]
  15:     [UmlTaggedValue("Eco.GenerateMultiplicityConstraints", "true")]
  16:     public partial class EcoSpace1 : Eco.Handles.DefaultEcoSpace
  17:     {
  18:         private static ITypeSystemService typeSystemProvider;
  19:  
  20:         /// <summary>
  21:         /// THIS IS HOW YOU CONTROL WHAT PACKAGES TO USE, Add more if needed
  22:         /// protected PackageType IncludeEcoPackage_NAMESPACE_PackageType
  23:         /// </summary>
  24:         protected EcoProject1Package IncludeEcoPackage_EcoProject1_EcoProject1Package;
  25:  
  26:         private Eco.Wcf.Client.PersistenceMapperWCFClient persistenceMapperClient1;
  27:  
  28:         public EcoSpace1()
  29:             : base()
  30:         {
  31:             this.persistenceMapperClient1 = new Eco.Wcf.Client.PersistenceMapperWCFClient();
  32:             this.persistenceMapperClient1.Uri = "http://localhost:8000/EcoProject1WinFormServer";
  33:             //  this.PersistenceMapper = this.persistenceMapperClient1; --- This does not work yet
  34:         }
  35:  
  36:         /// <summary>
  37:         /// Persist all changes to the domain objects.
  38:         /// </summary>
  39:         /// <remarks>
  40:         /// This function persists all changes to the eco space, including object creation,
  41:         /// object manipulation, changed associations and object deletions. After invoking this method
  42:         /// all undo information is removed.
  43:         /// If the application does not have any persistence layer defined the operation does nothing.
  44:         /// </remarks>
  45:         public void UpdateDatabase()
  46:         {
  47:             if ((Persistence != null) && (DirtyList != null))
  48:             {
  49:                 Persistence.UpdateDatabaseWithList(DirtyList.AllDirtyObjects());
  50:             }
  51:         }
  52:  
  53:         public static new ITypeSystemService GetTypeSystemService()
  54:         {
  55:             if (typeSystemProvider == null)
  56:             {
  57:                 lock (typeof(EcoSpace1))
  58:                 {
  59:                     if (typeSystemProvider == null)
  60:                         typeSystemProvider = MakeTypeService(typeof(EcoSpace1));
  61:                 }
  62:             }
  63:             return typeSystemProvider;
  64:         }
  65:  
  66:         protected override ITypeSystemService GetTypeSystemProvider()
  67:         {
  68:             return EcoSpace1.GetTypeSystemService();
  69:         }
  70:     }
  71: }

You need to add references to ECO Silverlight assemblies, to all the Silverlight assemblies that will use ECO:

image

And The SilverlightApplication assembly needs also a reference to the SLEcoSpaceAndModel assembly.

Then we can initialize an EcoSpace instance, I choose to do it in the MainPage.xaml.cs:

   1: private SLEcoSpaceAndModel.EcoSpace1 _ecoSpace;
   2: public MainPage()
   3: {
   4:     InitializeComponent();
   5:     _ecoSpace = new SLEcoSpaceAndModel.EcoSpace1();
   6:     _ecoSpace.Active = true;
   7:     DequeuerSL.Active = true;

And to test things out I use a ViewModelUserControl:

   1: <UserControl x:Class="SilverlightApplication2.MainPage"
   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   4:     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   5:     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   6:     xmlns:ecovm="clr-namespace:Eco.ViewModel.Silverlight;assembly=Eco.Silverlight"
   7:     mc:Ignorable="d"
   8:     d:DesignHeight="300" d:DesignWidth="400">
   9:  
  10:     <Grid x:Name="LayoutRoot" Background="White">
  11:         <StackPanel>
  12:             <Button Click="Button_Click">
  13:                 <TextBlock Text="Add C1"></TextBlock>
  14:             </Button>
  15:             <Button Click="Button_Click_1">
  16:                 <TextBlock Text="Add C2"></TextBlock>
  17:             </Button>
  18:             <ecovm:ViewModelSLUserControl x:Name="ViewModelUserControl" 
  19:                                           ViewModelName="ViewModel1" ></ecovm:ViewModelSLUserControl>
  20:         </StackPanel>
  21:     </Grid>
  22: </UserControl>

And when using rendered ViewModels we also need to init them:

   1: ViewModelDefinitionsInApplication.Init(_ecoSpace);
   2: ViewModelUserControl.SetEcoSpace(_ecoSpace);

Implement the button event handlers:

   1: private int x = 0;
   2: private void Button_Click(object sender, RoutedEventArgs e)
   3: {
   4:     x++;
   5:     new Class1(_ecoSpace) { Attribute1 = "c1" + x.ToString() };
   6: }
   7:  
   8: private void Button_Click_1(object sender, RoutedEventArgs e)
   9: {
  10:     x++;
  11:     new Class2(_ecoSpace) { Name = "c2" + x.ToString() };
  12: }

And the result is a silverlight app that shares model and user code implementations with the server:

image

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

 
Contact Us | Terms of Use | Privacy Statement © 2009 CapableObjects