<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Obstructed Graph &#187; MVC</title>
	<atom:link href="http://robertschultz.org/tag/mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://robertschultz.org</link>
	<description>A blog managed and maintained by a software engineer living in Fresno, California.</description>
	<lastBuildDate>Mon, 09 Aug 2010 07:13:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>59 Days of Code: StructureMap Dependency Injection with ASP.NET MVC</title>
		<link>http://robertschultz.org/2010/03/15/59-days-of-code-structuremap-dependency-injection-with-asp-net-mvc/</link>
		<comments>http://robertschultz.org/2010/03/15/59-days-of-code-structuremap-dependency-injection-with-asp-net-mvc/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 16:44:55 +0000</pubDate>
		<dc:creator>Robert Schultz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[59 Days of Code]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[StructureMap]]></category>

		<guid isPermaLink="false">http://www.robertschultz.org/?p=762</guid>
		<description><![CDATA[To continue on my blog posts discussing some of my research and technologies I am going to utilize for the 59 Days of Code competition here in Fresno, I wanted to touch on the use of Dependency Injection.  Dependency Injection is a technique to supply an instantiated external dependency to another component of your code. [...]]]></description>
			<content:encoded><![CDATA[<p>To continue on my blog posts discussing some of my research and technologies I am going to utilize for the 59 Days of Code competition here in Fresno, I wanted to touch on the use of Dependency Injection.  Dependency Injection is a technique to supply an instantiated external dependency to another component of your code.  This allows you to keep the dependency between objects completely separate and is used quite a bit now days.</p>
<p>Previously I have not ventured into the Dependency Injection world, mainly due to the fact that we don&#8217;t use any sort of programming methodologies or any testing frameworks.  So I guess you can say I&#8217;ve been waiting to look into it for when the need arose, which is now.</p>
<p>So to start, I evaluated a few DI frameworks like Castle, Spring.NET, StructureMap, Ninject and Unity.  After reading some of the sample code it seemed clear that I wanted to go with StructureMap.  Almost all of the DI frameworks had similar or the same features but I wanted to go with the one that allowed me to get up and running the quickest with less configuration as possible and StructureMap did this.</p>
<p>For example say you already have an existing MVC application where you work with Users and UserDepartments, and your Business namespace contains helper ADO.NET methods:</p>
<pre class="brush: csharp;">
[HttpGet]
pubic ActionResult Index()
{
	Data.Users users = new Business.Users();
	Data.UserDepartments userDepartments = new Business.UserDepartments();

	ViewData[&quot;UserDepartments&quot;] = userDepartments;
	return View(users);
}
</pre>
<p>The problem with this is that the Users ad UserDepartments dependency is tightly coupled with this action method.  It is required to know the implementation.  This presents a huge problem when you want to implement testing or mock frameworks to your code because there is no way you can supplement a different implementation for your data call.  Now, if I was to wire this up with StructureMap, I would do the following.</p>
<p>Configure my Global.asax.cs to make it aware of my repository interface:</p>
<pre class="brush: csharp;">
protected void Application_Start()
{
	RegisterRoutes(RouteTable.Routes);

	ObjectFactory.Initialize(init =&gt;
	{
		init.For&lt;IUsers&gt;().Use&lt;Users&gt;();
		init.For&lt;IUserDepartments&gt;().Use&lt;UserDepartments&gt;();
	});
}
</pre>
<p>This tells StructureMap to inject an instance of User and UserDepartments whenever there exists a class that has each of these as a ctor parameter, like so:</p>
<pre class="brush: csharp;">
public class UserController : Controller
{
	Users users = null;
	UserDepartments userDepartments = null;

	public UserController(IUsers users, IUserDepartments userDepartments)
	{
		this.users = users;
		this.userDepartments = userDepartments;
	}

	[HttpGet]
	pubic ActionResult Index()
	{
		// Note here that your instances of users and userDepartments has already been instantiated by StructureMap
		ViewData[&quot;UserDepartments&quot;] = userDepartments;
		return View(users);
	}
}
</pre>
<p>That is a quick run through so far of basic StructureMap wiring and working with ASP.NET MVC, hope this helps anyone out there just getting started with StructureMap.</p>
]]></content:encoded>
			<wfw:commentRss>http://robertschultz.org/2010/03/15/59-days-of-code-structuremap-dependency-injection-with-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Telerik Embraces Open Source with New Extentions for ASP.NET MVC</title>
		<link>http://robertschultz.org/2009/08/12/telerik-embraces-open-source-with-new-extentions-for-asp-net-mvc/</link>
		<comments>http://robertschultz.org/2009/08/12/telerik-embraces-open-source-with-new-extentions-for-asp-net-mvc/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 21:57:41 +0000</pubDate>
		<dc:creator>Robert Schultz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Telerik]]></category>

		<guid isPermaLink="false">http://www.robertschultz.org/?p=341</guid>
		<description><![CDATA[One of my biggest issues with ASP.NET MVC was it&#8217;s lack of built-in controls similar to what ships with ASP.NET WebForms.  This means when you want things like a TreeView or some sort of DataGrid you pretty much need to write it yourself.  Well now it seems as though companies are starting to push some [...]]]></description>
			<content:encoded><![CDATA[<p>One of my biggest issues with ASP.NET MVC was it&#8217;s lack of built-in controls similar to what ships with ASP.NET WebForms.  This means when you want things like a TreeView or some sort of DataGrid you pretty much need to write it yourself.  Well now it seems as though companies are starting to push some controls targeted at MVC.  Today Telerik <a href="http://www.telerik.com/company/press-center/company-news/telerik-embraces-open-source-with-new-extentions-for-asp-net-mvc.aspx" target="_blank">announced</a> their first CTP for <a href="http://www.telerik.com/products/aspnet-mvc.aspx" target="_blank">ASP.NET MVC extensions</a>.</p>
<p>Right now it seems pretty basic for what they offer but it&#8217;s a start.  You get an accordion, date picker, progress bar, slider, message box and tabs.  Even though you can already add these controls with some custom javascript, they take the work out of them and clean them up a bit.  In additional allowing you to bind to properties and methods from within ASP.NET.  This removes the need to have to have to implement ASP.NET data within your jQuery tags.</p>
<p>Best of all?  They&#8217;re open source!  Nice one Telerik.</p>
]]></content:encoded>
			<wfw:commentRss>http://robertschultz.org/2009/08/12/telerik-embraces-open-source-with-new-extentions-for-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multiple Roles with Authorize Attribute using Enums in ASP.NET MVC</title>
		<link>http://robertschultz.org/2009/07/29/multiple-roles-with-authorize-attribute-using-enums-in-asp-net-mvc/</link>
		<comments>http://robertschultz.org/2009/07/29/multiple-roles-with-authorize-attribute-using-enums-in-asp-net-mvc/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 15:49:18 +0000</pubDate>
		<dc:creator>Robert Schultz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.robertschultz.org/?p=255</guid>
		<description><![CDATA[In MVC, if you have ever used the [Authorize] attribute you will know that there are two options of using it by decorating your controller or action.  Either with pre-determined users: [Authorize(Users = &#34;Bob, John&#34;)] public class OrdersController : Controller { } Or with pre-determined roles: [Authorize(Roles = &#34;Admin, User&#34;)] public class OrdersController : Controller [...]]]></description>
			<content:encoded><![CDATA[<p>In MVC, if you have ever used the [Authorize] attribute you will know that there are two options of using it by decorating your controller or action.  Either with pre-determined users:</p>
<pre class="brush: csharp;">
[Authorize(Users = &quot;Bob, John&quot;)]
public class OrdersController : Controller
{
}
</pre>
<p>Or with pre-determined roles:</p>
<pre class="brush: csharp;">
[Authorize(Roles = &quot;Admin, User&quot;)]
public class OrdersController : Controller
{
}
</pre>
<p>But what if you want to not use untyped strings as your roles?  Specifying strings throughout the application to me seems unsafe especially if we are using these roles through hundreds of classes or action methods.  Well, the problem is if you have an Enum with your security roles like I do:</p>
<pre class="brush: csharp;">
public enum UserRoles
{
	Admin,
	User
}
</pre>
<p>You can&#8217;t specify multiple in this case because you can&#8217;t concatenate them properly.  This is where a custom AuthorizationAttribute comes in handy.  I created the following:</p>
<pre class="brush: csharp;">
public class MultipleEnumRolesAuthorizeAttribute : AuthorizeAttribute
{
	public new UserRoles Roles;

	protected override bool AuthorizeCore(HttpContextBase httpContext)
	{
		if (httpContext == null)
			throw new ArgumentNullException(&quot;httpContext&quot;);

		if (!httpContext.User.Identity.IsAuthenticated)
			return false;

		string[] userRoles = System.Web.Security.Roles.GetRolesForUser();
		foreach (string userRole in userRoles)
		{
			Constants.UserRoles role = (Constants.UserRoles) Enum.Parse(typeof(Constants.UserRoles), userRole);

			if ((Roles &amp; role) == role)
			{
				return true;
			}
		}

		return false;
	}
}
</pre>
<p>Now, all you have to do is decorate your controller or action with the new attribute and use bitwise operators to pass multiple roles.</p>
<pre class="brush: csharp;">
[MultipleEnumRolesAuthorizeAttribute(Roles = Constants.UserRoles.OrderManager | Constants.UserRoles.Admin)]
public class OrdersController : Controller
{
}
</pre>
<p>Hope this helps someone out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://robertschultz.org/2009/07/29/multiple-roles-with-authorize-attribute-using-enums-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
