简体   繁体   English

C#中的N层架构

[英]N-Layer Architecture in C#

I'm trying to implement N-layer architecture to my project first time. 我正在尝试第一次在我的项目中实现N层体系结构。

I created BLL, DAL and GUI 我创建了BLL,DAL和GUI

Here is in 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);

here is in BLL 这是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; }  
}

and here is in DAL 这是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; }
}

so, in GUI, naturally it needs to add DAL as reference because I derived NewDictionary from IDictionary that is in DAL. 因此,在GUI中,自然需要添加DAL作为参考,因为我是从DAL中的IDictionary派生NewDictionary的。 But I want to seperare GUI and DAL each other. 但是我想将GUI和DAL彼此分开。

apart from creating an IDictionary object, how can I do it? 除了创建IDictionary对象之外,我该怎么做? I hope the question is clear 我希望问题清楚

Under the condition that neither can reference each other, and no 3rd party reference of contracts; 在彼此之间不能相互参照,没有第三方合同参照的条件下; the only logical solution is to deal with it as a change in domain. 唯一的逻辑解决方案是将其视为域的更改。 You could use a DataContract and a DataContractSerialiser to help you. 您可以使用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);
        }
    }

here you have effectively the same object defined in (pretend) two libraries.. FooA and FooB 在这里,您实际上具有在(假装)两个库中定义的相同对象。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);

    }

Look up Windows Communication Foundation 查找Windows Communication Foundation

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged. 数据合同是服务与客户端之间的正式协议,抽象地描述了要交换的数据。 That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. 也就是说,进行通信时,客户端和服务不必共享相同的类型,而只需共享相同的数据合同。 A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged. 数据协定为每个参数或返回类型精确定义了要交换的序列化数据(转换为XML)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM