PrimeDigit - A Design Blog by Will Shaver http://www.primedigit.com Thoughts on ASP.NET, Visual Design, SQL Server 2005, C# and much more... Mon, 27 Oct 2008 17:07:43 +0000 http://wordpress.org/?v=2.2.1 en Is this the .future you were expecting? http://www.primedigit.com/2008/10/27/is-this-the-future-you-were-expecting/ http://www.primedigit.com/2008/10/27/is-this-the-future-you-were-expecting/#comments Mon, 27 Oct 2008 17:07:10 +0000 Will http://www.primedigit.com/2008/10/27/is-this-the-future-you-were-expecting/ ICANN RFC for anything.* domain names. Might want to take a look. Most of you readers play in this here internets thing, this could effect you or the company you work for. :)

]]>
http://www.primedigit.com/2008/10/27/is-this-the-future-you-were-expecting/feed/
ActionFilter / ParameterBinder sequence http://www.primedigit.com/2008/10/19/actionfilter-parameterbinder-sequence/ http://www.primedigit.com/2008/10/19/actionfilter-parameterbinder-sequence/#comments Sun, 19 Oct 2008 19:23:23 +0000 Will http://www.primedigit.com/2008/10/19/actionfilter-parameterbinder-sequence/ GetBinder() 2. IModelBinder -> BindModel() 3. ActionFilterAttribute -> OnActionExecuting() 4. ActionFilterAttribute -> OnActionExecuted() 5. ActionFilterAttribute -> OnResultExecuting() 6. ActionFilterAttribute -> OnResultExecuted() (As of Beta 1) ]]> For future reference for myself and others:

1. CustomModelBinderAttribute -> GetBinder()
2. IModelBinder -> BindModel()
3. ActionFilterAttribute -> OnActionExecuting()
4. ActionFilterAttribute -> OnActionExecuted()
5. ActionFilterAttribute -> OnResultExecuting()
6. ActionFilterAttribute -> OnResultExecuted()

(As of Beta 1)

]]>
http://www.primedigit.com/2008/10/19/actionfilter-parameterbinder-sequence/feed/
Stack Overflow! MVC Model Binders Gone Awry http://www.primedigit.com/2008/10/19/stack-overflow-mvc-model-binders-gone-awry/ http://www.primedigit.com/2008/10/19/stack-overflow-mvc-model-binders-gone-awry/#comments Sun, 19 Oct 2008 17:00:17 +0000 Will http://www.primedigit.com/2008/10/19/stack-overflow-mvc-model-binders-gone-awry/ The recently released MVC Beta 1 has some pretty cool features. One of the features that I've been using for a while is Model Binders. These are now turned on by default which can cause some problems if you're not expecting this behavior. For example, say you have an action as follows:

        public ActionResult Binder(Dog dog)
        {
            return View("Index");
        }

Here's the model:

    public class Dog
    {
        public string Name { get; set; }
        public Dog cat { get; set; }
    }

Calling this action will cause a stack overflow error, as the default model binder will attempt to recreate every dog in the chain.

I would not have noticed this as I use my own implementation of the model binders to provide functionality for pulling an entity from NHibernate that is passed by ID via Ajax. The problem is that I recently switched over to the [Transaction]/ATM method of managing transactions with NHibernate. The problem with this method is that it creates a dynamic proxy of your controller so it can intercept the transaction. In doing so it looses the method argument attributes and then - boom - stack overflow. So now I'm off to hack YET ANOTHER open source project to see if I can get it to not remove the method parameter attributes. :(

]]>
http://www.primedigit.com/2008/10/19/stack-overflow-mvc-model-binders-gone-awry/feed/
Dead Wrong UI Design http://www.primedigit.com/2008/10/17/dead-wrong-ui-design/ http://www.primedigit.com/2008/10/17/dead-wrong-ui-design/#comments Fri, 17 Oct 2008 18:27:15 +0000 Will http://www.primedigit.com/2008/10/17/dead-wrong-ui-design/ I am a Microsoft Fan Boy. Today I'm going to show how good a friend I am and point out the bit of cilantro stuck between the teeth of Microsoft SQL Server Management Studio Express.

For this example I'm designing a new table in the management tool, using a bit field to indicate if a pet is dead or not. (Nevermind the obvious flaw that there are several types of dead.) Obviously I want a default value of NOT-DEAD or as we like to call it around here: 0.

Dead Bit
Ok, no problems so far. Lets put in some sample data:

Dead Wrong

Wait a minute now. You mean that when I specify the default value I must put a zero, but when I enter data into the table I have to type out 'false/true' for every entry? Ohh, and just in case you're curious no, you can't type 'false/true' in for the default value.
I'm sure none my readers would ever do anything like this. :)

(All 5 of you.)

]]>
http://www.primedigit.com/2008/10/17/dead-wrong-ui-design/feed/
Create an ISession specific cache http://www.primedigit.com/2008/09/26/create-an-isession-specific-cache/ http://www.primedigit.com/2008/09/26/create-an-isession-specific-cache/#comments Sat, 27 Sep 2008 00:11:25 +0000 Will http://www.primedigit.com/2008/09/26/create-an-isession-specific-cache/ Sometimes it is useful to create a cache specific to a particular session. I use this for pre-NH cache in my repositories that I can query with Linq before hitting NH sometimes. You could easily modify this to not be templated and instead work for any object...

If you think there is something wrong with this approach please let me know. :)

