Skip to content

Commit 5f2e009

Browse files
committed
Updated StructureMap as Transient by default and lifecycle is parametrizable
1 parent 93d2978 commit 5f2e009

6 files changed

Lines changed: 105 additions & 60 deletions

File tree

SharpRepository.Ioc.Mvc/MvcDependencyResolver.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using SharpRepository.Repository.Configuration;
55
using SharpRepository.Repository.Ioc;
66
using StructureMap;
7+
using StructureMap.Pipeline;
78
using System;
89
using System.Web.Http;
910
using System.Web.Mvc;
@@ -12,7 +13,14 @@ namespace SharpRepository.Ioc.Mvc
1213
{
1314
public static class MvcDependencyResolver
1415
{
15-
public static void ForRepositoriesUseSharpRepository(string jsonConfigurationFileName, string sharpRepositoryConfigurationSectionName, string repoisitoryName = null)
16+
/// <summary>
17+
/// Configures DependencyResolver with configured SharpRepository as implementation of IRepository and ICompoundRepository instances
18+
/// </summary>
19+
/// <param name="jsonConfigurationFileName"></param>
20+
/// <param name="sharpRepositoryConfigurationSectionName"></param>
21+
/// <param name="repositoryName">name of repository implementation in configuration, null tell to use default in configuration</param>
22+
/// <param name="lifecycle">StructureMap coping of variables default is Lifecycle.Transient</param>
23+
public static void ForRepositoriesUseSharpRepository(string jsonConfigurationFileName, string sharpRepositoryConfigurationSectionName, string repoisitoryName = null, ILifecycle lifecycle = null)
1624
{
1725
var config = new ConfigurationBuilder()
1826
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
@@ -22,10 +30,16 @@ public static void ForRepositoriesUseSharpRepository(string jsonConfigurationFil
2230
var section = config.GetSection(sharpRepositoryConfigurationSectionName);
2331
var sharpConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(section);
2432

25-
ForRepositoriesUseSharpRepository(sharpConfig, repoisitoryName);
33+
ForRepositoriesUseSharpRepository(sharpConfig, repoisitoryName, lifecycle);
2634
}
2735

28-
public static void ForRepositoriesUseSharpRepository(ISharpRepositoryConfiguration sharpConfig, string repositoryName = null)
36+
/// <summary>
37+
/// Configures DependencyResolver with configured SharpRepository as implementation of IRepository and ICompoundRepository instances
38+
/// </summary>
39+
/// <param name="sharpConfig"></param>
40+
/// <param name="repositoryName">name of repository implementation in configuration, null tell to use default in configuration</param>
41+
/// <param name="lifecycle">StructureMap coping of variables default is Lifecycle.Transient</param>
42+
public static void ForRepositoriesUseSharpRepository(ISharpRepositoryConfiguration sharpConfig, string repositoryName = null, ILifecycle lifecycle = null)
2943
{
3044
var container = new Container(c =>
3145
{
@@ -37,13 +51,12 @@ public static void ForRepositoriesUseSharpRepository(ISharpRepositoryConfigurati
3751
s.WithDefaultConventions();
3852
});
3953

40-
c.ForRepositoriesUseSharpRepository(sharpConfig, repositoryName);
54+
c.ForRepositoriesUseSharpRepository(sharpConfig, repositoryName, lifecycle);
4155
});
4256

4357
var dependencyResolver = new StructureMapDependencyResolver(container);
4458
DependencyResolver.SetResolver(dependencyResolver);
4559
GlobalConfiguration.Configuration.DependencyResolver = dependencyResolver;
46-
4760
RepositoryDependencyResolver.SetDependencyResolver(new StructureMapRepositoryDependencyResolver(container));
4861
}
4962
}

SharpRepository.Ioc.StructureMap/StructureMapExtensions.cs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,50 @@
22
using SharpRepository.Repository;
33
using SharpRepository.Repository.Configuration;
44
using StructureMap;
5+
using StructureMap.Pipeline;
56

67
namespace SharpRepository.Ioc.StructureMap
78
{
89
public static class StructureMapExtensions
910
{
10-
public static void ForRepositoriesUseSharpRepository(this ConfigurationExpression initialization, ISharpRepositoryConfiguration configuration, string repositoryName = null)
11+
/// <summary>
12+
/// Configures StructureMap container telling to resolve IRepository and ICompoundKeyRepository with the repository from configuration
13+
/// </summary>
14+
/// <param name="init"></param>
15+
/// <param name="configuration"></param>
16+
/// <param name="repositoryName">name of repository implementation in configuration, null tell to use default in configuration</param>
17+
/// <param name="lifecycle">StructureMap coping of variables default is Lifecycle.Transient</param>
18+
public static void ForRepositoriesUseSharpRepository(this ConfigurationExpression init, ISharpRepositoryConfiguration configuration, string repositoryName = null, ILifecycle lifeCycle = null)
1119
{
12-
initialization.Scan(scan => {
20+
init.Scan(scan => {
1321
scan.IncludeNamespaceContainingType<IAmInRepository>();
1422
scan.WithDefaultConventions();
1523
});
16-
17-
initialization.For(typeof(IRepository<>)).Singleton();
18-
initialization.For(typeof(IRepository<,>)).Singleton();
19-
initialization.For(typeof(ICompoundKeyRepository<,,>)).Singleton();
20-
initialization.For(typeof(ICompoundKeyRepository<,,,>)).Singleton();
21-
initialization.For(typeof(ICompoundKeyRepository<>)).Singleton();
2224

23-
initialization.For(typeof(IRepository<>)).Use(new RepositoryNoKeyInstanceFactory(configuration, repositoryName));
24-
initialization.For(typeof(IRepository<,>)).Use(new RepositorySingleKeyInstanceFactory(configuration, repositoryName));
25-
initialization.For(typeof(ICompoundKeyRepository<,,>)).Use(new RepositoryDoubleKeyInstanceFactory(configuration, repositoryName));
26-
initialization.For(typeof(ICompoundKeyRepository<,,,>)).Use(new RepositoryTripleKeyInstanceFactory(configuration, repositoryName));
27-
initialization.For(typeof(ICompoundKeyRepository<>)).Use(new RepositoryCompoundKeyInstanceFactory(configuration, repositoryName));
25+
if (lifeCycle == null)
26+
{
27+
lifeCycle = Lifecycles.Transient;
28+
}
29+
30+
init.For(typeof(IRepository<>))
31+
.LifecycleIs(lifeCycle)
32+
.Use(new RepositoryNoKeyInstanceFactory(configuration, repositoryName));
33+
34+
init.For(typeof(IRepository<,>))
35+
.LifecycleIs(lifeCycle)
36+
.Use(new RepositorySingleKeyInstanceFactory(configuration, repositoryName));
37+
38+
init.For(typeof(ICompoundKeyRepository<,,>))
39+
.LifecycleIs(lifeCycle)
40+
.Use(new RepositoryDoubleKeyInstanceFactory(configuration, repositoryName));
41+
42+
init.For(typeof(ICompoundKeyRepository<,,,>))
43+
.LifecycleIs(lifeCycle)
44+
.Use(new RepositoryTripleKeyInstanceFactory(configuration, repositoryName));
45+
46+
init.For(typeof(ICompoundKeyRepository<>))
47+
.LifecycleIs(lifeCycle)
48+
.Use(new RepositoryCompoundKeyInstanceFactory(configuration, repositoryName));
2849
}
2950
}
3051
}

SharpRepository.Samples.MVC5/Controllers/ContactsController.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public ContactsController(IRepository<Contact, int> repository)
1717
this.repository = repository;
1818
}
1919

20+
protected override void Dispose(bool disposing)
21+
{
22+
if (disposing)
23+
{
24+
repository.Dispose();
25+
}
26+
27+
base.Dispose(disposing);
28+
}
29+
2030
// GET: Contacts
2131
public ActionResult Index()
2232
{

SharpRepository.Samples.MVC5/Global.asax.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using SharpRepository.Ioc.Mvc;
22
using SharpRepository.Samples.MVC5.Models;
33
using StructureMap;
4+
using StructureMap.Pipeline;
45
using System;
56
using System.Collections.Generic;
67
using System.Data.Entity;
@@ -21,7 +22,7 @@ protected void Application_Start()
2122
RouteConfig.RegisterRoutes(RouteTable.Routes);
2223
BundleConfig.RegisterBundles(BundleTable.Bundles);
2324
// MvcDependencyResolver.ForRepositoriesUseSharpRepository("repository.json", "sharpRepository", "efConnectionString"); // holds connection string on repository.json
24-
MvcDependencyResolver.ForRepositoriesUseSharpRepository("repository.json", "sharpRepository"); // no connection string on repository.json
25+
MvcDependencyResolver.ForRepositoriesUseSharpRepository("repository.json", "sharpRepository", lifecycle: Lifecycles.Unique); // no connection string on repository.json
2526
}
2627
}
2728

@@ -32,7 +33,7 @@ public class DbContexRegistry : Registry
3233
{
3334
public DbContexRegistry()
3435
{
35-
For<DbContext>().Use(new ContactsDbContext("name=ContactsDbContext"));
36+
For<DbContext>().Use(() => new ContactsDbContext("name=ContactsDbContext"));
3637
}
3738
}
3839
}

