There’s quite a little bit of boiler plate code that is required to be written to get this all working so I’m writing it here so we can all learn from it and use it.

Reference StructureMap  in your MVC3 solution.

In your Global.asax file in the Application_Start method, write a line to call a yet-undefined method called “ContainerStrapper.BootStructureMap();”.  This is where the whole IoC gets injected and is an absolute requirement.

This container strapper contains all your concrete mappings to interfaces across the entire scope of the project.

public static class ContainerStrapper
{
	public static void BootStructureMap()
	{
		IContainer container = new Container(x =>
									 {
										 // Controller and Filter
										 x.For<IControllerActivator>().Use<StructureMapControllerActivator>();
										 x.For<IFilterProvider>().Use<StructureMapFilterProvider>();

										 // Constructor Dependencies
										 x.For<IGenericRepository>().Use<SomeSpecificRepository>();
										 x.For<IFooManager>().Use<FooManager>();
										 x.For<IClock>().Use<SystemClock>();

										 // Property Dependencies
										 x.SetAllProperties(p => p.OfType<IGenericRepository>());
										 x.SetAllProperties(p => p.OfType<IFooManager>());
									 });

		DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
	}
}

Next you will need to create a structuremap controller activator as referenced in the first line of that container mapping we just created.

public class StructureMapControllerActivator : IControllerActivator
    {
        public StructureMapControllerActivator(IContainer container)
        {
            _container = container;
        }

        private IContainer _container;

        public IController Create(RequestContext requestContext, Type controllerType)
        {
            return _container.GetInstance(controllerType) as IController;
        }
    }

…and also a StructureMapFilterProvider:

public class StructureMapFilterProvider : FilterAttributeFilterProvider
    {
        public StructureMapFilterProvider(IContainer container)
        {
            _container = container;
        }

        private IContainer _container;

        public override IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            var filters =  base.GetFilters(controllerContext, actionDescriptor);

            foreach (var filter in filters) {
                _container.BuildUp(filter.Instance);
            }

            return filters;
        }
    }

and lastly we make a StructureMapDependencyResolver:

public class StructureMapDependencyResolver : IDependencyResolver
    {
        public StructureMapDependencyResolver(IContainer container)
        {
            _container = container;
        }

        private IContainer _container;

        public object GetService(Type serviceType)
        {
            object instance = _container.TryGetInstance(serviceType);

            if (instance == null && !serviceType.IsAbstract)
            {
                _container.Configure(c => c.AddType(serviceType, serviceType));
                instance = _container.TryGetInstance(serviceType);
            }

            return instance;
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _container.GetAllInstances(serviceType).Cast<object>();
        }
    }

You’re all done and ready to go :)

To inject dependencies in to your controllers, put the interfaces as arguments in the controller constructor and set them as private variables if you need them in your actions

 

I was playing Kinect Sports Football/Soccer yesterday and when I won I heard the familiar theme tune that played in the background.

It sounded like a modernised version of the Lotus Turbo Challenge 2 theme tune! I don’t know if this is an ode to the game or to Lotus but the stadium you play at is Lotus Park.

I understand that advertisements are all over the game, with Samsung this and T-Mobile that. But the music is a subtle addition! I like it. Check the videos below to hear for yourselves:

Kinect Sports Football (Will automatically start at 6m 20s)

Lotus Turbo Challenge 2 – Music

 

In SharePoint 2010 I came across a bug where the listitems lost their content types and reverted all the way back to being a ‘folder’. This was in a document library where the content types were managed by setting “Allow management of content types” to Yes in the advanced library settings.

I needed a way of iterating through every list item in the list and programmatically setting the content type to the correct one.

First off I made a small struct to store the path of the document library and the corresponding content type name.

public struct ListContentTypeMapping
    {
        public string Folder;
        public string ContentType;
    }

Then I iterate through the list to change the content types. The actual way to change content types of list items (at least in sharepoint 2010) is to change the property called “ContentTypeId” and set it to the Id (SPContentTypeId) of the chosen SPContentType object.

public void SetContentTypesOnDocumentLibraries(IEnumerable<ListContentTypeMapping> mappings)
	{
		using (var siteRoot = new SPSite("http://sitename/"))
		{
			foreach (var mapping in mappings)
			{
				using (var web = siteRoot.RootWeb)
				{
					var list = web.GetList(mapping.Folder);

					foreach(SPListItem item in list.Items)
					{
						ChangeContentType(list, item, mapping.ContentType);
					}
				}
			}
		}
	}

private static void ChangeContentType(SPList list, SPListItem listItem, string contentTypeName)
	{
		SPContentType contentType = list.ContentTypes[contentTypeName];

		listItem["ContentTypeId"] = contentType.Id;
		listItem.Update();
	}

Here’s some sample code, it will change all the items in those folders to the specific content types.

var mappings = new List<ListContentTypeMapping>();
mappings.Add(new ListContentTypeMapping { Folder = "some/path/to/list/", ContentType = "ContentTypeOne" });
mappings.Add(new ListContentTypeMapping { Folder = "some/path/to/anotherlist/", ContentType = "ContentTypeTwo" });
SetContentTypesOnDocumentLibraries(mappings);
 

Sharepoint has a nasty habbit of keep navigation flyouts on the screen for a good second before they hide.  This is probably to aid accessibility where a user might not have precise mouse control.  However if you’re a good designer then you should have plenty of large clickable navigation menu items and plenty of white space.

When rolling over a number of flyouts quickly, the user sees all the flyouts shown on the screen at once which looks rubbish.  To remove the delay altogether use this css in your page somewhere:

li.hover-off>ul
{
    display:none;
}

The way it works is when you hover over an item in the nav the built in sharepoint javascript adds a css class called “hover” and as soon as your mouse leaves the area it changes the class to “hover-off” for 1 second before removing it completely. This CSS will hide the unordered list directly below the list item that has the class “hover-off” thus hiding the flyout as soon as your mouse leaves the parent.

(P.s. this kind of flyout navigation can be set up by setting the ‘MaximumDynamicDisplayLevels’ attribute of the SharePoint:AspMenu control)

 

On the SPFile object there is a method called UndoCheckOut().  This discards the checkout and returns the file or publishing page back to its original state.

To discard the checkout of a publishing page do this:

publishingPage.ListItem.File.UndoCheckOut();
© 2011 athe.la blog Suffusion theme by Sayontan Sinha