fluent interfaces and xml serialisation...

O

Ollie

I'm thinking of adding a more fluent interface to a set C# class in my
current domain, the only thing that is causing me a dilemma is the fact that
the classes need to support serialisation (xml), this means they will have 2
ways to set properties and I don't generally like this...

Does anyone have any experience with supporting a a fluent interface style
on .Net classes that support xml serialisation?

Cheers in advance

Ollie
 
J

Jon Skeet [C# MVP]

Ollie said:
I'm thinking of adding a more fluent interface to a set C# class in my
current domain, the only thing that is causing me a dilemma is the fact that
the classes need to support serialisation (xml), this means they will have 2
ways to set properties and I don't generally like this...

Does anyone have any experience with supporting a a fluent interface style
on .Net classes that support xml serialisation?

The way I see it, the issue isn't supporting a fluent interface - it's
having mutable types. The two can coexist.

I'm afraid I don't know very much about XML Serialization, but I'd be
surprised if there weren't some way of intercepting the deserialization
in the class itself.
 
O

Ollie

hello Jon,

I'm sure (in fact I know) you can control the xml serialisation at a more
fine grained level but I'm trying not to go down that road at the moment...

Cheers

Ollie
 
M

Marc Gravell

If you want to go with a fluent approach rather than getter/setter,
you'd need to handle IXmlSerializable yourself; note that you'd also
lose automatic schema visibility, and the ability to do standard data-
binding. And probably a few other ComponentModel things that I'm
forgetting. This sounds unncessarily painful.

Can I ask what the driver is for fluent in this case? If the problem
is primarily at instantiation, then perhaps consider C# 3 object
initializers? i.e.

Foo foo = new Foo {Width = 100, Length = 20, Name = "Fred", Foo =
"Bar", When = DateTime.Now };

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top