LessThanDot Site Logo

LessThanDot

A decade of helpful technical content

This is an archive of the posts published to LessThanDot from 2008 to 2018, over a decade of useful content. While we're no longer adding new content, we still receive a lot of visitors and wanted to make sure the content didn't disappear forever.

VB.Net: Setter injection with StructureMap and the FI

I’m using the SVN version for this. Lets give an example of how to do this. First we create a few interfaces. Public Interface IPerson Function Name() As String End Interface``` ```vbnet Public Interface ITest Function Name() As String Property Person() As IPerson End Interface``` as we can see ITest has a property Person of type IPerson. Ideally we would like to inject the IPerson. Then we have the concrete classes. ```vbnet Public Class Person Implements IPerson Public Function Name() As String Implements IPerson.Name Return "Person" End Function End Class``` ```vbnet Public Class Test Implements ITest Private _Person As IPerson Public Sub New() End Sub Public Function Name() As String Implements ITest.Name Return "test" End Function Public Property Testprop() As IPerson Implements ITest.Person Get Return _Person End Get Set(ByVal value As IPerson) _Person = value End Set End Property End Class``` Now I want to inject the Person that I have already made with structureMap. ```vbnet Dim _Registry As StructureMap.Configuration.DSL.Registry _Registry.ForRequestedType(Of IPerson).TheDefault.Is.OfConcreteType(Of Person).WithName("default") _Registry.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test).WithName("default").SetterDependency(Of IPerson).Is(Function(e) e.TheDefault) Dim _Container As New StructureMap.IContainer(_Registry)``` The first line makes a new registry. The second line adds a Iperson to the registry with as concretetype Person that is also the default for IPerson with a name of default. The third line adds an ITest to the registry with as concretetype Test that is also the default for ITest with a name of default and a setterdepency on IPerson that wants the default implementation of IPerson which we declared in line 2. Line 4 creates the container. Having done all this we can now see the result via console.writeline (our favorite testrunner ;-)) ```vbnet Console.WriteLine(_container.GetInstance(Of ITest).Name) Console.WriteLine(_container.GetInstance(Of ITest).Person.Name) Console.WriteLine(_container.GetInstance(Of IPerson).Name)``` This should give the following as a result. > test > Person > Person Simple enough. Very usefull for when you want to inject your observable into a usercontrol. Usercontrols should have a default constructor for the designer, actually it is also the only constructor because the designer can have a difficult time otherwise. This is actually the only reason why I use Setterinjection instead of constructorinjection.

Read More...

VB.Net: A better configuration for structureMap

There used to be time when you would configure StructureMap this way StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)()``` and then this to get your implementation ```vbnet ObjectFactory.GetInstance(Of ITest)()``` Fine that all worked nice and dandy but it had a drawback. Structuremapconfiguration was static/shared. This meant that this. ```vbnet StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)() ObjectFactory.GetInstance(Of ITest) StructureMap.StructureMapConfiguration.ForRequestedType(Of ITest).TheDefault.Is.OfConcreteType(Of Test)() ObjectFactory.GetInstance(Of ITest)``` would lead to an error because you could not add classes to structuremap once objectfactory.getinstance was called. But [Jeremy and his compadres (sorry musketeers)][1] to the rescue. In the new svn version (not version 2.4.9 apparently) you can do this. ```vbnet #Region " Private members " Imports TDB2007.Utils.CommonFunctions.IoC.Interfaces Imports StructureMap Namespace IoC.StructureMapIoC ''' <summary> ''' ''' </summary> ''' <remarks></remarks> Public Class StructureMapContainer Implements IoC.Interfaces.IContainerIoC #Region " Private members " ''' <summary> ''' ''' </summary> ''' <remarks></remarks> Private _Container As StructureMap.IContainer ''' <summary> ''' ''' </summary> ''' <remarks></remarks> Private _Registry As StructureMap.Configuration.DSL.Registry #End Region #Region " Constructors " ''' <summary> ''' ''' </summary> ''' <remarks></remarks> Public Sub New() _Registry = New StructureMap.Configuration.DSL.Registry _Container = New StructureMap.Container(_Registry) End Sub #End Region #Region " Public methods " ''' <summary> ''' ''' </summary> ''' <typeparam name="T"></typeparam> ''' <returns></returns> ''' <remarks></remarks> Public Function Resolve(Of T)() As T Implements IContainerIoC.Resolve Return _Container.GetInstance(Of T)() End Function ''' <summary> ''' ''' </summary> ''' <typeparam name="T"></typeparam> ''' <param name="ImplamentationName"></param> ''' <returns></returns> ''' <remarks></remarks> Public Function Resolve(Of T)(ByVal ImplamentationName As String) As T Implements IContainerIoC.Resolve Return _Container.GetInstance(Of T)(ImplamentationName) End Function ''' <summary> ''' ''' </summary> ''' <typeparam name="T"></typeparam> ''' <param name="Implemtation"></param> ''' <remarks></remarks> Public Sub InjectImplementation(Of T)(ByVal Implemtation As T) Implements IContainerIoC.InjectImplementation _Container.Inject(Of T)(Implemtation) End Sub ''' <summary> ''' ''' </summary> ''' <typeparam name="T"></typeparam> ''' <param name="ImplementationName"></param> ''' <remarks></remarks> Public Sub SetDefaultInstanceName(Of T)(ByVal ImplementationName As String) Implements IContainerIoC.SetDefaultInstanceName _Container.SetDefault(GetType(T), ImplementationName) End Sub ''' <summary> ''' ''' </summary> ''' <typeparam name="INPUTTYPE"></typeparam> ''' <typeparam name="CONCRETETYPE"></typeparam> ''' <param name="name"></param> ''' <param name="IsDefault"></param> ''' <remarks></remarks> Public Sub Register(Of INPUTTYPE, CONCRETETYPE As {INPUTTYPE})(Optional ByVal name As String = "default", Optional ByVal IsDefault As Boolean = True) Implements Interfaces.IContainerIoC.Register If IsDefault Then _Registry.ForRequestedType(Of INPUTTYPE).TheDefault.Is.OfConcreteType(Of CONCRETETYPE).WithName(name) Else _Registry.BuildInstancesOf(Of INPUTTYPE).AddConcreteType(Of CONCRETETYPE)(name) End If _Container = New StructureMap.Container(_Registry) End Sub ''' <summary> ''' ''' </summary> ''' <typeparam name="INPUTTYPE"></typeparam> ''' <typeparam name="CONCRETETYPE"></typeparam> ''' <param name="name"></param> ''' <param name="IsDefault"></param> ''' <remarks></remarks> Public Sub RegisterSingleton(Of INPUTTYPE, CONCRETETYPE As {INPUTTYPE})(Optional ByVal name As String = "default", Optional ByVal IsDefault As Boolean = True) Implements Interfaces.IContainerIoC.RegisterSingleton If IsDefault Then _Registry.ForRequestedType(Of INPUTTYPE).AsSingletons.TheDefault.Is.OfConcreteType(Of CONCRETETYPE).WithName(name) Else _Registry.ForRequestedType(Of INPUTTYPE).AsSingletons.AddConcreteType(Of CONCRETETYPE)(name) End If _Container = New StructureMap.Container(_Registry) End Sub ''' <summary> ''' ''' </summary> ''' <remarks></remarks> Public Sub ResetContainer() Implements IContainerIoC.ResetContainer _Registry = New StructureMap.Configuration.DSL.Registry _Container = New StructureMap.Container(_Registry) End Sub #End Region End Class End Namespace``` Or something equally brilliant. You can now add things to the container even after calling resolve. You can find more of this [Using the StructureMap Container independently of ObjectFactory by Jeremy][2] and [Comparing .NET DI (IoC) Frameworks, Part 2 by Andrey Shchekin][3] [1]: http://codebetter.com/blogs/jeremy.miller/ [2]: http://codebetter.com/blogs/jeremy.miller/archive/2008/09/10/using-the-structuremap-container-independently-of-objectfactory.aspx [3]: http://blog.ashmind.com/index.php/2008/09/08/comparing-net-di-ioc-frameworks-part-2/

