0

我想我现在掌握了大部分知识,但遗憾的是我找不到任何可以真正帮助我的教程,因为大多数人都在使用 SQL 连接,而我正在使用实体框架。

到目前为止,我创建了一个名为 Farve 的 CSLA 业务类和一个名为 FarbeListe 的 CSLA 业务列表类。在我的 xaml 中,我有一个列出模型的网格视图。但我现在不知道如何获取数据,但我确信我已经很接近了。请帮我举一个简单的例子,不要发布任何教程。我想我现在最了解他们,但他们仍然没有帮助。

这是我的 csla 类 Farbe

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Csla;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using MusterConnectionDB.Datenbank;

namespace MusterConnectionDB.Business
{
    [Serializable]
    public class Farbe : Csla.BusinessBase<Farbe>
    {

        TestDBEntities db = new TestDBEntities();

        public Farbe()
        {
            BusinessRules.AddRule(new Csla.Rules.CommonRules.Required(FarbauswahlNrProperty));
            BusinessRules.AddRule(new Csla.Rules.CommonRules.MinValue<int>(FarbauswahlNrProperty, 3));


        }

        #region Properties

        public static readonly PropertyInfo<int> FarbauswahlNrProperty = RegisterProperty<int>(c => c.FarbauswahlNr);
        public int FarbauswahlNr
        {
            get { return GetProperty(FarbauswahlNrProperty); }
            set
            {

                SetProperty(FarbauswahlNrProperty, value);
            }
        }


        public static readonly PropertyInfo<string> KurztextProperty = RegisterProperty<string>(c => c.Kurztext);
        public string Kurztext
        {
            get { return GetProperty(KurztextProperty); }
            set { SetProperty(KurztextProperty, value); }
        }

        public static readonly PropertyInfo<string> RessourceProperty = RegisterProperty<string>(c => c.Ressource);
        public string Ressource
        {
            get { return GetProperty(RessourceProperty); }
            set { SetProperty(RessourceProperty, value); }
        }

        public static readonly PropertyInfo<bool> Vari1Property = RegisterProperty<bool>(c => c.Vari1);
        public bool Vari1
        {
            get { return GetProperty(Vari1Property); }
            set { SetProperty(Vari1Property, value); }
        }

        public static readonly PropertyInfo<string> Vari2Property = RegisterProperty<string>(c => c.Vari2);
        public string Vari2
        {
            get { return GetProperty(Vari2Property); }
            set { SetProperty(Vari2Property, value); }
        }

        #endregion

        #region Synchronous Factory Methods

        public class DataEventArgs : EventArgs
        {
            public DataEventArgs(Farbe data)
            {
                this.Data = data;
            }

            public Farbe Data { get; set; }
        }

        internal static Farbe New()
        {
            return DataPortal.CreateChild<Farbe>();
        }


        internal static Farbe Get(Farbe data)
        {
            if (data == null)
                return null;

            return DataPortal.FetchChild<Farbe>(data);
        }


        #endregion

        #region DataProtal Methods

        


        private void Child_Update()
        {
            using (var ctx = Csla.Data.ObjectContextManager<TestDBEntities>.GetManager(EntitiesDatabase.Name))
            {
                var data = ctx.ObjectContext.Farben.SingleOrDefault(e => e.FarbauswahlNr == this.FarbauswahlNr);

                data.Kurztext = ReadProperty<string>(KurztextProperty);
                data.Ressource = ReadProperty<string>(RessourceProperty);
                data.Var1 = ReadProperty<bool>(Vari1Property);
                data.Vari2 = ReadProperty<string>(Vari2Property);
                ctx.ObjectContext.SaveChanges();

            }
        }

        private void Child_Insert()
        {
            using (var ctx = Csla.Data.ObjectContextManager<TestDBEntities>.GetManager(EntitiesDatabase.Name))
            {

                try
                {
                    var data = new Datenbank.Farbe();
                    data.Kurztext = ReadProperty<string>(KurztextProperty);
                    data.Ressource = ReadProperty<string>(RessourceProperty);
                    data.Var1 = ReadProperty<bool>(Vari1Property);
                    data.Vari2 = ReadProperty<string>(Vari2Property);
                    ctx.ObjectContext.Farben.AddObject(data);
                    ctx.ObjectContext.SaveChanges();
                }
                catch (Exception e)
                {

                    MessageBox.Show(e.ToString());
                }


            }
        }

        private void Child_Delete()
        {
            
            using (var ctx = Csla.Data.ObjectContextManager<TestDBEntities>.GetManager(EntitiesDatabase.Name))
            {
                try
                {
                    var data = ctx.ObjectContext.Farben.SingleOrDefault(e => e.FarbauswahlNr == this.FarbauswahlNr);
                    ctx.ObjectContext.Farben.DeleteObject(data);
                    ctx.ObjectContext.SaveChanges();
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());

                }
            }
        }


        
        #endregion

    }
}

现在我的 CLSA 清单 klass FarbeListe

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Csla;
using MusterConnectionDB.Business;

namespace MusterConnectionDB.Business
{
    public class FarbeListe : Csla.BusinessListBase<FarbeListe, Farbe>
    {
        public FarbeListe()
        {
            
        }


        protected override void Child_Create()
        {
            base.Child_Create();
        }

        private void Child_Fetch(IEnumerable<Farbe> data)
        {

            RaiseListChangedEvents = false;

            foreach (var item in data)
            {
                this.Add(Farbe.Get(item));

            }

            RaiseListChangedEvents = true;
        }

        internal static FarbeListe Get(IEnumerable<Farbe> data)
        {
            if (data == null)
                return null;

            return DataPortal.FetchChild<FarbeListe>(data);
        }

        internal static FarbeListe New()
        {
            return DataPortal.CreateChild<FarbeListe>();
        }

        internal static FarbeListe GetAll()
        {

            return DataPortal.Fetch<FarbeListe>();
        }
    }

}

如何编写模型类 FarbeViewModel 的方法?我只需要获取属性模型的数据。

public void ExecuteAktu(object obj)
{

  


}
4

1 回答 1

0

FarbeViewModel 的构造函数中的这一行

Model =  MusterConnectionDB.Business.FarbeListe.GetAll();

这在 FarbListe CSLA BusinessList 类中

    private void DataPortal_Fetch()
    {
        using (var ctx = Csla.Data.ObjectContextManager<Datenbank.TestDBEntities>.GetManager(EntitiesDatabase.Name))
            ReadData(ctx.ObjectContext.Farben);
    }

    private void ReadData(IEnumerable<Datenbank.Farbe> data)
    {
        // Partial Method BeforeReadData


        RaiseListChangedEvents = false;

        foreach (var item in data)
            this.Add(Farbe.Get(item));
        RaiseListChangedEvents = true;


    }

最后在商务舱 Farbe

    internal static Farbe Get(Datenbank.Farbe data)
    {
        try
        {
            if (data == null)
                return null;

            return DataPortal.FetchChild<Farbe>(data);
        }
        catch (System.Exception e)
        {
            MessageBox.Show(e.ToString()); 
        }
        return null;

    }

    private void Child_Fetch(Datenbank.Farbe data)
    {

        LoadProperty(FarbauswahlNrProperty, data.FarbauswahlNr);
        LoadProperty(KurztextProperty, data.Kurztext);
        LoadProperty(RessourceProperty, data.Ressource);
        LoadProperty(Vari1Property, data.Var1);
        LoadProperty(Vari2Property, data.Vari2);

    }

这就是我从数据库中读取数据的方式...... -.-' 简单吧?:D

于 2013-03-25T12:15:06.333 回答