Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support auto resolving with VContainer for ISubscriber,IPublisher,etc #120

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,8 @@ You need to install Core library and choose [VContainer](https://github.com/hada

Andalso, requires [UniTask](https://github.com/Cysharp/UniTask) to install, all `ValueTask` declaration in .NET is replaced to `UniTask`.

Unity version does not have open generics support(for IL2CPP) and does not support auto registration. Therefore, all required types need to be manually registered.
> [!NOTE]
> Unity version does not have open generics support(for IL2CPP) and does not support auto registration. Therefore, all required types need to be manually registered.

VContainer's installation sample.

Expand Down Expand Up @@ -1233,6 +1234,15 @@ public class MessagePipeDemo : VContainer.Unity.IStartable
}
```

> [!TIP]
> If you are using Unity 2022.1 or later and VContainer 1.14.0 or later, you do not need `RegsiterMessageBroker<>`.
> A set of types including `ISubscriber<>`, `IPublisher<>` or its asynchronous version will be resolved automatically.
> Note that `IRequesthandler<>` and `IRequestAllHanlder<>` still require manual registration.


Unity version does not have open generics support(for IL2CPP) and does not support auto registration. Therefore, all required types need to be manually registered.


Zenject's installation sample.

```csharp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public void Add(Type serviceType, Type implementationType, Lifetime lifetime)
builder.Register(implementationType, lifetime).As(serviceType);
}

public void Add(Type serviceType, InstanceLifetime lifetime)
{
var l = (lifetime == InstanceLifetime.Singleton) ? Lifetime.Singleton
: (lifetime == InstanceLifetime.Scoped) ? Lifetime.Scoped
: Lifetime.Transient;

builder.Register(serviceType, l);
}

