2

Using Castle.Windsor in ASP.NET MVC (3.0) is there any way I can appropriately handle if one of my dependencies resolves null? For instance, say I have a IMembershipService.

class ServiceInstaller
{
 // ... 
 void Install( // .. )
 { 
  container.Register(
  Component
  .For<IMembershipService>()
  .ImplementedBy<MembershipService>()
  .LifeStyle.PerWebRequest
  );
 }
}

Okay, this works great. Now, perhaps not all of my site requires a user to be logged in. Let's assume that maybe my web host's database server crashes for a few hours. In that event, things that looked into the database, or tried to call on my ISession might return null.

What can I do in this case? I can write if(membershipService == null) a hundred times over, but that seems pretty dumb. Is there a built-in solution to say "Hey, if we have an error, do this..?"

1
  • If your DB server crashes, your DB queries should throw, never return null. Commented Feb 7, 2011 at 15:31

2 Answers 2

4

I think that the service should never be null. If the database is down, the service should be returned nevertheless, but its methods should throw an exception, return null or some default value, depending on the semantic of the service.

Sign up to request clarification or add additional context in comments.

11 Comments

Regardless, there needs to be a way to handle the situation of it not finding what it is looking for. Is there any way to set up a default behavior if the service does not return correctly?
@Stacey: can you name a concrete, plausible example?
@Mauricio: Anything has the potential to error... that's a very broad spectrum. Castle.Windsor has nothing to just handle improper resolution? Property injection, to name one. Trying to set up a property such as a logger that is not log4net, which I'm not using, could resolve null if there is no file to write to due to an unforseen circumstance like a permissions error. Or trying to resolve information about a logged in user can return null on Controller destruction. In fact that's the very problem that brought me to trying to figure this out. Castle doesn't give any way to see if something
is null when it 'Releases' a controller, and as such continues to throw problems.
What I am trying to say is that I don't think it is the responsibility of the container to handle such errors. The service creation should always succeed, and then it is the responsibility of the service to handle errors.
|
0

Ciel, i had this problem just recently and found your question while looking for the answer.

Basically you should be using a typed factory to resolve your components at runtime in your wrapping component. The factory should return a default object if there is no match regarding the component you're looking for, default object that would implement whatever behavior is needed.

In the case of your IMembershipService, implement a NotCheckingMembershipService class inheriting the interface and doing nothing and make it the default for the components that won't need it. More specific membership services can be linked to specific controllers.

To do so you must create a generic "catch-all" implementation

public class NotCheckingMembershipService<T>: IMembershipService<T> where T: Controller
{
}

And register it as the default component for an open IMembershipService

_container.Register(
            Component.For(typeof(IMembershipService<>))
            .ImplementedBy(typeof(NotCheckingMembershipService<>))
            .IsDefault());

Then simply register your custom membership services where needed. Resolution won't fail and you will always be able to call the interface.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.