1

我第一次尝试在我的项目中实现 N 层架构。

我创建了 BLL、DAL 和 GUI

这是在 GUI

XmlSettingsBLL xmlSettings = new XmlSettingsBLL();

  var newDict = new NewDictionary()
  {
   StrDataSourceType = "AccessMdb",// DataSourceType.AccessMdb,
   DictionaryID = Guid.NewGuid().ToString(),
   FirstColumnName = "Kelime",
   SecondColumnName = "Karsilik",
   TableName = "kelimelerpro",
   LastShowedID = 0,
   Name = "kpds",
   Path = "kelimeler.mdb"
  };

  xmlSettings.AddNewDictionary(newDict);

这是在 BLL

public bool AddNewDictionary(NewDictionary list)
{
list.DatasourceType = (DataSourceType)Enum.Parse(typeof (DataSourceType), list.StrDataSourceType);

IDictionaryList newDictionary =list;

try
{
   helper.AddDictionary(newDictionary);
   return true;
}
catch
{
  return false;
}      
}

 public class NewDictionary : IDictionaryList
{
    public string Name { get; set; }
    public string Path { get; set; }
    public string DictionaryID { get; set; }
    public string TableName { get; set; }
    public int LastShowedID { get; set; }
    public string FirstColumnName { get; set; }
    public string SecondColumnName { get; set; }
    public DataSourceType DatasourceType { get; set; }
    public string StrDataSourceType { get; set; }  
}

这是在 DAL

 public void AddDictionary(IDictionaryList list)
 {
   var channelElem = xdoc.Element("MemorizeSettings");
   var dictionaries = channelElem.Element("Dictionaries"); 

   XAttribute[] attrs = new XAttribute[8];
   attrs[0] = new XAttribute("Name", list.Name);
   attrs[1] = new XAttribute("Path", list.Path);
   attrs[2] = new XAttribute("TableName", list.TableName);
   attrs[3] = new XAttribute("DatasourceType", Enum.GetName(typeof(DataSourceType),list.DatasourceType));
   attrs[4] = new XAttribute("LastShowedID", "0");
   attrs[5] = new XAttribute("FirstColumnName", list.FirstColumnName);
   attrs[6] = new XAttribute("SecondColumnName", list.SecondColumnName);
   attrs[7] = new XAttribute("DictionaryID", list.DictionaryID);

   var newdict = new XElement("Dictionary", attrs);

   dictionaries.Add(newdict);
   xdoc.Save(fileName);
 }

public interface IDictionaryList
{
     string Name { get; set; }
     string Path { get; set; }
     string DictionaryID { get; set; }
     string TableName { get; set; }
     int LastShowedID { get; set; }
     string FirstColumnName { get; set; }
     string SecondColumnName { get; set; }
     DataSourceType DatasourceType { get; set; }
}

因此,在 GUI 中,自然需要添加 DAL 作为参考,因为我从 DAL 中的 IDictionary 派生了 NewDictionary。但我想将 GUI 和 DAL 分开。

除了创建一个 IDictionary 对象,我该怎么做?我希望问题很清楚

4

1 回答 1

1

在双方不能相互引用、合同没有第三方引用的情况下;唯一合乎逻辑的解决方案是将其视为域的更改。您可以使用 DataContract 和 DataContractSerializer 来帮助您。

从这里借来的序列化器

    public static string Serialize(object obj)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
            serializer.WriteObject(memoryStream, obj);
            return Encoding.UTF8.GetString(memoryStream.ToArray());
        }
    }

    public static object Deserialize(string xml, Type toType)
    {
        using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
        {
            XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.UTF8, new XmlDictionaryReaderQuotas(), null);
            DataContractSerializer serializer = new DataContractSerializer(toType);
            return serializer.ReadObject(reader);
        }
    }

在这里,您实际上在(假装)两个库中定义了相同的对象.. FooA 和 FooB

    [DataContract(Name="Foo")]
    public class FooA
    {
        [DataMember] 
        public int Value;
    }

    [DataContract(Name = "Foo")]
    public class FooB
    {
        [DataMember]
        public int Value;
    }

    static public void Main()
    {
        var fooA = new FooA() {Value = 42};

        var serialised = Serialize(fooA);

        // Cross domain

        var fooB = (FooB) Deserialize(serialised, typeof(FooB));

        Console.WriteLine(fooB.Value);

    }

查找Windows 通信基础

数据契约是服务和客户端之间的正式协议,它抽象地描述了要交换的数据。也就是说,为了进行通信,客户端和服务不必共享相同的类型,只需相同的数据契约。数据契约为每个参数或返回类型精确定义了哪些数据被序列化(转换为 XML)以进行交换。

于 2013-03-09T11:55:31.827 回答