Thursday, March 27, 2008

[important: this post belongs to a whole host of other posts. Basically it's about my efforts of building a light-weight n-tier disconnected solution for Entity Framework]

Another status update. I've worked on connecting up a clean graph to a EntityFramework context and found it far from straight-forward! I ended up with very little code though, which is a good sign.

The approach of not serializing the object context, makes it harder to figure out when an object was added or removed. On the client, I keep copies of the 'original' collection and I compare the new collection with the old collection. Problem there is that it's quite possible that an object was just removed from a collection and added to another collection. How do we know we have to do a real 'delete' on it?
I have not yet figured this out ;-)

Here is some code for you to look at:

  0    Customer c = new Customer { Name = "Ruurd Boeke" };
  1    Car car1 = new Car { Make = "Saab", Customer = c };
  2    Order order1 = new Order { Amount = 2, Customer = c };
  3
  4
  5    IEditableBusinessObject eC = c as IEditableBusinessObject;
  6    string MsgOnWire = "";
  7
  8    // at this point, we are not at all attached
  9    using (SimpleRelationshipTestEntities context = new SimpleRelationshipTestEntities())
  10    {
  11     context.AddToCustomer(c);
  12     context.SaveChanges();  // this instructs context to start changetracking
  13
  14     ((IContextAware)c).CreateSerializableState(context);
  15
  16     DataContractSerializer s = new DataContractSerializer(c.GetType(), null, int.MaxValue, false, false, new SubstituteDomainDataContractSurrogate());
  17
  18     MsgOnWire = s.GetWellFormedToContract(c);
  19    }
  20

The message on the wire looks like this:

<CustomerSurrogate xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="myNamespace">
    <Cars>
        <Car>
            <CarID>244</CarID>
            <Customer>
                <SerializationID>0</SerializationID>
            </Customer>
            <Make>Saab</Make>
            <SerializationID>1</SerializationID>
            <OriginalValue_CarID>244</OriginalValue_CarID>
            <OriginalValue_Customer>
                <SerializationID>0</SerializationID>
            </OriginalValue_Customer>
            <OriginalValue_Make>Saab</OriginalValue_Make>
        </Car>
    </Cars>
    <CustomerID>152</CustomerID>
    <Name>Ruurd Boeke</Name>
    <Orders>
        <Order>
            <Amount>2</Amount>
            <Customer>
                <SerializationID>0</SerializationID>
            </Customer>
            <OrderID>159</OrderID>
            <SerializationID>2</SerializationID>
            <OriginalValue_Amount>2</OriginalValue_Amount>
            <OriginalValue_Customer>
                <SerializationID>0</SerializationID>
            </OriginalValue_Customer>
            <OriginalValue_OrderID>159</OriginalValue_OrderID>
        </Order>
    </Orders>
    <SerializationID>0</SerializationID>
    <OriginalValue_Cars>
        <Car>
            <SerializationID>1</SerializationID>
        </Car>
    </OriginalValue_Cars>
    <OriginalValue_CustomerID>152</OriginalValue_CustomerID>
    <OriginalValue_Name>Ruurd Boeke</OriginalValue_Name>
    <OriginalValue_Orders>
        <Order>
            <SerializationID>2</SerializationID>
        </Order>
    </OriginalValue_Orders>
</CustomerSurrogate>

Next step is to receive this xml on the client, deserialize it and change some stuff:

  0    DataContractSerializer s2 = new DataContractSerializer(typeof(CustomerClient), null, int.MaxValue, false, false, new SubstituteDomainDataContractSurrogate());
  1    CustomerClient c2 = (CustomerClient)s2.UnwrapFromString(MsgOnWire);
  2
  3    // delete
  4    c2.Cars.First().Customer = null;
  5    c2.Cars.Clear();
  6
  7    // add
  8    OrderClient order2 = new OrderClient { Amount = 100, Customer = c2 };
  9    c2.Orders.Add(order2);
  10
  11    // change
  12    c2.Name = "Changed";
  13
  14    // we have edited everything, let's get back to the server
  15    DataContractSerializer s3 = new DataContractSerializer(typeof(CustomerClient), null, int.MaxValue, false, false, new SubstituteDomainDataContractSurrogate());
  16    MsgOnWire = s3.GetWellFormedToContract(c2);

