R.Tanaka.Ichiro's Blog

主にC# な話題です

目次

Blog 利用状況

ニュース

WCF の DataContract 属性のついたクラスの汎用性(その2)

WCF の DataContract 属性のついたクラスの汎用性
http://blogs.wankuma.com/rti/archive/2008/08/19/153479.aspx

の続きです。


「前回までのあらすじ」

アセンブリA(WcfServiceLibrary)
・アセンブリCを参照
・DataConstract 属性が付いたHogeClassを保有

アセンブリB(WcfClient)
・アセンブリCを参照
・アセンブリAをサービス参照することで HogeClass を利用

アセンブリC(Common Logic Library)
・ここでアセンブリAにあるHogeClass型を使いたい
・アセンブリCはアセンブリA,B から参照される

簡単に言うと、

DataConstract なオブジェクトを使いまわすには!?

ってことです。


その後、試してみた結果、どうやら インターフェイスなら DataContract 属性付きのクラスが継承できることがわかりました。

つまり、インターフェイスに格納した状態であれば DataContract 属性付きのオブジェクトを各アセンブリで使いまわして処理することができます。

こんな感じです。


namespace CommonLogicLibrary
{
  public interface IRow
  {
    int Code { get; set; }
    string Name { get; set; }
    int Age { get; set; }
  }
  public class Row
  {
    public IRow Row { get; set; }
    pubilc void ChangeAge() {
      this.Row.Age = 18;
    }
  }
}

namespace WcfServiceLibrary
{
  [DataContract]
  public class Row : CommonLogicLibrary.IRow
  {
    [DataMember] public int  Code { get; set; }
    [DataMember] public string Name { get; set; }
    [DataMember] public int  Age  { get; set; }
  }
}


これによって、 インスタンスの生成については、WcfServiceLibrary か、サービス参照した WcfClient 側で行って、共通した処理は、CommonLogicLibrary の Row オブジェクトに行わせることができます。

 

投稿日時 : 2008年8月27日 13:17

Feedback

# re: WCF の DataContract 属性のついたクラスの汎用性(その2) 2008/08/27 14:37 NAL-6295

私もインタフェースを利用する事で事なきを得てます。

# re: WCF の DataContract 属性のついたクラスの汎用性(その2) 2008/08/27 15:16 ghost_shell

あ・・・、書いたことある。
それが知りたかったのか・・・。

#RさんはWCFをすんなり使えますか?

# re: WCF の DataContract 属性のついたクラスの汎用性(その2) 2008/08/27 20:14 まどか

> アセンブリC(Common Logic Library)
> ・ここでアセンブリAにあるHogeClass型を使いたい
> ・アセンブリCはアセンブリA,B から参照される

これって循環参照ですよね。

前回もちょっと理解できなかったのですが
単純にWCFに関するサービスとクライアントで共有するものを
単独アセンブリに押し込めればいいだけのような気がしてなりません。
とんちんかんなこと言ってるかなぁ?

# re: WCF の DataContract 属性のついたクラスの汎用性(その2) 2008/08/28 11:28 R・田中一郎

NAL-6295 さん

>私もインタフェースを利用する事で事なきを得てます。

僕も、最初にインターフェイスを試してみるべきでした。
インターフェイスに対してコーディングするという原則を忘れていた証拠ですね。

DataContract属性付きクラスの継承に対応して欲しいです。
(できる方法があるのかなぁ)

----------------------------------------
ghost_shell さん

>#RさんはWCFをすんなり使えますか?

この問題以外は、今のところ特に不満はないですね。

----------------------------------------
まどか さん

>単純にWCFに関するサービスとクライアントで共有するものを
>単独アセンブリに押し込めればいいだけのような気がしてなりません。

この方法がわからないのですよ。
どうすれば良いのか教えていただきたいです。

DataContract属性付きのクラスを CommonLogic アセンブリにおいて、これを WcfServiceLibrary から継承することまでは可能です。
しかし、クライアント側で指定した値が正しく渡らないんですよね。

# re: WCF の DataContract 属性のついたクラスの汎用性(その2) 2008/08/28 13:06 NAL-6295

私の場合は、明示的には項目を持たないステータスくらいしかメンバのないインタフェースだけ用意して、ライブラリにインタフェースを実装したクラスと、テーブル情報だけ渡して、ライブラリ内で、実装したクラスに存在するプロパティを動的に解釈するようにしています。
そうすると、ライブラリ側は明示的な情報を知らなくても良いので都合が良かったんです。

# re: WCF の DataContract 属性のついたクラスの汎用性(その2) 2008/08/28 22:00 まどか

前回、なちゃさんが書かれていますが
自動生成したクライアントコードのうち、型宣言をすべてコメントアウトして
インターフェースとクライアントクラスだけを有効にします。
要は型をCommonのものを参照するようにします。
これで型の不一致エラーが無くなるはずだと思います。

# re: WCF の DataContract 属性のついたクラスの汎用性(その2) 2008/08/30 17:00 R・田中一郎

NAL-6295 さん

なるほど、ステータスで値の種別を把握して解釈させる訳ですね。
皆さん、この辺はいろいろやり繰りしているんですね。

----------------------------------------
まどか さん

いま一つわからないので教えていただきたいです。

>前回、なちゃさんが書かれていますが

は、

>プロキシを自動生成させなきゃ大丈夫です。

の部分で、これが

>自動生成したクライアントコードのうち、型宣言をすべてコメントアウトして

ってことであってます?
また、これはサービス参照時に作成された Reference.cs の Row 宣言全てをコメントアウトするということを指しますか?

>要は型をCommonのものを参照するようにします。

つまり、Commonライブラリのクラスを、Wcf クライアントアセンブリで利用するということですか?
この場合、シリアライズに関する部分も全て自分で行う必要があるということですよね?

うーむ。

# re: WCF の DataContract 属性のついたクラスの汎用性(その2) 2008/09/04 20:59 まどか

> Commonライブラリのクラスを、Wcf クライアントアセンブリで利用するということですか?

はい。
サービスとクライアントの双方から参照する共有ライブラリとします。
#双方に共通する情報のみのアセンブリ

> シリアライズに関する部分も全て自分で行う必要があるということですよね?

独自のものがあればそうだと思いますが
私の実績ではコメントアウト+型既定のシリアライザで動作しています。
#Nonコーディング

別の話ですが
Private _Text As String
Public Property Text As String
において
DataMemberをプロパティ側につけると""をNothingで受け取ってしまうという現象がありました。
フィールドにつけると正しく受け取れました。

# re: WCF の DataContract 属性のついたクラスの汎用性(その2) 2008/09/09 9:59 R・田中一郎

なるほど、とりあえずミニマムなソリューション作って検証してみることにします。
ありがとうございました。

#また聞いてしまうかもですが^^;

# always i used to read smaller articles that as well clear their motive, and that is also happening with this piece of writing which I am reading at this place. 2019/05/04 21:57 always i used to read smaller articles that as we

always i used to read smaller articles that as well clear their motive, and that is also happening with this piece of writing which I am
reading at this place.

# I'm not sure where you are getting your info, but good topic. I needs to spend some time learning more or understanding more. Thanks for great information I was looking for this info for my mission. 2019/05/12 2:09 I'm not sure where you are getting your info, but

I'm not sure where you are getting your info, but good topic.
I needs to spend some time learning more or understanding more.
Thanks for great information I was looking
for this info for my mission.

# Hello! I could have sworn I've been to this website before but after browsing through many of the articles I realized it's new to me. Anyways, I'm definitely happy I found it and I'll be book-marking it and checking back often! 2019/05/15 19:24 Hello! I could have sworn I've been to this websit

Hello! I could have sworn I've been to this website before but after browsing through many of the articles
I realized it's new to me. Anyways, I'm definitely happy I found it and
I'll be book-marking it and checking back often!

# I know this web site provides quality dependent posts and other material, is there any other site which gives these kinds of stuff in quality? 2019/07/25 18:28 I know this web site provides quality dependent po

I know this web site provides quality dependent
posts and other material, is there any other site which gives these kinds of stuff in quality?

# I know this web site provides quality dependent posts and other material, is there any other site which gives these kinds of stuff in quality? 2019/07/25 18:29 I know this web site provides quality dependent po

I know this web site provides quality dependent
posts and other material, is there any other site which gives these kinds of stuff in quality?

# I know this web site provides quality dependent posts and other material, is there any other site which gives these kinds of stuff in quality? 2019/07/25 18:30 I know this web site provides quality dependent po

I know this web site provides quality dependent
posts and other material, is there any other site which gives these kinds of stuff in quality?

# I know this web site provides quality dependent posts and other material, is there any other site which gives these kinds of stuff in quality? 2019/07/25 18:31 I know this web site provides quality dependent po

I know this web site provides quality dependent
posts and other material, is there any other site which gives these kinds of stuff in quality?

# My programmer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using Movable-type on a variety of websites for about a year and am nervous about switching t 2019/07/31 20:22 My programmer is trying to convince me to move to

