Re: Serialize a Strategy Pattern List of objects with out xsi:type

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




Thanks Nick for the response.

If you or anyone wants to take on this challenge the sample that I have
given can be considered a problem to be solved. Forget about the business
aspects of it. Nick I know you asked for more business information, however,
I'm not asking this question to get alternative solutions, I am interested in
the specific solution to solve this simple (but may not be possible) problem.
I have an alternative solution developed and in place which will work fine,
however, I believe the solution that I am looking for exists and I would like
to know for the next time this situation occurs. If a simple solution does
not exist, then it should be brought to the Microsoft Product teams
attention. I'm not saying that something should be changed, I'm simply
saying that this issued should be thought about if it has not already.

Interestingly what I am more interested in now is our discussion as to
whether this is a strategy pattern or not. Again, I'm not certain that it is
however, I don't see yet how it is not. You say that that an algorithm is
needed and you say that Car and MotorCycle are not algorithms. I guess it
depends on how you define algorithm. My thought is that those creating the
patterns need a generic term to define the structure so I believe algorithm
is being used loosely. Let me ask this, what if we added the method
Accelerate() to Vechical which both Car and MotorCycle them implement. Would
they then be considered algorithms?

You stated "Patterns represent 'good' or 'best' practices. Not all
practices are good. Therefore, not all heirarchies can be illustrated as a
pattern". While I agree this is probably a true statement, I don't see how
it applies here. Are you saying that it is bad practice to call a MotorCycle
a Vehicle. I don't see how you can say such a thing, without knowing the
solution that it is being used for. The example given is a very common
"problem" so there definitely is a name for this pattern, personally as you
know, I think it is called strategy. I cannot believe it is only considered
to be an object hierarchy as you stated. So again, I ask if it is not
strategy then what is it?

Thanks for your responses, I do appreciate them. Just so you know, I don't
intend to be confrontational, but I am interested in learning.

Have a great day,

Tom





"Nick Malik [Microsoft]" wrote:

"Tom Krueger" <TomKrueger@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F56CF84B-6A24-4E18-81ED-8EC6CE2C3990@xxxxxxxxxxxxxxxx
Hello,

Thanks for the posts (I think). I have to say I'm a bit angered by your
first post. If you were not going to answer the question or ask for
clarification, then why did you reply. All you did is stop other people
that
answer newsgroup questions from looking at my post.

I have no control over other responders. On occasion, in other threads, I
have wished for such control, but alas... ;-)

If I made a mistake in
calling something a strategy pattern when it is not, then by all means
please
correct me, but you could be a little more polite rather than trying to
fire
on me. As for your "secondly" comment about homework, who cares if this
is
homework. Are the newsgroups not for asking questions that you do not
know
the answer to. If this was a simple homework question someone would have
answered it correctly by now. Just so you know, this is not a homework
assignment. I just gave this simplified example so readers could focus on
solving the problem instead of needing to learn about the business domain.

Alas, my 'homework filter' does occasionally catch someone who tries to
simplify a problem to the point where it is no longer clearly a business
problem. I am sorry that I 'charged' your post with the 'homework' label if
in fact it was not.

In my defense, there are hundreds of posts every year from folks who post
their homework problems, verbatim, and ask for someone on the board to
innocently solve it for them. These people are cheating, and some of them
are going to make it through the educational process without learning, and
one or two may very well make it past a job interview to work at my company.
I don't want them to succeed in their efforts to thwart their own learning
process. This is when I mention 'homework.' If I believe that the problem
is homework, I choose not to actually respond with details, but rather with
generalities that should help an actual student to solve their problem
without giving them 'cut and paste text' for their assignment.

I am hopeful that my original answer was useful, if not completely helpful,
in helping you to research potential solutions.

The real problem is regarding systems integration for an ecommerce web
site
hosted by a 3rd party. I have no control over the xml structure, I just
have
to abide by it. Which is part of the problem.

Alas... no one likes this situation.


Then there is the question of did I really make a mistake in calling this
problem a strategy pattern. Honestly I'm not sure. The problem I
presented
very well may not be a strategy pattern by a technicality. From the
DAOFactory link that you provided couldn't Vechicle be considered a
strategy,
with Car and MotorCycle being ConcreteStrategyA and B? If you are looking
for a context, I used List<Vechicles>. I am very interested to learn why
exactly this is not a strategy pattern.

