Head.SmackOnTable();

Contains Nuts.

Extending NetTiers part 1

without comments

Im a very big fan of NetTiers – its an excellent product, an amazing piece of code created by the community, it rapidly speeds up development and basically gets rid of all the boring faff that you have to do to create databases – all that select, insert and update statements etc etc etc.

I would like to do some minor improvements to the project thoguh – iv already experimented with ILMERGE’ing all of the Microsoft.Patterns dll’s into one (So you dont have 500 DLLs referenced in each project, and speeds up deployment). One thing id like to detail on this blog is how to implement a SeperatorTemplate (ala System.Web.WebControls.Repeater) on one of the generated UI Component’s repeaters…

For this demo, im going to be using the Nettiers 2.3.0 beta 1 release…

Firstly, to find out the actual code to include a SeperatorTemplate, by our good friend the Reflector has revealed that you need to create a property and an accessor for it like the following:

	private ITemplate m_seperatorTemplate;

	[Browsable(false)]
	[TemplateContainer(typeof(WebsiteItem))]
	[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
	public ITemplate SeperatorTemplate
	{
		get { return m_seperatorTemplate; }
		set { m_seperatorTemplate = value; }
	}

(Side note – I know that prefixing member variables with m_ is a bad practice, but I want to keep to the standards of the NetTiers code, because in my opinion, having 2 coding standards is worse than having one “bad” one)

Once you have created this property, all you have to do is create the seperator when it creates the child controls, in CreateChildControls, replace the following:

	if (m_itemTemplate != null && (pos % 2) == 0)
	{
		m_itemTemplate.InstantiateIn(container);
	}

with:

	if (m_itemTemplate != null && (pos % 2) == 0)
	{
		m_itemTemplate.InstantiateIn(container);
		m_seperatorTemplate.InstantiateIn(container);
	}

What this basically does, is instead of just creating the template, it will create teh template AND the seperator template underneath it. You can fiddle this around to how you want. Just as a side note, you will want to add the m_seperatorTemplate’s instantiator in the 2 other references a few lines down where it creates the alternating item style and the itemtemplate (if there is no alternate provided).

Once you have done this change, a quick check of the page, and it seems to work fine and exactly how I had planned. The next step is to change the Nettiers codesmith project so this gets done automatically, and for every repeater in the project.

By looking at where the source file is living, and a lil experimenting, you have to change both TableRepeater.cst and ViewRepeater.cst that lives in the WebLibrary/UI directory, like the following:

        /// <summary>
        /// Gets or sets the Seperator Template
        /// </summary>
        [Browsable(false)]
        [TemplateContainer(typeof(<%=entityItem%>))]
        [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
        public ITemplate SeperatorTemplate
        {
            get { return m_seperatorTemplate; }
            set { m_seperatorTemplate = value; }
        }

Note the replacing of the type with <%=entityItem%> so it automagically generates this for all types.

A small improvement on the code above (Check to see that a Seperator template has actually been set!) when inserting, and your CreateChildControls should look something like this:

	/// <summary>
	/// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
	/// </summary>
	protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
	{
	 int pos = 0;

	 if (dataBinding)
	 {
		//Instantiate the Header template (if exists)
		if (m_headerTemplate != null)
		{
			Control headerItem = new Control();
			m_headerTemplate.InstantiateIn(headerItem);
			Controls.Add(headerItem);
		}
		if (dataSource != null)
		{
			foreach (object o in dataSource)
			{
					<%=entityName%> entity = o as <%=entityName%>;
					<%=entityItem%> container = new <%=entityItem%>(entity);

					if (m_itemTemplate != null &amp;amp;&amp;amp; (pos % 2) == 0)
					{
						m_itemTemplate.InstantiateIn(container);

						if (m_seperatorTemplate != null)
						{
							m_seperatorTemplate.InstantiateIn(container);
						}
					}
					else
					{
						if (m_altenateItemTemplate != null)
						{
							m_altenateItemTemplate.InstantiateIn(container);

							if (m_seperatorTemplate != null)
							{
								m_seperatorTemplate.InstantiateIn(container);
							}

						}
						else if (m_itemTemplate != null)
						{
							m_itemTemplate.InstantiateIn(container);

							if (m_seperatorTemplate != null)
							{
								m_seperatorTemplate.InstantiateIn(container);
							}
						}
						else
						{
							// no template !!!
						}
					}
					Controls.Add(container);

					container.DataBind();

					pos++;
			}
		}
		//Instantiate the Footer template (if exists)
		if (m_footerTemplate != null)
		{
			Control footerItem = new Control();
			m_footerTemplate.InstantiateIn(footerItem);
			Controls.Add(footerItem);
		}

	}

		return pos;
	}
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DotNetKicks
  • DZone

Written by Monty

October 15th, 2008 at 7:03 pm

Leave a Reply