2013-03-09 96 views
1

我试图首次将N层体系结构实现到我的项目中。C中的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中,自然需要广告d DAL作为参考,因为我从Dictionary中的IDictionary派生了NewDictionary。但我想分开GUI和DAL彼此。

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

+0

从IDictionaryList派生的问题是什么?如果它的引用问题,只需将您的合同接口移动到另一个项目;像核心/框架或基地 – 2013-03-09 11:36:35

+0

谢谢你的答案,但我不想这样做。我正在寻找我不需要添加引用/将界面移动到我的GUI项目中。否则,我已经可以做到了 – ertan2002 2013-03-09 11:39:22

回答

1

在既不能互相引用也没有合同的第三方引用的条件下;唯一合乎逻辑的解决方案是将其作为域中的变更来处理。您可以使用DataContract和DataContractSerialiser来帮助您。

Serialiser borrowed from here

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 Communication Foundation

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