Read More...

Why singletons are bad

I didn’t know that google actually had a policy about it, but they explain very well why you should avoid them. They even have an application that searches for singletons in code. Here is a little extract of that article. The use of singletons is actually a fairly controversial subject in the Java community; what was once an often-used design pattern is now being looked at as a less than desirable coding practice. The problem with singletons is that they introduce global state into a program, allowing anyone to access them at anytime (ignoring scope). Even worse, singletons are one of the most overused design patterns today, meaning that many people introduce this possibly detrimental global state in instances where it isn’t even necessary. What’s wrong with singletons’ use of global state?

Read More...

Interview With Louis Davidson Author of Pro SQL Server 2008 Relational Database Design and Implementation

Pro SQL Server 2008 Relational Database Design and Implementation is one of those books that should be in the hands of every SQL Server developer. There are tons of SQL Server programming books around but none of them covers the fundamentals of a good SQL Server database, the design. If your design is ‘broken’ then it is a lot harder to fix it down the road, design is probably the number one reason people have all kinds of problems with their databases.

Read More...

Nice .NET Design Patterns Articles

The DotNetSlackers site has a nice series of articles about design patterns. The articles were written by Granville Barnett and are a very good read. Here is what is in the first four articles. Design Patterns – Part 1 Learn how to design more robust and maintainable code by incorporating design patterns into your software projects. 1 Introduction 2 The strategy pattern 3 Implementing our design 4 Summary Design Patterns – Part 2

