Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a convention for IRepository<T> with StructureMap mapping

Tags:

structuremap

Is there a way in StructureMap to do this kind of repetitive mapping with one line or a convention?

    For<IRepository<Mailout>>().Use<MailoutRepository>();
    For<IRepository<MailServer>>().Use<MailServerRepository>();
    For<IRepository<MailoutStatus>>().Use<MailoutStatusRepository>();
    For<IRepository<MailoutTemplate>>().Use<MailoutTemplateRepository>();
    For<IRepository<Publication>>().Use<PublicationRepository>();
    For<IRepository<Recipient>>().Use<RecipientRepository>();
like image 394
ssmith Avatar asked Jun 30 '11 14:06

ssmith


1 Answers

To map IRepository<Mailout> to MailoutRepository, use:

var c = new Container(x =>
{
    x.Scan(scan =>
    {
        // there are other options to expand which assemblies to scan for types
        scan.TheCallingAssembly(); 
        scan.ConnectImplementationsToTypesClosing(typeof (IRepository<>));
    });
});

To map IRepository<Mailout> to Repository<Mailout>, use:

var c = new Container(x =>
{
    x.For(typeof (IRepository<>)).Use(typeof (Repository<>));
});
like image 55
Joshua Flanagan Avatar answered Sep 25 '22 08:09

Joshua Flanagan