Mark Needham

Thoughts on Software Development

ASP.NET MVC: Reducing duplication for partial models

with 3 comments

One of the problems we can encounter when using partials throughout our views is how we should create the model needed for those partials.

The approach that we have been following is to have the partial/child model on the parent model and then just call the appropriate method where we create the partial.

e.g.

public class ParentModel 
{
	public string Property1 {get;set;}
	public ChildModel ChildModel { get;set; }
}
 
public class ChildModel
{
	public string Property1 {get;set;}
}

We have sometimes run into the problem where the data in the ChildModel is being populated from the ParentModel (due to it also being needed there) leading to data duplication.

1
2
3
4
5
6
ParentModel parentModel = new ParentModel();
parentModel.Property1 = "value1"
parentModel.ChildModel = new ChildModel 
						{	
							Property1 = parentModel.Property1;
					  	}

Now the other problem with this is that we are relying on line 2 being executed before line 3 – we have created an order dependency in our code for no gain!

We are following a convention of having minimal logic in our views which means that we want to avoid creating the ChildModel in our view, meaning that we now have a problem to solve.

A cool approach which Dave introduced me to makes use of the Adaptor pattern to solve the problem.

We would adjust the ParentModel like so:

public class ParentModel
{
	public IChildModel { get { return new ChildModelAdaptor(this); }}
}

We then just delegate the calls to the ParentModel and drive the ChildModel to become an interface since it no longer needs to be a class.

public interface ChildModel
{
	string Property1 {get;set;}
}
public class ChildModelAdaptor : IChildModel 
{
	private ParentModel parentModel;
 
	public ChildModelAdaptor(ParentModel parentModel)
	{
		this.parentModel = parentModel;
	}
 
	public string Property1
	{		
		get { return parentModel.Property1; }
	}
}

If the data on the ChildModel is completely independent of the ParentModel then I would probably just create the model like before.

If the data on the ChildModel is a combination of data from the ParentModel and other classes then I would pass in those other classes in the constructor of the adaptor.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • HackerNews
  • StumbleUpon
  • Twitter

Written by Mark Needham

March 3rd, 2009 at 11:55 pm

Posted in .NET

Tagged with ,

3 Responses to 'ASP.NET MVC: Reducing duplication for partial models'

Subscribe to comments with RSS or TrackBack to 'ASP.NET MVC: Reducing duplication for partial models'.

  1. ASP.NET MVC: Reducing duplication for partial models at Mark Needham…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

    DotNetShoutout

    4 Mar 09 at 8:42 am

  2. ASP.NET MVC: Reducing duplication for partial models at Mark Needham…

    DotNetBurner.com – news and articles about .net DotNetBurner…

  3. [...] to VoteASP.NET MVC: Reducing duplication for partial models (3/2/2009)Monday, March 02, 2009 from Mark NeedhamOne of the problems we can encounter when using partials [...]

Leave a Reply