Sunday, December 6, 2009

A business oriented drawing application made easy with Entity Framework 4

Hi, here is a basic example of how to design a generic business oriented drawing application using Entity Framework 4. The Client of the Entity model could be a Silverlight or WPF application, using RIA Services for instance (although so far RIA Services has some limitations).

(On this example, my project uses an ASP.Net front office, just to validate that it also works easily)

Consider the following edmx model :


After connecting to a Database and generating all tables from the model ("Generate Database Script from Model..."), the EF also magically generates all the C# classes for you. It's a nice and clean code that you can directly use :


protected void Page_Load(object sender, EventArgs e)
{

    EFModelContainer BusinessContainer = new EFModelContainer();

    Screen screen = new Screen() { Guid = Guid.NewGuid(), Name = "My Screen" };
    Tab tab = new Tab() { Guid = Guid.NewGuid(), Name = "My Tab" };


    BlockElementary elementaryBlock1 = new BlockElementary() { Guid = Guid.NewGuid(), Name = "My elementaryBlock1" };
    BlockElementary elementaryBlock2 = new BlockElementary() { Guid = Guid.NewGuid(), Name = "My elementaryBlock2" };
    BlockElementary elementaryBlock3 = new BlockElementary() { Guid = Guid.NewGuid(), Name = "My elementaryBlock3" };

    BlockContainer blockContainer1 = new BlockContainer() { Guid = Guid.NewGuid(), Name = "My BlockContainer1" };
    BlockContainer blockContainer2 = new BlockContainer() { Guid = Guid.NewGuid(), Name = "My BlockContainer2" };

    // Adds the elementary blocs to the container
    // NOTE : I know this is not proper English,
    // but I prefer this way for intellisens purposes
    blockContainer1.BlockElementaries.Add(elementaryBlock1);
    blockContainer1.BlockElementaries.Add(elementaryBlock3);

    blockContainer2.BlockElementaries.Add(elementaryBlock1);
    blockContainer2.BlockElementaries.Add(elementaryBlock2);
    blockContainer2.BlockElementaries.Add(elementaryBlock3);

    atmTextBlock atomTB = new atmTextBlock()
{ Guid = Guid.NewGuid(), Name = "My atmTextBlock" };
    atomTB.Label = "Label of my atome of type TextBlock atomTB";

    atmTextBlock atomTB2 = new atmTextBlock()
{ Guid = Guid.NewGuid(), Name = "My atmTextBlock" };
    atomTB2.Label = "Label of my atome of type TextBlock atomTB2";


    elementaryBlock1.Atoms.Add(atomTB);
    elementaryBlock2.Atoms.Add(atomTB2);

    // NOTE : I should be renaming tab.BlockContainer into tab.BlockContainers
    tab.BlockContainer.Add(blockContainer1);
    tab.BlockContainer.Add(blockContainer2);
    screen.Tabs.Add(tab);

    BusinessContainer.AddToEltOfScreens(screen);
    int countUpdates = BusinessContainer.SaveChanges();

    Response.Write("Number of updates = " + countUpdates);

}

Everything saves properly into the database :

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.