public void Add(Type serviceType, Type implementationType, InstanceLifetime lifetime)
{
var l = (lifetime == InstanceLifetime.Singleton) ? Lifetime.Singleton
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "MessagePipe",
"rootNamespace": "MessagePipe",
"references": [
"UniTask",
"UniTask.Linq"
Expand All @@ -12,6 +11,12 @@
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"versionDefines": [
{
"name": "jp.hadashikick.vcontainer",
"expression": "1.14.0",
"define": "MESSAGEPIPE_OPENGENERICS_SUPPORT"
}
],
"noEngineReferences": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static IServiceCollection AddMessagePipe(this IServiceCollection services
services.TryAddTransient(item); // filter itself is Transient
}

#if !UNITY_2018_3_OR_NEWER
#if !UNITY_2018_3_OR_NEWER || (MESSAGEPIPE_OPENGENERICS_SUPPORT && UNITY_2022_1_OR_NEWER)
// open generics implemntations(.NET Only)

{
Expand Down Expand Up @@ -107,14 +107,16 @@ public static IServiceCollection AddMessagePipe(this IServiceCollection services

var lifetime2 = options.RequestHandlerLifetime; // requesthandler lifetime

#endif
#if !UNITY_2018_3_OR_NEWER
// RequestHandler
services.Add(typeof(IRequestHandler<,>), typeof(RequestHandler<,>), lifetime2);
services.Add(typeof(IAsyncRequestHandler<,>), typeof(AsyncRequestHandler<,>), lifetime2);

// RequestAll
services.Add(typeof(IRequestAllHandler<,>), typeof(RequestAllHandler<,>), lifetime2);
services.Add(typeof(IAsyncRequestAllHandler<,>), typeof(AsyncRequestAllHandler<,>), lifetime2);

// auto registration is .NET only.
if (options.EnableAutoRegistration)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ public void AddSingleton(Type serviceType, Type implementationType)
}


public void Add(Type serviceType, InstanceLifetime lifetime)
{
Add(serviceType, serviceType, lifetime);
}

public void Add(Type serviceType, Type implementationType, InstanceLifetime lifetime)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static object GetRequiredService(this IServiceProvider provider, Type typ

public interface IServiceCollection
{
void Add(Type serviceType, InstanceLifetime lifetime);
void Add(Type serviceType, Type implementationType, InstanceLifetime lifetime);
void AddSingleton<T>(T instance);
void AddSingleton(Type type);
Expand Down
7 changes: 7 additions & 0 deletions src/MessagePipe.Unity/Assets/Tests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

public static class TestHelper
{
public static IObjectResolver BuildVContainer()
{
var builder = new ContainerBuilder();
builder.RegisterMessagePipe();
return builder.Build();
}

public static IObjectResolver BuildVContainer(Action<MessagePipeOptions> configure, Action<MessagePipeOptions, ContainerBuilder> use)
{
var builder = new ContainerBuilder();
Expand Down
24 changes: 16 additions & 8 deletions src/MessagePipe.Unity/Assets/Tests/VContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ public class VContainerTest
public void SimpelePush()
{
var resolver = TestHelper.BuildVContainer((options, builder) =>
{
builder.RegisterMessageBroker<int>(options);
});
{
#if !UNITY_2022_1_OR_NEWER
builder.RegisterMessageBroker<int>(options);
#endif
});

var pub = resolver.Resolve<IPublisher<int>>();
var sub1 = resolver.Resolve<ISubscriber<int>>();
Expand All @@ -41,9 +43,11 @@ public void SimpelePush()
public IEnumerator SimpleAsyncPush() => UniTask.ToCoroutine(async () =>
{
var resolver = TestHelper.BuildVContainer((options, builder) =>
{
builder.RegisterMessageBroker<int>(options);
});
{
#if !UNITY_2022_1_OR_NEWER
builder.RegisterMessageBroker<int>(options);
#endif
});

var pub = resolver.Resolve<IAsyncPublisher<int>>();
var sub1 = resolver.Resolve<IAsyncSubscriber<int>>();
Expand Down Expand Up @@ -74,8 +78,10 @@ public void WithFilter()
options.AddGlobalMessageHandlerFilter<MyFilter<int>>(1200);
}, (options, builder) =>
{
#if !UNITY_2022_1_OR_NEWER
builder.RegisterMessageBroker<int>(options);

#endif

builder.RegisterMessageHandlerFilter<MyFilter<int>>();
builder.RegisterInstance(store);
});
Expand Down Expand Up @@ -162,8 +168,10 @@ public void Buffered()
{
var provider = TestHelper.BuildVContainer((options, builder) =>
{
#if !UNITY_2022_1_OR_NEWER
builder.RegisterMessageBroker<IntClass>(options);
});
#endif
});

var p = provider.Resolve<IBufferedPublisher<IntClass>>();
var s = provider.Resolve<IBufferedSubscriber<IntClass>>();
Expand Down
2 changes: 1 addition & 1 deletion src/MessagePipe.Unity/Packages/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"com.unity.test-framework": "1.1.24",
"com.unity.toolchain.win-x86_64-linux-x86_64": "0.1.20-preview",
"com.unity.ugui": "1.0.0",
"jp.hadashikick.vcontainer": "https://github.com/hadashiA/VContainer.git?path=VContainer/Assets/VContainer"
"jp.hadashikick.vcontainer": "https://github.com/hadashiA/VContainer.git?path=VContainer/Assets/VContainer#1.15.2"
}
}
15 changes: 3 additions & 12 deletions src/MessagePipe.Unity/Packages/packages-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.nuget.mono-cecil": {
"version": "1.10.1",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.sysroot": {
"version": "0.1.19-preview",
"depth": 1,
Expand Down Expand Up @@ -93,13 +86,11 @@
}
},
"jp.hadashikick.vcontainer": {
"version": "https://github.com/hadashiA/VContainer.git?path=VContainer/Assets/VContainer",
"version": "https://github.com/hadashiA/VContainer.git?path=VContainer/Assets/VContainer#1.15.2",
"depth": 0,
"source": "git",
"dependencies": {
"com.unity.nuget.mono-cecil": "1.10.1"
},
"hash": "04c22dc81b4c8b0ccfc133050f23a08b9fc0934e"
"dependencies": {},
"hash": "634d03aa8349e5698be39d487e2ef0f9d5c881d9"
},
"com.unity.modules.imgui": {
"version": "1.0.0",
Expand Down
6 changes: 4 additions & 2 deletions src/MessagePipe/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static IServiceCollection AddMessagePipe(this IServiceCollection services
services.TryAddTransient(item); // filter itself is Transient
}

#if !UNITY_2018_3_OR_NEWER
#if !UNITY_2018_3_OR_NEWER || (MESSAGEPIPE_OPENGENERICS_SUPPORT && UNITY_2022_1_OR_NEWER)
// open generics implemntations(.NET Only)

{
Expand Down Expand Up @@ -107,14 +107,16 @@ public static IServiceCollection AddMessagePipe(this IServiceCollection services

var lifetime2 = options.RequestHandlerLifetime; // requesthandler lifetime

#endif
#if !UNITY_2018_3_OR_NEWER
// RequestHandler
services.Add(typeof(IRequestHandler<,>), typeof(RequestHandler<,>), lifetime2);
services.Add(typeof(IAsyncRequestHandler<,>), typeof(AsyncRequestHandler<,>), lifetime2);

// RequestAll
services.Add(typeof(IRequestAllHandler<,>), typeof(RequestAllHandler<,>), lifetime2);
services.Add(typeof(IAsyncRequestAllHandler<,>), typeof(AsyncRequestAllHandler<,>), lifetime2);

// auto registration is .NET only.
if (options.EnableAutoRegistration)
{
Expand Down
Loading