This client is removing the cars. Note that we do not get the automatic hookup of relations that we used to get on the server: there is no EF Relationship manager on the client!

<CustomerSurrogate xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="myNamespace">
    <Cars />
    <CustomerID>152</CustomerID>
    <Name>Changed</Name>
    <Orders>
        <Order>
            <Amount>2</Amount>
            <Customer>
                <SerializationID>0</SerializationID>
            </Customer>
            <OrderID>159</OrderID>
            <SerializationID>1</SerializationID>
            <OriginalValue_Amount>2</OriginalValue_Amount>
            <OriginalValue_Customer>
                <SerializationID>0</SerializationID>
            </OriginalValue_Customer>
            <OriginalValue_OrderID>159</OriginalValue_OrderID>
        </Order>
        <Order>
            <Amount>100</Amount>
            <Customer>
                <SerializationID>0</SerializationID>
            </Customer>
            <SerializationID>2</SerializationID>
        </Order>
    </Orders>
    <SerializationID>0</SerializationID>
    <OriginalValue_Cars>
        <Car>
            <CarID>244</CarID>
            <Make>Saab</Make>
            <SerializationID>3</SerializationID>
            <OriginalValue_CarID>244</OriginalValue_CarID>
            <OriginalValue_Customer>
                <SerializationID>0</SerializationID>
            </OriginalValue_Customer>
            <OriginalValue_Make>Saab</OriginalValue_Make>
        </Car>
    </OriginalValue_Cars>
    <OriginalValue_CustomerID>152</OriginalValue_CustomerID>
    <OriginalValue_Name>Ruurd Boeke</OriginalValue_Name>
    <OriginalValue_Orders>
        <Order>
            <SerializationID>1</SerializationID>
        </Order>
    </OriginalValue_Orders>
</CustomerSurrogate>

We see that there are no current cars, but the original value of cars still has them. That is how I figure out to delete the cars on the server.
Did you also notice how the new order does not have a ID assigned? The server will take care of it.

  0    // we're at the server
  1    using (SimpleRelationshipTestEntities context = new SimpleRelationshipTestEntities())
  2    {
  3     DataContractSerializer s4 = new DataContractSerializer(typeof(Customer), null, int.MaxValue, false, false, new SubstituteDomainDataContractSurrogate());
  4     Customer c3 = (Customer)s4.UnwrapFromString(MsgOnWire);
  5
  6     ((IContextAware)c3).AttachGraphToContext(context,
  7      delegate(object source)
  8      {
  9       return source.GetType().Name;   // our setnames correspond to our classname.
  10      });
  11
  12
  13     context.SaveChanges();
  14    }

Now, we're deserializing and attaching the graph to the context. The delete on line 7 is a way of returning entitysetnames. EF needs to know the entitysetname to be able to attach an object to a context, but there is no way for me to know it. So at this point I let you make the decision yourself.

The database is correctly filled and we are happy.

Now, I really want to check in this source, but there is a lot to be done still. The API I have created is monstrous.
Also, I think there are quite a few situations where this will break horribly.

Once I get just a little happier about it, I will do a screencast building a server using Entity Framework and a silverlight client that is editing.

