How to use CslaDataProvider correctly in CSLA.NET 4.1.0.0

How to use CslaDataProvider correctly in CSLA.NET 4.1.0.0

Old forum URL: forums.lhotka.net/forums/t/10515.aspx


dpfxiangfei posted on Tuesday, July 12, 2011

      I like CSLA.NET and WPF very much. It's my first time to use CSLA.NET and WPF in product-like application development. Now i encounter a problem: I don't know how to use CslaDataProvider correctly.PLS give me some help.The code excerpt  as below:

   UI   XAML:

 <UserControl.Resources>

<Xaml:ErrorDialog x:Key="errorDlg" DialogTItle="Tips" />

<Xaml:CslaDataProvider x:Key="providerModuleList"   OjbectType="{x:Type Business:ModuleDynamicList}"

   FactoryMethod="GetModuleDynamicList"   IsInitialLoadEnabled="True"      IsAsynchronous="False"

  DataChangedHandler="{StaticResource errorDlg}"       ManageObjectLifetime="True"/>

<DataGrid  x:Name="list"  AutoGererateColumns="False"   AlternatingRowBackground="#D4D0C8" CanUserAddRows="True"  CanUserDeleteRows="True"    CanUserReorderColumns="True"

 CanUserResizeRows="False"   ItemsSource="{Binding Source={StaticResource providerModuleList}}">

<--  Columns Definitions---->

</DataGrid>

 

     Entity Definition:

       [serializable]

     public class ModuleDynamicList : DynamicListBase<Module>

   {

       public static ModuleDynamicList GetModuleDynamicList()

         {

           return     DataPortal.Fetch<ModuleDynamicList>();

         }

         private void DataPortal_Fetch()

         {

             var rlce=RaiseListChangedEvents;

             using(var ctx=ContextManager<SomeDataContext>.GetManager(someDataBaseName))

                  {

                     var moduleIds=from module in ctx.DataContext.TblKD_Module

                                                 select module.Module_ID;

                     foreach(var moduleId in moduleIds)

                      {

                         Add(Module.GetModule(moduleId));

                     }

                  }

         }

 

[Serializable]

public  class Module : BusinessBase<Module>

 {

      #region Property Definitions

   .............................

    #endregion

      public static Module GetModule(string moduleId)

        {

          return DataPortal.Fetch<Module>(moduleId);

        }

         private void DataPortal_Fetch(string moduleId)

           {

              using(Var ctx=ContextManger<SomeDataContext>.GetManager(someDataBaseName))

                {

                    Some Code to Get Linq Object then set Module property values

                }

           }

            [Transactional(TransactionalTypes.TransactionScope)]

              protected override void DataPortal_Update()

              {

                     using(var ctx=ContextManager<SomeDataContext>.GetManager(someDataBaseName))

                       {

                         TblKD_Module module=(from mod in ctx.DataContext.TblKD_Module

                                                                        where mod.Module_ID==Id

                                                                       select mod).FirstOrDefault();

                           if(module == null)

                              return;

                             module.Module_Order=OrderId;

                              <--  Set Other Properties -->

                             ctx.DataContext.SubmitChanges();

                             FieldManager.UpdateChildren(this);

                        }

             }

                <--     Other CRUD Operations Like   above DataPortal_Update        -->

}

 

    As you can see,I have define a class ModuleDynamicList subclasses  DynamicListBase,and contains a number of instances of class Module which subclasses BusnessBase.   use CslaDataProvider,I try  to bind ModuleDynamicList   to a DataGrid,want to have in-edit effect.  My problem is:  This UI can get CRUD properly,which means data can be saved  to database correctly,but when some errors happens during CRUD,the errorDialog doesn't show ,and after operation,the edited row in datagrid  cann't  edit anymore,the edited row  uneditable, just like seting the row's property IsReadonly as True.

        Please give me  some tips to help me out of this delimma. 

        TKS.

 

 

RockfordLhotka replied on Tuesday, July 12, 2011

I suggest that you avoid the CslaDataProvider. Microsoft seems to be moving away from the data provider application model, toward the use of the MVVM design pattern. CSLA .NET has followed Microsoft's lead in this area over the past couple years.

I left CslaDataProvider in the code for backward compatibility, but I recommend that you create viewmodel classes instead of using a data provider.

dpfxiangfei replied on Tuesday, July 12, 2011

  Thank you very much for your reply. It's a good suggestion.I will adopt the MVVM model next time.But in my current project,I still want to use the CslaDataProvider in some user Controls just for convenience. I really appreciate if you can give me some tips to solve the problem about using the CslaDataProvider that i mention before.

    TKS.

  

RockfordLhotka replied on Tuesday, July 12, 2011

Several of the samples from CSLA .NET 3.0 and maybe 3.5 use the CslaDataProvider. I recommend looking at those older samples to see how the control is used.

Copyright (c) Marimer LLC