Code Review: SharpArchitecture.MultiTenant
Join the DZone community and get the full member experience.Join For Free
it has been suggested that i’ll look at a more modern implementation of sharparchitecture, and i was directed toward the multitenant project.
the first thing to notice is the number of projects. it is actually hard to keep the number of projects down, as i well know, but this has several strange choices.
i am not really sure what is the point in separating the controllers into a separate assembly, or why we have a separate project for the applicationservices.
i am not the only one thinking so, i think:
then there is the core project:
personally, i wouldn’t create a project for just two files, but i can live with that. i don’t like attributes like domainsignature. it is hard for me to really say what, except that i think that they encourage a way of thinking that puts the model in the center of all things. i am usually much more interested in what something is doing than how it is shaped.
the data project is mostly concerned with setting up nhibernate via fluent nhibernate.
next up is the framework project. and there we run into the following marker interfaces. i really don’t like marker interfaces, and having those here doesn’t seem to be adding anything important to the application.
it seems that there is a lot going on simply to try to get a fallback to a non tenant situation, but the problem here is that it is usually much better to be explicit about those sort of things. you have the centralsession, and you have the tenantsession, and you are working with each in a different manner. it makes the infrastructure easier to manage and usually result in code that is clearer to follow.
so far, it has all been pretty much uninteresting, i would strongly encourage merging the solution into just two projects, the web & the tests projects, but that is about it.
now we move into the fancy bits, the controllers project. and there we find the following piece of code:
public class tenantlistquery : nhibernatequery, itenantlistquery
public ipagination<tenantviewmodel> getpagedlist(int pageindex, int pagesize)
var query = session.queryover<tenant>()
.orderby(customer => customer.name).asc;
var countquery = query.torowcountquery();
var totalcount = countquery.futurevalue<int>();
var firstresult = (pageindex - 1) * pagesize;
tenantviewmodel viewmodel = null;
var viewmodels = query.selectlist(list => list
.select(mission => mission.id).withalias(() => viewmodel.id)
.select(mission => mission.name).withalias(() => viewmodel.name)
.select(mission => mission.domain).withalias(() => viewmodel.domain)
.select(mission => mission.connectionstring).withalias(() => viewmodel.connectionstring))
return new custompagination<tenantviewmodel>(viewmodels, pageindex, pagesize, totalcount.value);
i quite like this code. it is explicit about what it is doing. there is a good reason to hide this sort of thing behind a class, because while it is easy to read, it is also a lot of detailed code that should be abstracted. i like the use of futures to reduce the number of queries, and that we have explicit paging here. i also like the projection directly into the view model.
what i don’t like is that i really don’t understand how the session instance is being selected. oh, i understand how multitenantsessionfactorykeyprovider is working, and that we get the central database because we aren’t using a tenanted entity, but it still seems too much magic here i would rather a centralsession instead.
another thin that i liked was the code structure:
all of the code is grouped by feature in a very nice fashion.
my main peeve with the application is that this is basically it. we are talking about an application that is basically two crud pages, nothing more. yes, it is a sample app to show something very specific, but i would have liked to see some more meat there to look at.
multi tenancy is a hard problem, and this application spend quite a bit of time doing what is essentially connection string management.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.