2013-03-09 2 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에서는 당연히 광고를해야합니다. DAL에있는 IDictionary에서 NewDictionary를 파생했기 때문에 dAL을 참조로 사용했습니다. 하지만 GUI와 DAL을 서로 분리하려고합니다.

IDictionary 개체를 만드는 것과는 별도로 어떻게 할 수 있습니까? 질문이 명확하길 바란다.

+0

IDictionaryList에서 파생 된 문제는 무엇입니까? 참조 문제가 있다면 계약 인터페이스를 다른 프로젝트로 옮기십시오. 같은 핵심/프레임 워크 또는 기본 –

+0

당신의 답변을 주셔서 감사합니다,하지만 난 그런 식으로하고 싶지 않아. 참조를 추가 할 필요가 없거나 인터페이스를 내 GUI 프로젝트로 옮기는 방법을 찾고 있습니다. 그렇지 않으면 이미 할 수 있습니다. – ertan2002

답변

1

어느 누구도 서로를 참조 할 수없고 제 3 자 계약서를 참조 할 수 없다는 조건하에; 유일한 논리적 인 해결책은 그것을 도메인의 변화로 처리하는 것입니다. 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

데이터 계약을 조회했습니다 서비스와 클라이언트 간의 정식 계약입니다. th 은 교환 할 데이터를 추상적으로 설명합니다. 즉, 으로 통신하려면 클라이언트와 서비스가 동일한유형을 공유 할 필요는 없으며 동일한 데이터 계약 만 공유하면됩니다. 데이터 계약은 각 매개 변수 또는 반환 유형에 대해 교환 할 데이터가 (XML로 변환 됨)으로 serialize 된 데이터를 정의합니다.