Thursday, October 14, 2010 2:52:21 PM (Romance Standard Time, UTC+01:00)
<p>Of the 30 million in the <a href="http://www.fivefingeronline.com/">vibram fivefingers</a> activities, 2010 non-environment-majors on China walk first stage <a href="http://www.fivefingeronline.com/">vibram five</a> activities, the poem ended Co., LTD. Offers outdoor products to both the retail price is lucky <a href="http://www.fivefingeronline.com/products_all.html">five finger</a> skywalker prize for ¥1980 German professional outdoor shoe in <a href="http://www.fivefingeronline.com/featured_products.html">vibram shoes</a> on GTX RENEGADE for hiking shoes. They will be wearing this pair of the world’s most powerful boots origin produces the boots top <a href="http://www.fivefingeronline.com/featured_products.html">five fingers</a> product officially began to walk.Called “outside” of the ID of the winners is walking <a href="http://www.fivefingeronline.com/products_new.html">vibram five finger</a> across the big horqin sandy land “green channel”. Inner Mongolia horqin sandy land and <a href="http://www.fivefingeronline.com/vibram-five-fingers-kso-c-67.html">fivefingers kso</a> liaoning, jilin across three provinces, 92% of eastern Inner Mongolia above <a href="http://www.fivefingeronline.com/products_new.html">vibram five fingers</a> distribution in chifeng city, area and the tongliao 5 million square <a href="http://www.fivefingeronline.com/specials.html">vibram five fingers shoes</a> kilometers, ranking first in four sand. Genghis khan’s <a href="http://www.fivefingeronline.com/vibram-five-fingers-kso-c-67.html">vibram kso</a> brother, and its subordinate subordinates’ called cheap five finger shoes, then formed by <a href="http://www.fivefingeronline.com/vibram-five-fingers-classic-c-76.html">vibram classic</a> tribe.In the first stage, the activity ended at the second stage, the second phase will start to Tibet, xinjiang, “stand in xinjiang, ningxia, Tibet, shaanxi, gansu, qinghai and within the <a href="http://www.fivefingeronline.com/vibram-five-fingers-flow-c-73.html">vibram flow</a> scope of land.</p>
<p>If you want to take part in outdoor activities, persistent and very love this <a href="http://www.fivefingeronline.com/vibram-five-fingers-kso-c-67.html">vibram fivefingers kso</a> sport, so choose cheap vibram is very important. A set of suitable equipment can indeed for our outdoor activities to provide protection, let us be more close to nature, feel nature.We choose to suit <a href="http://www.fivefingeronline.com/reviews.html">5fingers</a> oneself is suitable for environmental equipment, so we first concern is the applicability of the equipment, rather than spend less money for price, event, although is good, but usually sale <a href="http://www.fivefingeronline.com/vibram-five-fingers-speed-c-115.html">five finger speed</a> is reality, the most expensive sometimes are not is most appropriate. There is not expected to equipment can be armed to the teeth. A bite to eat dinner.</p>
<p><a href="http://www.fivefingeronline.com/">MBT shoes</a> have become increasingly popular. Because of its tremendous role to humans. <a href="http://www.fivefingeronline.com/products_all.html">mbt walking shoes</a> not only fashionable, but also healthy. I think this is more important is fashion. Swiss <a href="http://www.fivefingeronline.com/specials.html">mbt shoes sale</a>, Karl Mueller invented. His invention came to the United Kingdom. In tourism this journey, it can be found wearing <a href="http://www.fivefingeronline.com/products_new.html">mbt sale</a> can easily get ahead, he who is like a walk in the soft grass. It takes years of research and development, <a href="http://www.fivefingeronline.com/reviews.html">mbt shoes review</a> Sale market development began in 1996, selling over one million every year is growing <a href="http://www.fivefingeronline.com/">mbt footwear</a> rapidly.<a href="http://www.fivefingeronline.com/">http://www.fivefingeronline.com/</a> </p>
Thursday, October 14, 2010 2:55:26 PM (Romance Standard Time, UTC+01:00)
3_103.html"><strong> </strong></a> are made more unique by the inclusion of a large wooden button with the Ugg logo on it and an elastic band closure. So it allows you to wear t
Just as with the <a href="http://www.shoeslockers.com/ugg-boots-ugg-classic-cardy-c-53_108.html"><strong>UGG boots</strong></a> the new calf high boot that this <a href="http://www.shoeslockers.com/ugg-boots-ugg-knightbridge-boots-c-53_118.html"><strong>Ugg Australia</strong></a> company have introduced the Bailey Button offers the same level of comfort. Certainly for women who are looking for a pair of <a href="http://www.shoeslockers.com/ugg-boots-ugg-lo-pro-button-c-53_124.html"><strong>UGG sale</strong></a> to team with jeans or a long skirt these are ideal.But as you will soon discover these <a href="http://www.shoeslockers.com/ugg-boots-ugg-bailey-button-c-5hem with this detailing on shown or you can undo the button and then turn the top over to create a cuff.With all the boots included in <a href="http://www.shoeslockers.com/ugg-boots-ugg-new-style-ultra-short-boots-c-53_122.html"><strong>Cheap UGG boots</strong></a> Australia’s Classic Collection range the <a href="http://www.shoeslockers.com/ugg-boots-ugg-classic-mini-c-53_114.html"><strong>UGG bailey button</strong></a> is lined with twin faced Grade A sheepskin. Also there is the traditional suede heel guard to ensure that not only is this area protected but offers this part of the <a href="http://www.shoeslockers.com/ugg-boots-ugg-bailey-button-fancy-c-53_102.html"><strong>UGG boots sale</strong></a> support. Plus within the <a href="http://www.shoeslockers.com/ugg-boots-ugg-classic-short-c-53_113.html"><strong>UGG classic cardy</strong></a> you find each pair has sock liners that are made from sheepskin PU foam so increasing the level of comfort a person feels when wearing <a href="http://www.shoeslockers.com/ugg-boots-ugg-classic-short-c-53_113.html"><strong>UGG short boots</strong></a>.<a href="http://www.shoeslockers.com/ugg-boots-ugg-classic-tall-c-53_110.html"><strong>zigtech shoes</strong></a> is very unique. It uses an innovative lightweight foam materials,<a href="http://www.shoeslockers.com/reebok-zigtech-shoes-c-127.html"><strong>ZigTech trainers</strong></a> are designed to help you run further and faster, allowing you to train while using their unique <a href="http://www.shoeslockers.com/reebok-easytone-shoes-c-126.html"><strong>Easytone shoes</strong></a> harder for longer with reduced risk of injury. They do this by providing lots of cushioning Yesterday we saw the promo video featuring Thierry Henry and Lewis Hamilton talking about the trainers. So today we’re looking at the latest technical video released by Reebok to explain what <a href="http://www.shoeslockers.com/women-mbt-shoes-mbt-fora-shoes-c-49_61_66.html"><strong>Discount Reebok Easytone</strong></a> is and how the <a href="http://www.shoeslockers.com/ugg-boots-ugg-knightbridge-boots-c-53_118.html"><strong>zigtech sale</strong></a> sole works.in the Slinky-esque midsole/outsole that is said to reduce muscle wear and transfer energy. <a href="http://www.shoeslockers.com/ugg-boots-ugg-lo-pro-button-c-53_124.html"><strong>Reebok zigtech shoes</strong></a> Stadium bring an innovative technology shoes- <a href="http://www.shoeslockers.com/men-mbt-shoes-mbt-casual-shoes-c-49_54_55.html"><strong>Reebok easytone shoes</strong></a> training run. We series, is the best <a href="http://www.shoeslockers.com/products_new.html"><strong>Easy tone shoes</strong></a> sport technology to date. It can reduce 20% of the leg muscle fatigue and wear, in order to cultivate strength and degree of difficulty is significantly increased.<a href="http://www.shoeslockers.com/men-mbt-shoes-mbt-sandals-shoes-c-49_54_57.html"><strong>MBT shoes</strong></a> effect abdominal, leg and buttock muscles.cheap <a href="http://www.shoeslockers.com/mbt-shoes-men-mbt-shoes-c-49_54.html"><strong>Cheap mbt shoes</strong></a> looks sunny, so it is a good choice <a href="http://www.shoeslockers.com/mbt-shoes-c-49.html"><strong>MBT shoes sale</strong></a> believe that human fitness majestic, <a href="http://www.shoeslockers.com/skechers-shapeups-shapeups-sleek-fit-c-50_75.html"><strong>Skecher Shape Ups</strong></a> walk to a large extent. This view wholesale <a href="http://www.shoeslockers.com/women-mbt-shoes-mbt-lami-shoes-c-49_61_67.html"><strong>Discount MBT Shoes</strong></a> a number of scientific ,Therefore, majestic mbt shoes and strengthen peoples understanding <a href="http://www.shoeslockers.com/skechers-shapeups-shape-ups-stability-c-50_74.html"><strong>shape ups Shoes</strong></a> .in <a href="http://www.shoeslockers.com/ugg-boots-ugg-classic-tall-stripe-cable-c-53_101.html"><strong>shape up skechers</strong></a> through disseminate the majestic <a href="http://www.shoeslockers.com/ugg-boots-ugg-classic-tall-metallic-c-53_112.html"><strong>shape ups discount</strong></a> ideas and concepts.
Friday, October 15, 2010 8:52:33 AM (Romance Standard Time, UTC+01:00)
href="http://www.feetlockers.com/reebok-easytone-reebok-easytone-womens-shoes-c-25_28.html">Reebok Easytone Shoes</a> syndication. And then there are those who are having <a href="http://www.feetlockers.com/specials.html">Discount Reebok zigtech</a> trouble getting out of their own way.The folks who are not succeeding are the ones whose first question is “where do I find a good deal?” Early stage syndicators think they need to find a great deal, then raise capital, and they will be on their way toward success and profit. I usually counter that with some tough love and a reality check. Instead of looking for deals and Reebok shoes, a prudent <a href="http://www.feetlockers.com/reebok-easytone-reebok-zig-pulse-running-shoes-c-25_26.html">Reebok EasyTone</a> syndicator needs to put the horse in front of the deal, and start with the question of how to raise capital.
Friday, October 15, 2010 8:52:42 AM (Romance Standard Time, UTC+01:00)
back pain and lumbosacral joint.Moreover, the girls who wear high heels early will lead to <a href="http://www.zigtechshoesonsale.com/mbt-men-shoes-mbt-casual-shoes-c-8_9.html"><strong>Discount Mbt Shoes</strong></a> the developing pelvic tilt, which make the <a href="http://www.zigtechshoesonsale.com/mbt-men-shoes-mbt-sandals-shoes-c-8_11.html"><strong>Mbt shoes clearance</strong></a>, is demanding.However, they are not only for people on their feet but for anyone who wants to wear a pair of comfortable <a href="http://www.zigtechshoesonsale.com/mbt-women-shoes-mbt-sports-shoes-c-15_26.html"><strong>Mbt shoes outlet</strong></a> that takes the pain away from your body when you are standing and walking. That’s why you need to wear <a href="http://www.zigtechshoesonsale.com/mbt-men-shoes-mbt-casual-shoes-c-8_9.html"><strong>MBT Sandals Shoes</strong></a>.
Friday, October 22, 2010 9:17:54 AM (Romance Standard Time, UTC+01:00)
With the coming of Nike Air Max series,and the unique design and fashion style have made more and more young people love them. In Nike air max 2010,nike company had been released their newest product, it is called Nike Air Max 2009,which is also the latest nike air max 90 series,the shoes is made from a synthetic leather which using the latest technology to give a more smooth surface of the shoe. A non-pressurized forefoot and heel Air-Sole unit is integrated into the Cushlon midsole foam for a responsive ride.n our online stores,we provide Air Max 2009.
Friday, October 22, 2010 9:18:03 AM (Romance Standard Time, UTC+01:00)
<a href="http://www.shapeupsoutlet.com/"><strong>Shape Ups Shoes</strong></a> come in different colors and designs. They <a href="http://www.shapeupsoutlet.com/shape-ups-stability-c-8.html"><strong>Skechers Shape Ups</strong></a> the same midsole and outsole, so they should fit and feel the same. <a href="http://www.shapeupsoutlet.com/shapeups-sleek-fit-c-9.html"><strong>Skechers Shape up Shoes</strong></a> also come in extra wide widths in select styles for men and women.<a href="http://www.shapeupsoutlet.com/womens-shapeups-c-1.html"><strong>Skechers Shape Ups Shoes</strong></a> While You Walk
Tuesday, October 26, 2010 2:37:27 AM (Romance Standard Time, UTC+01:00)
href="http://www.feetlockers.com/reebok-easytone-c-25.html">Reebok ZigTech</a> tests the shoes on real runners who fill in a detailed questionnaire at the start, mid-way and end of 300-mile increments. They’re light, flexible and so comfortable. <a href="http://www.feetlockers.com/reebok-easytone-c-25.html">Reebok ZigTech shoes</a> footwear, training longer just got easier.<a href="http://www.feetlockers.com/reebok-easytone-reebok-easytone-womens-shoes-c-25_28.html">Reebok Easytone Shoes</a> has the top technology which uses shoes’ energy and transfers it horizontally. ZigTech helps to transfer weight through the whole range of motion by pushing the force of the strike through to the front of your foot. <a href="http://www.feetlockers.com/specials.html">Discount Reebok zigtech</a> footwear is designed to protect your feet and return energy to the athlete for a soft and springy ride. The foam is lightweight. Forefoot flex grooves for added <a href="http://www.feetlockers.com/reebok-easytone-reebok-zig-pulse-running-shoes-c-25_26.html">Reebok EasyTone</a> flexibility. This ultimately enables athletes to stay healthier during in this season.<br>
Tuesday, October 26, 2010 2:38:35 AM (Romance Standard Time, UTC+01:00)
href="http://www.zigtechshoesonsale.com/mbt-men-shoes-mbt-sandals-shoes-c-8_11.html"><strong>Cheap mbt shoes</strong></a> is not too high, if the heel is higher than 3.5 cm, it will increase the risk of lumbar lordosis and lumbosacral angle, which easily lead to fatigue and lower back pain and lumbosacral joint.Moreover, the girls who wear high heels early will lead to <a href="http://www.zigtechshoesonsale.com/mbt-men-shoes-mbt-casual-shoes-c-8_9.html"><strong>Discount Mbt Shoes</strong></a> the developing pelvic tilt, which make the <a href="http://www.zigtechshoesonsale.com/mbt-men-shoes-mbt-sandals-shoes-c-8_11.html"><strong>Mbt shoes clearance</strong></a>, is demanding.However, they are not only for people on their feet but for anyone who wants to wear a pair of comfortable <a href="http://www.zigtechshoesonsale.com/mbt-women-shoes-mbt-sports-shoes-c-15_26.html"><strong>Mbt shoes outlet</strong></a> that takes the pain away from your body when you are standing and walking. That’s why you need to wear <a href="http://www.zigtechshoesonsale.com/mbt-men-shoes-mbt-casual-shoes-c-8_9.html"><strong>MBT Sandals Shoes</strong></a>.
Thursday, November 04, 2010 4:33:34 AM (Romance Standard Time, UTC+01:00)
http://www.coachusabags.com/
http://www.asics-us.com/
http://www.ukuggus.com/
http://www.mbt-usa.com/
Thursday, December 02, 2010 9:17:11 AM (Romance Standard Time, UTC+01:00)
xiaolaba Only after before a "concept, unlike other principles vile ugg boots on sale and difficult to understand, on the contrary boots uggs on sale, it is simple and easily arouses the child's confidence and high spirits, goals are small but specific ugg boot sale. In this goal, the child without understanding thoughts, solid, conscientious to do with bailey button, abandoned children in a meeting in surface. Small success to classic mini uggs, and gets the others recognized and appreciated, will gradually moving towards greater success mini uggs on sale, you'll make a big career. Words can ruin a man's confidence uggs mini on sale, even burst of his hopes of survival classic mini uggs on sale, But a word can also encourage one from loss to come out, or make one from a new Angle know yourself ugg classic cardy, changed his life. So at any time, we don't save said an encouraging word, give a trusting eyes ugg classic short, do a duck-yard trifle. A person's strength for himself perhaps is very limited ugg classic short navy, but he has probably help stimulate another person's infinite potential ugg boot sale.
Comments are closed.