Skip to content

Commit

Permalink
Aotufac的类型注册添加.AsSelf注册实现类自身,优化OAuth的Scope的Claim实现
Browse files Browse the repository at this point in the history
  • Loading branch information
gmf520 committed Nov 9, 2015
1 parent 3e27b5c commit 3a70c8e
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 114 deletions.
2 changes: 1 addition & 1 deletion samples/OSharp.Demo.Core/Identity/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public UserManager(IUserStore<User, int> store, IDataProtectionProvider dataProt
PasswordValidator = new PasswordValidator()
{
RequiredLength = 6,
RequireNonLetterOrDigit = true,
RequireNonLetterOrDigit = false,
RequireDigit = true,
RequireLowercase = true
};
Expand Down
26 changes: 17 additions & 9 deletions samples/OSharp.Demo.Web/Controllers/TestsController.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web.Mvc;

using OSharp.Core;
using Microsoft.AspNet.Identity;

using OSharp.Core.Caching;
using OSharp.Core.Context;
using OSharp.Core.Data;
using OSharp.Data.Entity;
using OSharp.Core.Dependency;
using OSharp.Core.Extensions;
using OSharp.Core.Logging;
using OSharp.Core.Security;
using OSharp.Core.Security.Models;
using OSharp.Demo.Contracts;
Expand Down Expand Up @@ -80,31 +78,41 @@ public ActionResult TestIoc()
format.FormatWith("DefaultDbContext", ServiceProvider.GetService<DefaultDbContext>().GetHashCode()),
format.FormatWith("DefaultDbContext", ServiceProvider.GetService<DefaultDbContext>().GetHashCode()),
format.FormatWith("IRepository<User,int>", ServiceProvider.GetService<IRepository<User,int>>().GetHashCode()),
format.FormatWith("IRepository<User,int>", ServiceProvider.GetService<IRepository<User,int>>().GetHashCode())
format.FormatWith("IRepository<User,int>", ServiceProvider.GetService<IRepository<User,int>>().GetHashCode()),
format.FormatWith("UserManager", ServiceProvider.GetService<UserManager<User,int>>().GetHashCode()),
format.FormatWith("UserManager", ServiceProvider.GetService<UserManager>().GetHashCode()),
};
return Content(lines.ExpandAndToString("<br>"));
}

public async Task<ActionResult> Test1()
{
ClientStore clientStore = ServiceProvider.GetService<ClientStore>();
OperationResult result = null;
//ClientInputDto clientDto = new ClientInputDto()
//{
// Name = "测试客户端01",
// ClientType = ClientType.Application,
// Url = "http://localhost:10240",
// LogoUrl= "http://localhost:10240",
// LogoUrl = "http://localhost:10240",
// RedirectUrl = "http://localhost:10240"
//};
//OperationResult result = clientStore.AddClient(clientDto).Result;
//result = await clientStore.AddClient(clientDto);
ClientSecretInputDto secretDto = new ClientSecretInputDto()
{
Type = "Test Type",
Remark = "Remark",
ClientId = 1
ClientId = 2
};
OperationResult result = await clientStore.AddClientSecret(secretDto);
result = await clientStore.AddClientSecret(secretDto);
return Content(result.Message);
}

public async Task<ActionResult> Test2()
{
UserManager manager = ServiceProvider.GetService<UserManager>();
IdentityResult result = await manager.AddPasswordAsync(1, "gmf31529019");
return Content(result.ToJsonString());
}
}
}
9 changes: 5 additions & 4 deletions samples/OSharp.Demo.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public void Configuration(IAppBuilder app)
services.AddOAuthServices();
services.AddDemoServices(app);

app.UseOsharpMvc(new MvcAutofacIocBuilder(services));
IIocBuilder apiAutofacIocBuilder = new WebApiAutofacIocBuilder(services);
app.UseOsharpWebApi(apiAutofacIocBuilder);
IIocBuilder mvcIocBuilder = new MvcAutofacIocBuilder(services);
app.UseOsharpMvc(mvcIocBuilder);
IIocBuilder apiIocBuilder = new WebApiAutofacIocBuilder(services);
app.UseOsharpWebApi(apiIocBuilder);
//app.UseOsharpSignalR(new SignalRAutofacIocBuilder(services));