Look at the GoF patterns as different solutions for fairly specific
problems. The problem that 'strategy' is trying to solve is that you have
an algorithm that may vary, and you want to encapsulate that variation in a
way that limits changes to the calling code. Strategy patterns are
therefore specifically trying to encapsulate algorithms. Your Vehicle
structure does not vary in algorithm in a discernable way. It varies in
structure. Therefore, the only way that I can think of to apply Strategy to
that class heirarchy is to consider the algorithm for serializing or
deserializing the data.

Therefore, it is a strategy if you write a custom serializer and/or
deserializer, since that is an algorithm that is encapsulated, thus allowing
for variation.

Also, if it is not a strategy
pattern, then I would also very much like to learn what pattern it is.
Seriously, I want to learn and would appreciate anyone telling me why the
Vechical example is not a strategy. I don't want to go around calling a
horse a donkey.

I'd need to know more about why the data is structured in this way. Your
example is spare, and perhaps that's because you invented it to illustrate
your point. As a result, it is difficult to tell what variation you are
attempting to hide from calling code. If I can understand what variation
you are attempting to encapsulate, perhaps I can point out the pattern.
Until then, it is simply an object heirarchy. Patterns represent 'good' or
'best' practices. Not all practices are good. Therefore, not all
heirarchies can be illustrated as a pattern.

Your problem is especially difficult because your list specifically shows
items as 'Vehicle' but provides elements to a 'Vehicle' item that are not
elements of 'Vehicle' but are, in fact, elements of 'Motorcycle' or 'Car'.


Ok, back to the original problem:
The problem is simple, when serializing my List of Vechicles the .NET
XMLSerializer adds the xsi:type attributes to each of the Vechicle XML
elements. I am interested in knowing how to tell the serializer not to
include those xsi:type attributes. I understand that I can write a
customize
the serialization, however, I don't want to. I want to either add an
attribute to the Vechical class or pass a value somehow into the
XMLSerializer. Please refer to my original post for xml and code examples.

In your case, since you don't want to write a custom serializer, I would
suggest that you create a seperate class that you will serialize. That
class will be called Vehicle but will contain a superset of attributes from
each of the different 'vehicle' classes that your app supports. When it
comes time to serialize the list, convert your elements, one at a time, into
this alternative class, and then serialize from there. Off the top of my
head, I don't remember the attribute to add to an element to note that it
should not be present if it is null, but I'm fairly sure that you can do
this.

//I cannot tell you the attributes you'd need, off the top of my head.
Other respondants?

public class SerVehicle : Vehicle
{
public int Wheels;
public string HandleBarStyle;
}

convert your list to objects of type SerVehicle and then serialize.


--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--



.



Relevant Pages

  • Re: Serialize a Strategy Pattern List of objects with out xsi:type
    ... calling something a strategy pattern when it is not, ... As for your "secondly" comment about homework, ... problem a strategy pattern. ... suggest that you create a seperate class that you will serialize. ...
    (microsoft.public.dotnet.framework)
  • Re: Reading and writing a big file in Ada (GNAT) on Windows XP
    ... memory for a substring; this is very descriptively called memmemand ... common cases (no mapping, single character patterns, and so on). ... So in this case a better algorithm is probably the way to go (and I ... the source length is>M and the pattern length is>N, ...
    (comp.lang.ada)
  • Re: Mersenne Twister
    ... And no PRNG is unpredictable. ... >> Just give me the algorithm and the key and I can predict it ... >hard to write a test which is able to find a pattern. ... Since the key is far far shorter than the stream ...
    (sci.crypt)
  • Re: A clear difference between algorithm and a pattern
    ... A pattern is a "formalized" algorithm. ... arrangements of class abstractions. ... Dijkstra and Parnas software architecture is ...
    (comp.object)
  • Re: Factory pattern and building object
    ... If you serialize concrete object A, ... Note that the builder is a creational pattern. ... > My issue is building the diagram from a file containing diagram items ...
    (comp.object)