My programmer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the costs.

But he's tryiong none the less. I've been using Movable-type on a variety of websites
for about a year and am nervous about switching to another platform.
I have heard good things about blogengine.net.
Is there a way I can transfer all my wordpress posts into it?
Any help would be really appreciated!

# My programmer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using Movable-type on a variety of websites for about a year and am nervous about switching t 2019/07/31 20:23 My programmer is trying to convince me to move to

My programmer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the costs.

But he's tryiong none the less. I've been using Movable-type on a variety of websites
for about a year and am nervous about switching to another platform.
I have heard good things about blogengine.net.
Is there a way I can transfer all my wordpress posts into it?
Any help would be really appreciated!

# My programmer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using Movable-type on a variety of websites for about a year and am nervous about switching t 2019/07/31 20:24 My programmer is trying to convince me to move to

My programmer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the costs.

But he's tryiong none the less. I've been using Movable-type on a variety of websites
for about a year and am nervous about switching to another platform.
I have heard good things about blogengine.net.
Is there a way I can transfer all my wordpress posts into it?
Any help would be really appreciated!

# My programmer is trying to convince me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using Movable-type on a variety of websites for about a year and am nervous about switching t 2019/07/31 20:25 My programmer is trying to convince me to move to

My programmer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the costs.

But he's tryiong none the less. I've been using Movable-type on a variety of websites
for about a year and am nervous about switching to another platform.
I have heard good things about blogengine.net.
Is there a way I can transfer all my wordpress posts into it?
Any help would be really appreciated!

# Hi, I do believe this is an excellent site. I stumbledupon it ;) I will come back once again since I book marked it. Money and freedom is the best way to change, may you be rich and continue to help other people. 2019/11/12 9:48 Hi, I do believe this is an excellent site. I stum

Hi, I do believe this is an excellent site. I stumbledupon it
;) I will come back once again since I book marked it.
Money and freedom is the best way to change, may you be rich and continue to help other people.

# Hi, I do believe this is an excellent site. I stumbledupon it ;) I will come back once again since I book marked it. Money and freedom is the best way to change, may you be rich and continue to help other people. 2019/11/12 9:49 Hi, I do believe this is an excellent site. I stum

Hi, I do believe this is an excellent site. I stumbledupon it
;) I will come back once again since I book marked it.
Money and freedom is the best way to change, may you be rich and continue to help other people.

# Hi, I do believe this is an excellent site. I stumbledupon it ;) I will come back once again since I book marked it. Money and freedom is the best way to change, may you be rich and continue to help other people. 2019/11/12 9:50 Hi, I do believe this is an excellent site. I stum

Hi, I do believe this is an excellent site. I stumbledupon it
;) I will come back once again since I book marked it.
Money and freedom is the best way to change, may you be rich and continue to help other people.

# Hi, I do believe this is an excellent site. I stumbledupon it ;) I will come back once again since I book marked it. Money and freedom is the best way to change, may you be rich and continue to help other people. 2019/11/12 9:51 Hi, I do believe this is an excellent site. I stum

Hi, I do believe this is an excellent site. I stumbledupon it
;) I will come back once again since I book marked it.
Money and freedom is the best way to change, may you be rich and continue to help other people.

# Hey! I know this is kind of off topic but I was wondering which blog platform are you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be awesome if 2019/11/18 5:45 Hey! I know this is kind of off topic but I was wo

Hey! I know this is kind of off topic but I was wondering which blog platform are
you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform.
I would be awesome if you could point me in the direction of
a good platform.

# Hey! I know this is kind of off topic but I was wondering which blog platform are you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be awesome if 2019/11/18 5:46 Hey! I know this is kind of off topic but I was wo

Hey! I know this is kind of off topic but I was wondering which blog platform are
you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform.
I would be awesome if you could point me in the direction of
a good platform.

# Hey! I know this is kind of off topic but I was wondering which blog platform are you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be awesome if 2019/11/18 5:47 Hey! I know this is kind of off topic but I was wo

Hey! I know this is kind of off topic but I was wondering which blog platform are
you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform.
I would be awesome if you could point me in the direction of
a good platform.

# Hey! I know this is kind of off topic but I was wondering which blog platform are you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be awesome if 2019/11/18 5:48 Hey! I know this is kind of off topic but I was wo

Hey! I know this is kind of off topic but I was wondering which blog platform are
you using for this site? I'm getting sick and tired of Wordpress because I've had issues with hackers and I'm looking at options for another platform.
I would be awesome if you could point me in the direction of
a good platform.

タイトル  
名前  
Url
コメント