Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [303]

By Root 5562 0
out of scope.

HierarchicalLifetimeManager

New in Unity 2.0, implements a singleton behavior for objects. However, child containers don’t share instances with parents.

PerResolveLifetimeManager

New in Unity 2.0, implements a behavior similar to the transient lifetime manager except that instances are reused across build-ups of the object graph.

PerThreadLifetimeManager

Implements a singleton behavior for objects, but it’s limited to the current thread.

TransientLifetimeManager

Returns a new instance of the requested type for each call. This is the default behavior.

You can also create custom managers by inheriting the LifetimeManager base class.

Here’s how you set a lifetime manager in code:

container

.RegisterType(

"Tracing",

new ContainerControlledLifetimeManager());

Here’s what you need, instead, to set a lifetime manager declaratively:

Note, however, that the word singleton you assign to the type attribute is not a keyword or a phrase with a special meaning. More simply, it is intended to be an alias for a type that must be declared explicitly:

type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,

Microsoft.Practices.Unity" />

type="Microsoft.Practices.Unity.PerThreadLifetimeManager,

Microsoft.Practices.Unity" />

type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager,

Microsoft.Practices.Unity" />

...

type="MyApplication.MyTypes.MyInterface, MyApplication.MyTypes" />

...

After you have the aliases all set, you can use alias names in the section where you register types.

Summary


Just as an architect designing a house wouldn’t ignore building codes that apply to the context, a software architect working in an object-oriented context shouldn’t ignore principles of software design such as the SOLID principles discussed in this chapter when designing a piece of software. Proper application of these principles leads straight to writing software that is far easier to maintain and fix. It keeps the code readable and understandable and makes it easier to test, both during development and for refactoring and extensibility purposes.

Most developers (and even more managers) commonly think that using software principles is first and foremost a waste of time and that no distinction is actually possible between “well-designed code that works” and “software that just works.” Guess what? I totally agree with this statement. If you don’t need design, any effort is overdesign. And overdesign is an anti-pattern.

So you save a lot of time by skipping over principles. However, if your “software that just works” has to be fixed or extended one day, be aware that you will find yourself in a serious mess. The costs at that point will be much higher. It all depends on the expected lifespan of the application. Ideally, you learn principles and make them a native part of your skill set so that you use them all the time in a natural way. Otherwise, the costs of applying principles will always be too high to seem effective. Ad hoc tools can help a lot in making the development of good code more sustainable. IoC frameworks are just one of these tools.

In the next chapter, I’ll continue with the theme of application design by tackling layers (and communication related to them) in ASP.NET applications.

Chapter 14. Layers of an Application


The advantage of a bad memory is that one enjoys several times the same good things for the first time.

—Friedrich Nietzsche

Any software of any reasonable complexity is best designed if organized in layers. Each layer represents a logical section of the system. A layer is hosted on a physical tier (for example, a server machine). Multiple layers can be hosted on the same tier, and each layer can optionally be moved to a separate tier at any

Return Main Page Previous Page Next Page

®Online Book Reader