In Onion Structure, can an infrastructure service call another infrastructure service? - onion-architecture

For example, there are two services in the infrastructure layer:
Contact, which is an Elasticesearch service.
PhoneNumber which is a SQL service.
The Contact service needs to get each contact's phone number and return to its caller, so it needs to call the PhoneNumber service.
I am wondering if Contact can call PhoneNumber directly? They are both in the infrastructure layer.
Or, should I let Contact return to the service in the domain layer first, then let the domain layer service call PhoneNumber?
Thanks!

Short answer is that in infrastructure, using a service in another service isn't right pattern, because all services in this layer should be third party services and there should be no relation between them in our infrastructure layer.
Application layer is responsible for fetching data from infrastructure services.
For instance, in this layer, below method could be implemented
List<string> GetPhoneNumbers(string contactName)
{
var phoneNumbers = new List<string>();
var contacts = _contactService.GetContacts(contactName);
foreach(var contact in contacts)
{
var phoneNumber = _phoneService.GetPhoneNumber(contact)
phoneNumbers.Add(phoneNumber);
}
return phoneNumbers;
}
_contactService along with _phoneService are the two services in infrastructure layer. The betters name for them would be _contactRepository and _phoneRepository if you use Repository pattern.

Related

Dynamic scoped dependency resolution in a console app for multiple tenants using dependency injection

