Giter VIP home page Giter VIP logo

Comments (6)

alexmg avatar alexmg commented on July 22, 2024 1

Yes, in the case of a stateless service its lifetime is essentially a singleton. The primary reason for supporting these in the integration is to provide a consistent pattern for working with DI in all types of SF services. You can also use the more natural IDisposable pattern and have the container dispose the appropriate services for you when the service is deactivated.

If you want to have dependencies that should exist only for the duration of a method invocation you can achieve this using an auto-generated factory (Func<T>). In the case that you need to control disposal of this dependency you can also combine that with an owned dependency (Func<Owned<T>>) and dispose the instance of T immediately after you have finished with it in the method. You can find out more about these relationship types on our documentation site.

http://docs.autofac.org/en/latest/resolve/relationships.html

In the example you provided a basic auto-generated factory approach would look something like this:

public class UserService : IUserService
{
	private readonly Func<IUserStore> _userStoreFactory;

	public UserService(Func<IUserStore> userStoreFactory)
	{
		_userStoreFactory = userStoreFactory;
	}

	public async Task<string> GetNameAsync(int id)
	{
		return await _userStoreFactory().GetNameAsync(id);
	}
}

If the IUserStore implemented IDisposable and needed to release resources eagerly it would look more like this:

public class UserService : IUserService
{
	private readonly Func<Owned<IUserStore>> _userStoreFactory;

	public UserService(Func<Owned<IUserStore>> userStoreFactory)
	{
		_userStoreFactory = userStoreFactory;
	}

	public async Task<string> GetNameAsync(int id)
	{
		using (var userStore = _userStoreFactory().Value)
		{
			return await userStore.GetNameAsync(id);
		}		
	}
}

I would also configure and build your container outside of the factory delegate so you don’t accidentally apply the same pattern when working with other types of services (such as reliable actors) and end up creating multiple instances of the container. When you use the integration all your services (including the SF services) are registered up front before building the container so this wouldn't be an issue.

Hope that helps. πŸ˜ƒ

from autofac.servicefabric.

alexmg avatar alexmg commented on July 22, 2024 1

Glad it is working well for you. Yes, the store and its dependencies will be created in their own lifetime scope that will be disposed at the end of the using, essentially making it instance per method call. πŸ‘

from autofac.servicefabric.

tillig avatar tillig commented on July 22, 2024 1

I added some quick docs around the Service Fabric integration which includes this faux per-request info so hopefully it'll help folks in the future.

from autofac.servicefabric.

Mardoxx avatar Mardoxx commented on July 22, 2024

For information: microsoft/service-fabric-issues#269 (comment)

from autofac.servicefabric.

Mardoxx avatar Mardoxx commented on July 22, 2024

Absolutely perfect, thanks so much! This appears to be exactly what I was looking for. I had no idea AutoFac was this powerful. What a great piece of kit! I shall have a play with this in the coming days πŸ˜„

from autofac.servicefabric.

Mardoxx avatar Mardoxx commented on July 22, 2024

I've now implemented this and it works brilliantly.

I opted for

public class UserService : IUserService
{
	private readonly Func<Owned<IUserStore>> _userStoreFactory;

	public UserService(Func<Owned<IUserStore>> userStoreFactory)
	{
		_userStoreFactory = userStoreFactory;
	}

	public async Task<string> GetNameAsync(int id)
	{
		using (var userStore = _userStoreFactory())
		{
			return await userStore.Value.GetNameAsync(id);
		}		
	}
}

This way, correct me if I'm wrong, a new scope is created per method call. So any services which are registered as per scope (or transient) will be constructed and then destroyed at the end of this call - In this case the IUserDbContext. Seems to work as I wanted anyway, unless this is not a sensible thing to do?

from autofac.servicefabric.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.