-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(caller): Extends ConfigHttpRequestMessage to support the same…
… lifecycle as CallerBase (#203) * feat(Caller): CallerBase supports the current ServiceProvider life cycle * test(Caller): Perfect unit tests * Refactor: Optimize Factory * style: Format code * fix(Caller): Support empty services in CallerBase constructor * fix(Caller): fix Code Smells * test(Caller): Perfect unit tests * style: format code * test(Caller): Perfect unit tests * doc(Caller): Modify the Readme * chore: remove the blank line
- Loading branch information
1 parent
8fe2d38
commit 657b3a4
Showing
29 changed files
with
365 additions
and
167 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/AbstractMasaFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the MIT License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.BuildingBlocks.Data; | ||
|
||
public abstract class AbstractMasaFactory<TService, TRelationOptions> : IMasaFactory<TService> | ||
where TService : class | ||
where TRelationOptions : MasaRelationOptions<TService> | ||
{ | ||
protected abstract string DefaultServiceNotFoundMessage { get; } | ||
protected abstract string SpecifyServiceNotFoundMessage { get; } | ||
protected abstract MasaFactoryOptions<TRelationOptions> FactoryOptions { get; } | ||
|
||
protected readonly IServiceProvider ServiceProvider; | ||
|
||
protected AbstractMasaFactory(IServiceProvider serviceProvider) | ||
{ | ||
ServiceProvider = serviceProvider; | ||
} | ||
|
||
private static MasaRelationOptions<TService>? GetDefaultOptions(List<TRelationOptions> optionsList) | ||
{ | ||
return optionsList.SingleOrDefault(c => c.Name == Options.DefaultName) ?? | ||
optionsList.FirstOrDefault(); | ||
} | ||
|
||
public virtual TService Create() | ||
{ | ||
var defaultOptions = GetDefaultOptions(FactoryOptions.Options); | ||
if (defaultOptions == null) | ||
throw new NotImplementedException(DefaultServiceNotFoundMessage); | ||
|
||
return defaultOptions.Func.Invoke(ServiceProvider); | ||
} | ||
|
||
public virtual TService Create(string name) | ||
{ | ||
var options = FactoryOptions.Options.SingleOrDefault(c => c.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); | ||
if (options == null) | ||
throw new NotImplementedException(string.Format(SpecifyServiceNotFoundMessage, name)); | ||
|
||
return options.Func.Invoke(ServiceProvider); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/BuildingBlocks/Data/Masa.BuildingBlocks.Data/IMasaFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the MIT License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.BuildingBlocks.Data; | ||
|
||
public interface IMasaFactory<out TService> where TService : class | ||
{ | ||
TService Create(); | ||
|
||
TService Create(string name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.