In case of a Web API, each request is a distinct scope and dependencies registered as scoped will get resolved per request. So resolving dependencies per request per tenant is easy as the tenant information (like TenantId) can be passed in the HTTP Request headers like below:
services.TryAddScoped<ITenantContext>(x =>
{
var context = x.GetService<IHttpContextAccessor>().HttpContext;
var tenantId = context.Request.Headers["TenantId"].ToString();
var tenantContext = GetTenantContext(tenantId);
return tenantContext;
}
Other registrations first resolve TenantContext and use it to resolve other dependencies. For example, IDatabase will be registered as below. During resolution it will resolve and connect to specific tenant database.
services.TryAddScoped<IDatabase>(x =>
{
var tenantContext = x.GetService<ITenantContext>();
return new Database(tenantContext.DatabaseConnectionString);
}
This is all good in a Web API service because each request is a scope. I am facing challenges using dependency injection in a multi-tenant Console App. Suppose the app processes items from a
multi-tenant queue and each message can belong to a different tenant. While processing each message, it commits data to tenant specific database. So in this case the scope is each message in a queue and message contains the tenantId.
So when the app reads a message from queue, it needs to get TenantContext. Then resolve other dependencies based on this TenantContext.
One straightforward option I see how this dynamic resolution can be achieved is to create the dependent objects manually using the TenantContext but then I wouldn't be able to leverage dependency injection. All objects would get created manually and disposed after going out of scope after the message is processed.
var messgage = GetMessageFromQueue(queueName);
var tenantContext = GetTenantContext(message.TenantId);
var database = GetDatabaseObject(tenantContext);
// Do other processing now we got the database object connected to specific tenant DB
Is there an option in DI where I can pass in the TenantId dynamically so that TenantContext gets set for this scope and then all further resolution within this scope leverage this TenantContext?
Because the role of the tenancy goes beyond the implementation ("this uses X database") and is actually contextual to the action being performed ("this uses X database and must use this connection string based on the context being handled in the action"), there's some risk of assuming that ambient context is present in alternate implementations due to it not expressly being described in your interface in some way, which is where the DI issue is coming up here.
You might be able to:
Update your interfaces so that the tenancy information is an expected parameter of your methods. This ensures that regardless of future implementation, the presence of the tenant ID is explicit in their signature:
public interface ITenantDatabase {
public TResponse Get(string TenantId, int Id);
//... other methods ...
}
Add a factory wrapper around your existing interfaces to handle assigning the context at object creation and have that factory return the IDatabase instance. This is basically what you are proposing manually but with an abstraction around it that you could register and inject to keep the code that leverages it from being responsible for the logic:
public interface ITenantDatabaseFactory {
public IDatabase GetDatabaseForTenant(int TenantId);
}
// Add an implementation that manually generates and returns the scoped objects

Azure Service Bus - Use multiple senders in a web API

We are trying to implement Azure service bus for managing user "Work Queues"
Background:
We have a web UI pushing new items to a Web API which are persisted to a DB and then pushed to a Service Bus Queue. All messages have a property which denote who can work on the message. For providing a user with the ability to pick messages assigned to them, I am thinking about creating a topic with subscriptions that filter on that property.
Approach
To achieve the above mentioned capability:
I need to register a sender for the queue and a sender for the topic all within the same Web API. I have tried adding the two senders as Singletons but during DI, how do I pick which sender to use ?
services.TryAddSingleton(implementationFactory =>
{
var serviceBusConfiguration = implementationFactory.GetRequiredService<IMessagingServiceConfiguration>();
var serviceBusClient = new ServiceBusClient(serviceBusConfiguration.IntakeQueueSendConnectionString);
var serviceBusSender = serviceBusClient.CreateSender(serviceBusConfiguration.IntakeQueueName);
return serviceBusSender;
});
services.TryAddSingleton(implementationFactory =>
{
var serviceBusConfiguration = implementationFactory.GetRequiredService<IMessagingServiceConfiguration>();
var serviceBusClient = new ServiceBusClient(serviceBusConfiguration.TopicConnectionString);
var topicSender = serviceBusClient.CreateSender(serviceBusConfiguration.TopicName);
return topicSender;
});
I am using the above setup to add the services as singletons and individually I am able to send and receive messages from either the topic or the queue.
How can I register both the implementations and pick which one should be injected when I use DI in the constructor to consume it.
With respect to resolving DI registration for multiple instances of the same type, the first answer to this question illustrates using a service resolver with ASP.NET Core. To my knowledge, that is still the best approach.
For the senders, you could differentiate by checking their EntityPath property to identify whether they point to your queue or topic.

ASP.Net Identity in Azure Cloud

I have a problem with ASP.Net Identity used in application scaled to two instances. It seems that we have problem with checking password (checking hashes?) or verifying generated tokens (for example: password reset). When application works on one instance, then everything seems to be fine. It's weird because I read that using ASP.Net Identity in Azure cloud should be safe - it should use the same machine key on both instances.
Our user manager use token provider created below:
public DataProtectorTokenProvider<ExternalUser> Create(string purpose = "GeneralPurpose")
{
var provider = new DpapiDataProtectionProvider(_appName);
var result = new DataProtectorTokenProvider<ExternalUser>(provider.Create(purpose));
return result;
}
What is more, our user manager is singleton, but I think it shouldn't be a difference.
Any idea why it doesn't work on two instances in Azure? I will appreciate any help or advice.

ASP.NET MVC Multi Tenant with seperated databases using Autofac and Owin

Situation
We have one ASP.NET MVC 5 application running along with SQL Server. We have one master database which contains a table Tenants where all of our tenants are registrated with a connection string property to their own personal database.
For authentication we are using the Microsoft Owin library.
Autofac
We have setup autofac like this:
var builder = new ContainerBuilder();
// Register the controllers
builder.RegisterControllers(typeof(Project.Web.ProjectApplication).Assembly);
// ### Register all persistence objects
// Project main database registration ( Peta Poco instance using connectionstring as parameter )
builder.RegisterType<ProjectDatabase>()
.As<ProjectDatabase>()
.WithParameter(new NamedParameter("connectionString", GlobalSettings.ProjectTenantConnectionString))
.InstancePerLifetimeScope();
// Project tenant specific database registration
// ...
// Unit of work
builder.RegisterType<PetaPocoUnitOfWork>()
.As<IDatabaseUnitOfWork>()
.InstancePerRequest();
// ### Register all services
builder.RegisterAssemblyTypes(Assembly.Load("Project.Core"))
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
// ### Register all repositories
builder.RegisterType<RepositoryFactory>()
.As<IRepositoryFactory>()
.InstancePerLifetimeScope();
builder.RegisterAssemblyTypes(Assembly.Load("Project.Core"))
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
// Register Logging
builder.RegisterType<Logger>().As<ILogger>().InstancePerLifetimeScope();
// Register Automapper
builder.RegisterAssemblyTypes(Assembly.Load("Project.Core")).As<Profile>();
builder.RegisterAssemblyTypes(Assembly.Load("Project.Web")).As<Profile>();
builder.Register(context => new MapperConfiguration(cfg =>
{
foreach (var profile in context.Resolve<IEnumerable<Profile>>())
{
cfg.AddProfile(profile);
}
})).AsSelf().SingleInstance();
builder.Register(c => c.Resolve<MapperConfiguration>().CreateMapper(c.Resolve))
.As<AutoMapper.IMapper>()
.InstancePerLifetimeScope();
// Register Owin
builder.Register(ctx => HttpContext.Current.GetOwinContext()).As<IOwinContext>();
builder.Register(
c => new IdentityUserStore(c.Resolve<IUserService>()))
.AsImplementedInterfaces().InstancePerRequest();
builder.Register(
ctx => ctx.Resolve<IOwinContext>().Authentication)
.As<IAuthenticationManager>().InstancePerRequest();
builder.RegisterType<IdentityUserManager>().AsSelf().Inst‌​ancePerRequest();
// Build container
var container = builder.Build();
// Tenant container
var tenantIdentifier = new RequestSubdomainStrategy();
var mtc = new MultitenantContainer(tenantIdentifier, container);
// Set autofac as dependency resolver
DependencyResolver.SetResolver(new AutofacDependencyResolver(mtc));
More details
Using this setup we have an instance setup in Autofac to our master Tenant database.
This is then injected into our PetaPocoUnitOfWork for committing the transaction.
This works, and I can get the tenant information.
But now we need the following to work and we don't have a clue where to start.
How do we setup autofac to register tenants peta poco database instances to inject into the PetaPocoUnitOfWork and how will the app now how to resolve this? Because we need to have access to 2 databases ( the master and the personal tenants database ), first for getting the tenants connection string and then for doing crud operations on the tenants database.
What about our PetaPocoUnitOfWork, which contains the database to work with, should we register this also per tenant and pass the database using the resolving method of autofac and set this on a instance per request?
You can actually have a shard manager [More similar to that of the Microsoft Azure Shard Manager] that takes the connectionstring name and the tenant context. From these information, it can resolve the connection and then pass it on to the Context.
This will be resolved on a per-tenant basis and then the application works with the tenant based connection, i.e. this is injected in each of the services so that the identity established [logged in user identity] could be used to set the right connection object in the EF / Data Tier. This way, it facilitates loose coupled design and also easy to test and mockup.
You can find sample code and little documentation of how such an implementation would look like from my github repository
IMHO, the rationale behind this approach that I suggest would be the fact that the partitions per tenant would be stored in a database [typically your master database] and that would need to be fetched and used even if you are able to some-how inject these via Autofac. I did not reproduce the code here as it takes a bit long explanation to get the code and explanations here, which is being taken care in github.
HTH

does onDisconnect event on SignalR2 releases all of its resources upon disconnect?

I have a concurrent dictionary List on my controller that stores list of online users. For example when Client A and Client B connects there are 2 online clients present on the list, but when i disconnect B and then re- connect again it must still show 2 online clients but in my case, only Client B detected as online user(disconnected then reconnected). I am using IIS server 7.5.. Please help me with this, do i need to use a database rather than dictionary? I think it resets the dictionary to zero if one user disconnects and reconnects again.... :( Below is my hub class code
public class Chat : Hub
{
//add online client
private static ConcurrentDictionary<string, string> personLists
= new ConcurrentDictionary<string, string>();
public void Connect(string Username, int ID)
{
string id = Context.ConnectionId;
if (!personLists.ContainsKey(Username))
{
personLists.TryAdd(Username, id);
Clients.Caller.viewOnlinePersons(personLists.Where(p => p.Key != Username));
Clients.Others.enters(Username);
}
else
{
string notif = "user: "+Username+" is already used";
Clients.Caller.onUse(notif);
}
The concurrent dictionary should work just fine. It'd be good if you could post some code, but if you go this route with concurrent dictionary, make sure it's static (the Hub classes get created and destroyed per signal) and I think it'd be better suited placed on the hub itself and private (and of course static, again). You can also use Dependency Injection with SignalR which will be a lot cleaner.
You'll only need database as a backplane if you plan on running the application on multiple servers where of course a memory of a single server is not accessible by the other servers and a duplicate dictionary would be created for each server, so in that case you need to take the storage and move it up a bit in the architecture to be accessible by all the servers.

Resources