mirror of https://github.com/Squidex/squidex.git
5 changed files with 62 additions and 57 deletions
@ -1,33 +0,0 @@ |
|||||
// ==========================================================================
|
|
||||
// AutofacDomainObjectFactory.cs
|
|
||||
// Squidex Headless CMS
|
|
||||
// ==========================================================================
|
|
||||
// Copyright (c) Squidex Group
|
|
||||
// All rights reserved.
|
|
||||
// ==========================================================================
|
|
||||
|
|
||||
using System; |
|
||||
using Autofac; |
|
||||
using Squidex.Infrastructure.CQRS.Commands; |
|
||||
|
|
||||
namespace Squidex.Infrastructure.CQRS.Autofac |
|
||||
{ |
|
||||
public sealed class AutofacDomainObjectFactory : IDomainObjectFactory |
|
||||
{ |
|
||||
private readonly ILifetimeScope lifetimeScope; |
|
||||
|
|
||||
public AutofacDomainObjectFactory(ILifetimeScope lifetimeScope) |
|
||||
{ |
|
||||
Guard.NotNull(lifetimeScope, nameof(lifetimeScope)); |
|
||||
|
|
||||
this.lifetimeScope = lifetimeScope; |
|
||||
} |
|
||||
|
|
||||
public IAggregate CreateNew(Type type, Guid id) |
|
||||
{ |
|
||||
return (IAggregate)lifetimeScope.Resolve(type, |
|
||||
new NamedParameter("id", id), |
|
||||
new NamedParameter("version", 0)); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,34 @@ |
|||||
|
// ==========================================================================
|
||||
|
// DefaultDomainObjectFactory.cs
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex Group
|
||||
|
// All rights reserved.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System; |
||||
|
|
||||
|
namespace Squidex.Infrastructure.CQRS.Commands |
||||
|
{ |
||||
|
public delegate T DomainObjectFactoryFunction<out T>(Guid id) where T : IAggregate; |
||||
|
|
||||
|
public class DefaultDomainObjectFactory : IDomainObjectFactory |
||||
|
{ |
||||
|
private readonly IServiceProvider serviceProvider; |
||||
|
|
||||
|
public DefaultDomainObjectFactory(IServiceProvider serviceProvider) |
||||
|
{ |
||||
|
Guard.NotNull(serviceProvider, nameof(serviceProvider)); |
||||
|
|
||||
|
this.serviceProvider = serviceProvider; |
||||
|
} |
||||
|
|
||||
|
public IAggregate CreateNew(Type type, Guid id) |
||||
|
{ |
||||
|
var factoryFunctionType = typeof(DomainObjectFactoryFunction<>).MakeGenericType(type); |
||||
|
var factoryFunction = (Delegate)serviceProvider.GetService(factoryFunctionType); |
||||
|
|
||||
|
return (IAggregate)factoryFunction.DynamicInvoke(id); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue