网络服务数据-> SQL-最佳做法? [英] webservice data -> sql - best practice?

查看:72
本文介绍了网络服务数据-> SQL-最佳做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要一些有关如何将Web服务响应数据最有效地放入数据库(结构和值)的建议.它应该很快并且可能很脏(例如,没有DAL等).目前,我只是在玩一些Web服务,在实现最终解决方案之前比较数据质量.我的第一种方法是直接在生成的Reference.cs中进行一些映射.我想知道的一点是,我感兴趣的数据广泛分布在Simple.AmazonECS.Item(又称为Item)属性的子类中.虽然我的解决方案适用于Item类中的基本类型(请参见未注释的[Attributes]),但对于这些复杂类型却没有好处.对于一个Web服务来说可能会发生变化,而我必须使用几种服务来做到这一点,那么将响应放入数据库的最佳方法就是什么.我查看了一些项目,例如xsd2db,但没有一个项目适用于webservices模式(从wsdl中提取).从Web服务xml架构到数据库架构必须有一种简单的方法...

PS:这可能是问题的一部分:所有类都从对象继承!我认为实体框架不支持此功能,对吧?

有什么建议吗?

Hey guys,

I need some advice on how to put a data of a webservice response the most efficent way into a database (structure and values). It should be quick and may be dirty (e.g. no DAL, etc.). At the moment i am just playing around with some webservices, comparing data quality before implementing a final solution. My first approach was to do some mapping in the generated Reference.cs directly. For a pitty i figured out, that the data i am interested in is spread widely in subclasses of the Simple.AmazonECS.Item (further called Item) properties. While my solution works on basic types in the Item class (see the uncommented [Attributes]), it is no good for these complex types. For a webservice may change and i have to do this with several services, what would be the best way to get the respons into a database. I looked at some projects like xsd2db, but none worked for the webservices schema (extracted from wsdl). There must be an easy way to get from a webservice xml schema to a database schema...

PS: this may be part of the problem: all classes are inherited from object! i think this is not supported by the entity framework, right?

Any suggestions?

// This ist the auto generated reference.cs of the Amazon Webservice 
// http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl

namespace Simple.Amazon.ECS
{
    // [...] Enumerations, Classes and other stuff
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
    [System.SerializableAttribute]
    [System.Diagnostics.DebuggerStepThroughAttribute]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://webservices.amazon.com/AWSECommerceService/2009-07-01")]
    public partial class Image : object, System.ComponentModel.INotifyPropertyChanged
    {
        #region Fields

        private string uRLField;
        //[...]

        #endregion Fields

        #region Events

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        #endregion Events

        #region Properties

        [System.Xml.Serialization.XmlElementAttribute(Order=0)]
        [Column(Name = "URL", Storage = "uRLField", AutoSync = AutoSync.OnInsert, IsPrimaryKey= true)]
        [DataMember(Order = 0)]
        public string URL
        {
            get {
                return this.uRLField;
            }
            set {
                this.uRLField = value;
                this.RaisePropertyChanged("URL");
            }
        }
        //[...]

        #endregion Properties

        #region Methods
        
        protected void RaisePropertyChanged(string propertyName)
		//[...]

        #endregion Methods
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
    [System.SerializableAttribute]
    [System.Diagnostics.DebuggerStepThroughAttribute]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://webservices.amazon.com/AWSECommerceService/2009-07-01")]
    // my approach
    //[Table(Name = "dbo.AmazonItems")]
    //[DataContract()]
    public partial class Item : object, System.ComponentModel.INotifyPropertyChanged
    {
        #region Fields

        private string aSINField;
        private Image largeImageField;
        private ItemAttributes itemAttributesField;
		//[...]

        #endregion Fields

        #region Events

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        #endregion Events

        #region Properties

        [System.Xml.Serialization.XmlElementAttribute(Order=0)]
        // my approach - this works for the basic type
        //[Column(Name = "ASIN",Storage="aSINField", AutoSync = AutoSync.OnInsert, IsPrimaryKey = true)]
        //[DataMember(Order=0)]
        public string ASIN
        {
            get {
                return this.aSINField;
            }
            set {
                this.aSINField = value;
                this.RaisePropertyChanged("ASIN");
            }
        }