app.ConfigureOAuth(apiAutofacIocBuilder.ServiceProvider);
app.ConfigureOAuth(apiIocBuilder.ServiceProvider);
app.ConfigureWebApi();
//app.ConfigureSignalR();
}
Expand Down
2 changes: 1 addition & 1 deletion samples/OSharp.Demo.Web/osharp.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<osharp xmlns="http://file.osharp.org/schemas/osharp.xsd">
<data>
<contexts>
<context name="default" enabled="true" dataLoggingEnabled="true" connectionStringName="default" type="OSharp.Data.Entity.DefaultDbContext, OSharp.Data.Entity">
<context name="default" enabled="true" dataLoggingEnabled="false" connectionStringName="default" type="OSharp.Data.Entity.DefaultDbContext, OSharp.Data.Entity">
<initializer type="OSharp.Data.Entity.DefaultDbContextInitializer, OSharp.Data.Entity" mapperFiles="OSharp.Demo.Core">
<createInitializer type="OSharp.Demo.Data.CreateDatabaseIfNotExistsWithSeed,OSharp.Demo.Core"/>
</initializer>
Expand Down
3 changes: 3 additions & 0 deletions src/OSharp.App.Local/Initialize/AutofacRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
}
builder.RegisterGeneric(descriptor.ImplementationType)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand All @@ -65,6 +66,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
}
builder.RegisterType(descriptor.ImplementationType)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand All @@ -85,6 +87,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
{
builder.RegisterInstance(descriptor.ImplementationInstance)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand Down
3 changes: 3 additions & 0 deletions src/OSharp.Autofac.Mvc/AutofacRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
}
builder.RegisterGeneric(descriptor.ImplementationType)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand All @@ -65,6 +66,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
}
builder.RegisterType(descriptor.ImplementationType)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand All @@ -85,6 +87,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
{
builder.RegisterInstance(descriptor.ImplementationInstance)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand Down
1 change: 0 additions & 1 deletion src/OSharp.Autofac.Mvc/OSharp.Autofac.Mvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="OSharp.Keys.snk" />
<None Include="OSharp.Autofac.Mvc.nuspec" />
<None Include="packages.config" />
Expand Down
15 changes: 0 additions & 15 deletions src/OSharp.Autofac.Mvc/app.config

This file was deleted.

3 changes: 3 additions & 0 deletions src/OSharp.Autofac.SignalR/AutofacRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
}
builder.RegisterGeneric(descriptor.ImplementationType)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand All @@ -65,6 +66,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
}
builder.RegisterType(descriptor.ImplementationType)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand All @@ -85,6 +87,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
{
builder.RegisterInstance(descriptor.ImplementationInstance)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand Down
1 change: 0 additions & 1 deletion src/OSharp.Autofac.SignalR/OSharp.Autofac.SignalR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
<Compile Include="SignalRAutofacIocBuilder.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="OSharp.Autofac.SignalR.nuspec" />
<None Include="OSharp.Keys.snk" />
<None Include="packages.config" />
Expand Down
23 changes: 0 additions & 23 deletions src/OSharp.Autofac.SignalR/app.config

This file was deleted.

3 changes: 3 additions & 0 deletions src/OSharp.Autofac.WebApi/AutofacRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
}
builder.RegisterGeneric(descriptor.ImplementationType)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand All @@ -65,6 +66,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
}
builder.RegisterType(descriptor.ImplementationType)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand All @@ -85,6 +87,7 @@ private static void RegisterInternal(ContainerBuilder builder, IEnumerable<Servi
{
builder.RegisterInstance(descriptor.ImplementationInstance)
.As(descriptor.ServiceType)
.AsSelf()
.PropertiesAutowired()
.ConfigureLifetimeStyle(descriptor.Lifetime);
}
Expand Down
1 change: 0 additions & 1 deletion src/OSharp.Autofac.WebApi/OSharp.Autofac.WebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
<Compile Include="WebApiAutofacIocBuilder.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="OSharp.Autofac.WebApi.nuspec" />
<None Include="OSharp.Keys.snk" />
<None Include="packages.config" />
Expand Down
31 changes: 0 additions & 31 deletions src/OSharp.Autofac.WebApi/app.config

This file was deleted.

26 changes: 7 additions & 19 deletions src/OSharp.Core/Dependency/IocBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// </copyright>
// <site>http://www.osharp.org</site>
// <last-editor>郭明锋</last-editor>
// <last-date>2015-10-12 15:33</last-date>
// <last-date>2015-11-09 3:05</last-date>
// -----------------------------------------------------------------------

using System;
Expand All @@ -22,8 +22,7 @@ public abstract class IocBuilderBase : IIocBuilder
{
private readonly IServiceCollection _services;
private bool _isBuilded;
private IServiceProvider _provider;


/// <summary>
/// 初始化一个<see cref="IocBuilderBase"/>类型的新实例
/// </summary>
Expand All @@ -32,6 +31,7 @@ protected IocBuilderBase(IServiceCollection services)
{
AssemblyFinder = new DirectoryAssemblyFinder();
_services = services.Clone();
_isBuilded = false;
}

/// <summary>
Expand All @@ -42,19 +42,7 @@ protected IocBuilderBase(IServiceCollection services)
/// <summary>
/// 获取 服务提供者
/// </summary>
public IServiceProvider ServiceProvider
{
get
{
if (_isBuilded)
{
return _provider;
}
_provider = Build();
_isBuilded = true;
return _provider;
}
}
public IServiceProvider ServiceProvider { get; private set; }

/// <summary>
/// 开始构建依赖注入映射
Expand All @@ -64,17 +52,17 @@ public IServiceProvider Build()
{
if (_isBuilded)
{
return _provider;
return ServiceProvider;
}

//设置各个框架的DependencyResolver
Assembly[] assemblies = AssemblyFinder.FindAll();

AddCustomTypes(_services);

_provider = BuildAndSetResolver(_services, assemblies);
ServiceProvider = BuildAndSetResolver(_services, assemblies);
_isBuilded = true;
return _provider;
return ServiceProvider;
}

/// <summary>
Expand Down
Loading

0 comments on commit 3a70c8e

Please sign in to comment.