public class SessionCache<T>
    {
        private ISession sn;
        private IList<T> entities;
 
        private void ValidateSession(ISession session)
        {
            if(sn == session)
                return;
            sn = session;
            entities = new List<T>();
        }
 
        public void Add(ISession session, params T [] entity)
        {
            ValidateSession(session);
            foreach (T t in entity)
            {
                if(!Equals(t, default(T)) && !entities.Contains(t))
                    entities.Add(t);
            }
        }
 
        public void Remove(T entity, ISession session)
        {
            ValidateSession(session);
            if(entities.Contains(entity))
                entities.Remove(entity);
        }
 
        public T[] GetEntities(ISession session)
        {
            ValidateSession(session);
            return entities.ToArray();
        }
 
        public void Clear()
        {
            if(entities != null)
                entities.Clear();
        }
    }
]]>
http://www.primedigit.com/2008/09/26/create-an-isession-specific-cache/feed/
ModelStateDictionary and ParameterBinding http://www.primedigit.com/2008/09/09/modelstatedictionary-and-parameterbinding/ http://www.primedigit.com/2008/09/09/modelstatedictionary-and-parameterbinding/#comments Tue, 09 Sep 2008 18:14:38 +0000 Will http://www.primedigit.com/2008/09/09/modelstatedictionary-and-parameterbinding/ I laid this out in more detail on the forums here:
http://forums.asp.net/p/1317154/2610342.aspx#2610342

I thought I'd mention it to this group so that people were aware of this difference between the interaction the old ParameterBinders and Rescues in version 4 and the new ModelBinders and HandleErrorAttributes of version 5.

The new way of doing parameter binding expects all errors to be placed into the ModelStateDictionary specified in the GetValue method.
public override object GetValue(ControllerContext controllerContext, string modelName, Type modelType, ModelStateDictionary modelState) {

The ModelStateDictionary can then be checked in the controller for errors via ViewData.ModelState.IsValid

I think the idea is that multiple binding errors can then be reported to the user at one time:
foreach (var value in ViewData.ModelState.Values)
{
value.Errors.....
}

Unfortunatly our current NameValueDeserializer that is used by the DeserializeAttribute throws exceptions when it encounters errors. These exceptions are NOT caught by Rescues / HandleErrorAttributes. The ControllerActionInvoker (in preview 5) calls

IDictionary parameters = GetParameterValues(methodInfo);

before the try/catch block that passes off errors to the HandleErrorAttributes. So if you're using any kind of attribute deserialization and expecting your exceptions to be displayed all pretty like in your rescues... then move line #177 to line #180 of your ControllerActionInvoker.cs :)

-Will

]]>
http://www.primedigit.com/2008/09/09/modelstatedictionary-and-parameterbinding/feed/
Mvc Preview 5 Authorization Filters http://www.primedigit.com/2008/09/08/mvc-preview-5-authorization-filters/ http://www.primedigit.com/2008/09/08/mvc-preview-5-authorization-filters/#comments Mon, 08 Sep 2008 17:48:46 +0000 Will http://www.primedigit.com/2008/09/08/mvc-preview-5-authorization-filters/ Looks like the bits were changed some for preview 5 around the action filters. The 'Cancel' property was removed from the ActionExecutingContext. It looks like for my purposes I can use IAuthorizationFilter and FilterAttribute to get at an AuthorizationContext which has the cancel attribute.

]]>
http://www.primedigit.com/2008/09/08/mvc-preview-5-authorization-filters/feed/
Multi Monitor Wallpapers at FrozenTear.com http://www.primedigit.com/2008/08/28/multi-monitor-wallpapers-at-frozentearcom/ http://www.primedigit.com/2008/08/28/multi-monitor-wallpapers-at-frozentearcom/#comments Thu, 28 Aug 2008 07:06:03 +0000 Will http://www.primedigit.com/2008/08/28/multi-monitor-wallpapers-at-frozentearcom/ I've put up some of my multi monitor wallpapers at frozentear.com. If you develop on a 3200x1200 screen you might want to check this out. No promises, I'm a coder not an artist. :)

]]>
http://www.primedigit.com/2008/08/28/multi-monitor-wallpapers-at-frozentearcom/feed/
ISession Extension for Saving Multiple Objects http://www.primedigit.com/2008/08/26/isession-extension-for-saving-multiple-objects/ http://www.primedigit.com/2008/08/26/isession-extension-for-saving-multiple-objects/#comments Tue, 26 Aug 2008 22:36:51 +0000 Will http://www.primedigit.com/2008/08/26/isession-extension-for-saving-multiple-objects/ Incredibly simple, but quite useful:

        public static void Save(this ISession session, params object [] entities)
        {
            foreach (var o in entities)
            {
                session.Save(o);
            }
        }

You can use it like:

session.Save(a,b,c,d,....);

with a list of objects of any type. Useful for unit testing where you want to save multiple items at once.

[Update]

Probably should call it something other than Save, such as SaveAll or SaveList because if you only pass two items to the overload it calls the standard Save(object, int id) version. :(

]]>
http://www.primedigit.com/2008/08/26/isession-extension-for-saving-multiple-objects/feed/
Abstract Class Mappings http://www.primedigit.com/2008/08/20/abstract-class-mappings/ http://www.primedigit.com/2008/08/20/abstract-class-mappings/#comments Wed, 20 Aug 2008 19:05:50 +0000 Will http://www.primedigit.com/2008/08/20/abstract-class-mappings/ If you're running into this error in NHibernate:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: Attempting to parse a null value into an sql string.

Examine your mappings for abstract classes that aren't implemented, or classes that are marked as abstract in the mappings but not in the c# file. I was refactoring out some abstract classes in the middle and forgot to delete the abstract="true" line in the mapping.

]]>
http://www.primedigit.com/2008/08/20/abstract-class-mappings/feed/