        [System.Xml.Serialization.XmlElementAttribute(Order=10)]
        // my approach - this crashes - unknown sql type - the data i am inteseted in
        //[Column(Name = "ItemAttributes", Storage = "itemAttributesField", AutoSync = AutoSync.OnInsert, DbType="SQL_VARIANT")]
        //[DataMember(Order = 10)]
        public ItemAttributes ItemAttributes
        {
            get {
                return this.itemAttributesField;
            }
            set {
                this.itemAttributesField = value;
                this.RaisePropertyChanged("ItemAttributes");
            }
        }

        [System.Xml.Serialization.XmlElementAttribute(Order=8)]
        // my approach - this crashed to - complex type
        //[Column(Name = "LargeImage", Storage = "largeImageField", AutoSync = AutoSync.OnInsert)]
        //[DataMember(Order = 8)]
        public Image LargeImage
        {
            get {
                return this.largeImageField;
            }
            set {
                this.largeImageField = value;
                this.RaisePropertyChanged("LargeImage");
            }
        }

        //[...] more Classes, Events, ...



// well, here is what i am doing in Main()

// create a WCF Amazon ECS client
BasicHttpBinding binding		= new BasicHttpBinding(BasicHttpSecurityMode.Transport);
binding.MaxReceivedMessageSize	= int.MaxValue;
AWSECommerceServicePortTypeClient client = new AWSECommerceServicePortTypeClient(
    binding,
    new EndpointAddress("https://webservices.amazon.com/onca/soap?Service=AWSECommerceService"));

// add authentication to the ECS client
client.ChannelFactory.Endpoint.Behaviors.Add(new AmazonSigningEndpointBehavior(accessKeyId, secretKey));

// prepare an ItemSearch request
ItemSearchRequest request1	= new ItemSearchRequest();

request1.SearchIndex			= "All";
request1.DeliveryMethod = DeliveryMethod.Ship;
request1.MerchantId = "All";
request1.Condition = Condition.All;
request1.ResponseGroup = new string[] {"Large"};
request1.Keywords = "test";

// prepare the search
ItemSearch itemSearch = new ItemSearch();
itemSearch.AssociateTag = "";
// batch the two requests together
itemSearch.Request = new ItemSearchRequest[] { request1, request2 };
itemSearch.AWSAccessKeyId = accessKeyId;            
            
// issue the ItemSearch request
ItemSearchResponse response = client.ItemSearch(itemSearch);

// issue the DataContext on SQL 2008 R2 server
DataContext db = new DataContext(@"Data Source=./;Database=test;Integrated Security=SSPI;");

// a Table of the Simple.ECS.Amazon.Item class
Table<item> it = db.GetTable<item>();

// drop the test DB
db.DeleteDatabase();
// and recreate the structure it to see if it works
// this ist where errors occure 
// for unknown sql types (complex types like Simple.Amazon.ECS.Image)
db.CreateDatabase();

foreach (var responses in response.Items)
{
    foreach (var item in responses.Item)
    {
        it.InsertOnSubmit(item);
    }
}
db.SubmitChanges();
db.Connection.Close();                                                
</item></item>

推荐答案

在理解代码后,我注意到您的数据合同是在"Item"对象中定义的,该对象通过Web服务和数据库操作使用. >
如果您不想使用DAL层,则可以使用LInQ to Entity框架,该框架已被DB操作证明.您可以将自动生成的"Item"对象映射到Entity对象.然后就完成了.
On understanding the code, I noticed your datacontract is defined in ''Item'' object, which is used through out web service and database operations.

If you don''t want to use DAL layer, you can use LInQ to Entity framework which is well proven for DB operations. You can map the auto generated ''Item'' object into Entity object. Then you are done.


这篇关于网络服务数据-&gt; SQL-最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