Click here to Skip to main content
15,886,689 members
Articles / Web Development / IIS

Creating a Domain Service Factory to Host POCO Entities

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
10 Dec 2010CPOL4 min read 39K   615   11  
This article demonstrates how to write a domain service factory to host plain old class object (POCO) entities and use them through RIA Services in Silverlight version 4.
using System;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BasicDataViewer.DynamicData.PageTemplates
{
    public partial class ListDetails : System.Web.UI.Page
    {
        protected MetaTable table;

        protected void Page_Init(object sender, EventArgs e)
        {
            table = DynamicDataRouteHandler.GetRequestMetaTable(Context);
            GridView1.SetMetaTable(table, table.GetColumnValuesFromRoute(Context));
            FormView1.SetMetaTable(table);
            GridDataSource.EntityTypeFilter = table.EntityType.Name;
            DetailsDataSource.EntityTypeFilter = table.EntityType.Name;

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Title = table.DisplayName;
            GridDataSource.Include = table.ForeignKeyColumnsNames;
            DetailsDataSource.Include = table.ForeignKeyColumnsNames;

            // Selection from url
            if (!Page.IsPostBack && table.HasPrimaryKey)
            {
                GridView1.SelectedPersistedDataKey = table.GetDataKeyFromRoute();
                if (GridView1.SelectedPersistedDataKey == null)
                {
                    GridView1.SelectedIndex = 0;
                }
            }

            // Disable various options if the table is readonly
            if (table.IsReadOnly)
            {
                DetailsPanel.Visible = false;
                GridView1.AutoGenerateSelectButton = false;
                GridView1.AutoGenerateEditButton = false;
                GridView1.AutoGenerateDeleteButton = false;
                GridView1.EnablePersistedSelection = false;
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            if (GridView1.Rows.Count == 0)
            {
                FormView1.ChangeMode(FormViewMode.Insert);
            }
        }

        protected void Label_PreRender(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            DynamicFilter dynamicFilter = (DynamicFilter)label.FindControl("DynamicFilter");
            QueryableFilterUserControl fuc = dynamicFilter.FilterTemplate as QueryableFilterUserControl;
            if (fuc != null && fuc.FilterControl != null)
            {
                label.AssociatedControlID = fuc.FilterControl.GetUniqueIDRelativeTo(label);
            }
        }

        protected void DynamicFilter_FilterChanged(object sender, EventArgs e)
        {
            GridView1.EditIndex = -1;
            GridView1.PageIndex = 0;
            FormView1.ChangeMode(FormViewMode.ReadOnly);
        }

        protected void GridView1_RowEditing(object sender, EventArgs e)
        {
            FormView1.ChangeMode(FormViewMode.ReadOnly);
        }

        protected void GridView1_SelectedIndexChanging(object sender, EventArgs e)
        {
            GridView1.EditIndex = -1;
            FormView1.ChangeMode(FormViewMode.ReadOnly);
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            SetDeleteConfirmation(e.Row);
        }

        protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            FormView1.DataBind();
        }

        protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            FormView1.DataBind();
        }

        protected void FormView1_ItemDeleted(object sender, FormViewDeletedEventArgs e)
        {
            GridView1.DataBind();
        }

        protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            GridView1.DataBind();
        }

        protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            GridView1.DataBind();
        }

        protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
        {
            if (e.NewMode != FormViewMode.ReadOnly)
            {
                GridView1.EditIndex = -1;
            }
        }

        protected void FormView1_PreRender(object sender, EventArgs e)
        {
            if (FormView1.Row != null)
            {
                SetDeleteConfirmation(FormView1.Row);
            }
        }

        private void SetDeleteConfirmation(TableRow row)
        {
            foreach (Control c in row.Cells[0].Controls)
            {
                LinkButton button = c as LinkButton;
                if (button != null && button.CommandName == DataControlCommands.DeleteCommandName)
                {
                    button.OnClientClick = "return confirm('Are you sure you want to delete this item?');";
                }
            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions