Conditionally Create Instance

222 views
Skip to first unread message

Steven Smith

unread,
Feb 14, 2011, 10:54:33 AM2/14/11
to structure...@googlegroups.com
I'm looking to pass back a Cached implementation of an object to all
requests for the type, except for the request made by the Cached
implementation itself - that should pull back the implementation that
fetches the data from its source.

I've tried this:

x.For<IAlbumRepository>().Use<CachedAlbumRepository>();

x.For<CachedAlbumRepository>().Use<CachedAlbumRepository>().Ctor<IAlbumRepository>().Is
<EfAlbumRepository>();

It fails with a "No default constructore defined" exception.

I started down this path:

x.For<IAlbumRepository>().Add(
c =>
{
if ( %%TheRequestingType%% is
CachedAlbumRepository)
{
return new
CachedAlbumRepository(new EfAlbumRepository());
}
return new EfAlbumRepository();
});

but I can't seem to figure out how to write a conditional that is
based on the type doing the requesting.

Any advice?

Thanks,
Steve


--
Steve Smith
http://SteveSmithBlog.com/
http://twitter.com/ardalis

Kyle Malloy

unread,
Feb 15, 2011, 10:40:43 AM2/15/11
to structure...@googlegroups.com
All you would need is the following configuration. To achieve the decorator pattern.

x.For<IAlbumRepository>().Use<CachedAlbumRepository>()
 .Ctor<IAlbumRepository>().Is<EfAlbumRepository>();

Hopes this helps,
~Kyle


--
You received this message because you are subscribed to the Google Groups "structuremap-users" group.
To post to this group, send email to structure...@googlegroups.com.
To unsubscribe from this group, send email to structuremap-us...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/structuremap-users?hl=en.


Steven Smith

unread,
Feb 15, 2011, 11:48:22 AM2/15/11
to structure...@googlegroups.com
Awesome, thanks! That works perfectly.

Steve

Nitin Ved

unread,
Jul 25, 2013, 1:23:40 PM7/25/13
to structure...@googlegroups.com
I had a question about this. If EfAlbumRepository requires a parameter for its constructor. How would I configure it ?
 
Adding the extra line for concrete class does not seem to work. I still get error Missing requested Instance property "fooInput" for InstanceKey "81f439f7-2d00-46a3-bc6a-53c0fe2bc77a"  when requesting an instance for IAlbumRepository from ObjectFactory.
 
x.For<IAlbumRepository>().Use<CachedAlbumRepository>().Ctor<IAlbumRepository>().Is<EfAlbumRepository>();
For<EfAlbumRepository>().Use<EfAlbumRepository>().Ctor<FooType>("fooInput").Is(foobar);

Steve

>> structuremap-users+unsub...@googlegroups.com.


>> For more options, visit this group at
>> http://groups.google.com/group/structuremap-users?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "structuremap-users" group.
> To post to this group, send email to structure...@googlegroups.com.
> To unsubscribe from this group, send email to

> structuremap-users+unsub...@googlegroups.com.

Scott

unread,
Jun 23, 2014, 4:53:02 PM6/23/14
to structure...@googlegroups.com
If I had a generic repository setup:

IRepository<T>, Repository<T>, CachedRepository<T>

How would I set up StructureMap in the same way? I can't use Ctor<> because it doesn't have a Ctor(type theType) overload, and you can't have "Ctor<IRepository<>>" since that produces a compiler error.

Thanks!
Reply all
Reply to author
Forward
0 new messages