Read More...

Behaviour-driven development and User stories

On Dan North‘s blog you will find a nice writeup on this subject the post is called What’s in a Story? and explains it a bit better. Compare to use cases where you describe the interaction between the stakeholder and the other stakeholder (mainly the system) this describes the desired behaviour of the system. This is an example of such a use case. Found here. Use Case Example Example for the Club Information System (CIS)

Read More...

Google Chrome Dev Channel: Early Access to Features and Fixes

If you want to get the new Chrome features as soon as possible then you are in luck; Google has launched the Google Chrome Dev Channel. The Google Chrome Dev Channel will give you early access to features and fixes. The Dev channel is useful for: Web developers who want to test their sites or scripts with pre-release versions of Google Chrome Testers who need to make sure they’re working with the latest bug fixes

Read More...

DATAllegro acquisition to enable SQL Server to scale into hundreds of terabytes of data

Microsoft just announced that they have finalized the DATAllegro acquisition: Microsoft Closes Acquisition of DATAllegro Some interesting thing in this press release: Microsoft will offer a new solution based on DATAllegro’s technology that extends Microsoft SQL Server to scale into hundreds of terabytes of data. The company will begin giving customers and partners early access to the combined solution through community technology previews (CTPs) within the next 12 months, with full product availability scheduled for the first half of calendar year 2010.

Read More...

FIX for A MERGE statement may not enforce a foreign key constraint when the statement updates a unique key column that is not part of a clustering key bug

FIX: A MERGE statement may not enforce a foreign key constraint when the statement updates a unique key column that is not part of a clustering key and there is a single row as the update source in SQL Server 2008 In Microsoft SQL Server 2008, a foreign key constraint may not be enforced when the following conditions are true: • A MERGE statement is issued. • The target column of the update has a nonclustered unique index.

Read More...

One Second Caching, Brilliant Or Insane?

Let’s say you get 1000 hits per second on your website’s home page, you want the users to feel that the site is as real time as possible without overloading your database. These hits on your site display some data from the database and are mostly reads; for every 10 reads there is one write. Yes you can cache it for 1 minute and the database won’t be hit that much. By doing this it will look like your site doesn’t update so often. Now what will happen if you do a 1 second cache? You will eliminate most of the reads and your site will still look like it is updating real time. If a user hits F5/CTRL + R every couple of seconds he will see new content. Does this make sense? I am interested in your opinion.

Read More...