SharpRepository.Samples.MVC5/Models/ContactsDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class ContactsDbContext : DbContext
66
{
77
public ContactsDbContext(string connectionString) : base(connectionString)
88
{
9-
9+
var cs = connectionString;
1010
}
1111

1212
public virtual DbSet<Contact> Contacts { get; set; }
Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
<?xml version="1.0"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<!--
33
For more information on how to configure your ASP.NET application, please visit
44
https://go.microsoft.com/fwlink/?LinkId=301880
55
-->
66
<configuration>
77
<configSections>
88
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
9-
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
9+
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
1010
</configSections>
1111
<connectionStrings>
12-
<add name="ContactsDbContext" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=SharpRepositoryMVC5Contacts; Integrated Security=True; MultipleActiveResultSets=True;" providerName="System.Data.SqlClient"/>
12+
<add name="ContactsDbContext" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=SharpRepositoryMVC5Contacts; Integrated Security=True; MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
1313
</connectionStrings>
1414
<appSettings>
15-
<add key="webpages:Version" value="3.0.0.0"/>
16-
<add key="webpages:Enabled" value="false"/>
17-
<add key="ClientValidationEnabled" value="true"/>
18-
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
15+
<add key="webpages:Version" value="3.0.0.0" />
16+
<add key="webpages:Enabled" value="false" />
17+
<add key="ClientValidationEnabled" value="true" />
18+
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
1919
</appSettings>
2020
<!--
2121
Per una descrizione delle modifiche al file web.config, vedere il sito Web all'indirizzo http://go.microsoft.com/fwlink/?LinkId=235367.
@@ -26,75 +26,75 @@
2626
</system.Web>
2727
-->
2828
<system.web>
29-
<compilation debug="true" targetFramework="4.5.1"/>
30-
<httpRuntime targetFramework="4.5.1"/>
29+
<compilation debug="true" targetFramework="4.5.1" />
30+
<httpRuntime targetFramework="4.5.1" />
3131
<httpModules>
32-
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
32+
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
3333
</httpModules>
3434
</system.web>
3535
<runtime>
3636
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
3737
<dependentAssembly>
38-
<assemblyIdentity name="System.Security.Cryptography.Primitives" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/>
39-
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0"/>
38+
<assemblyIdentity name="System.Security.Cryptography.Primitives" publicKeyToken="B03F5F7F11D50A3A" culture="neutral" />
39+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
4040
</dependentAssembly>
4141
<dependentAssembly>
42-
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
43-
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0"/>
42+
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
43+
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
4444
</dependentAssembly>
4545
<dependentAssembly>
46-
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/>
47-
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
46+
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
47+
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
4848
</dependentAssembly>
4949
<dependentAssembly>
50-
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/>
51-
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/>
50+
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
51+
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
5252
</dependentAssembly>
5353
<dependentAssembly>
54-
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
55-
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
54+
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
55+
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
5656
</dependentAssembly>
5757
<dependentAssembly>
58-
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
59-
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
58+
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
59+
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
6060
</dependentAssembly>
6161
<dependentAssembly>
62-
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
63-
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
62+
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
63+
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
6464
</dependentAssembly>
6565
<dependentAssembly>
66-
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
67-
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0"/>
66+
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
67+
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
6868
</dependentAssembly>
6969
<dependentAssembly>
70-
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
71-
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0"/>
70+
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
71+
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
7272
</dependentAssembly>
7373
</assemblyBinding>
7474
</runtime>
7575
<system.webServer>
76-
<validation validateIntegratedModeConfiguration="false"/>
76+
<validation validateIntegratedModeConfiguration="false" />
7777
<modules>
78-
<remove name="ApplicationInsightsWebTracking"/>
79-
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler"/>
78+
<remove name="ApplicationInsightsWebTracking" />
79+
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
8080
</modules>
8181
<handlers>
82-
<remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
83-
<remove name="OPTIONSVerbHandler"/>
84-
<remove name="TRACEVerbHandler"/>
85-
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
82+
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
83+
<remove name="OPTIONSVerbHandler" />
84+
<remove name="TRACEVerbHandler" />
85+
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
8686
</handlers>
8787
</system.webServer>
8888
<system.codedom>
8989
<compilers>
90-
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
91-
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
90+
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
91+
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
9292
</compilers>
9393
</system.codedom>
9494
<entityFramework>
95-
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
95+
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
9696
<providers>
97-
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
97+
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
9898
</providers>
9999
</entityFramework>
100100
</configuration>

0 commit comments

Comments
 (0)