Consider Umbraco for new project. Best way to integrate existing database?
Hi,
This is a general Umbraco architectural question, not a specific-to-the-line-of-code-question.
I am new to Umbraco but know my way around .net and asp.net mvc well.
For an upcoming project I am considering using Umbraco as a cms but as my experience with Umbraco is little, I cannot quite oversee what my options are when it comes to integrating an already existing mssql database.
The database that will support the Umbraco installation already has some tables present, containing product information. What is the best way to expose this data using the Umbraco api/models and subsequently integrate this data in Umbraco views?
E.g. can I create data models in the Umbraco admin and map those to the existing tables or is there another way that is considered to be a best practice?
With integrating this kind of setup with Umbraco there are lots of small variations in what you are trying to achieve that would influence the best approach (Ironically the Umbraco Application Integration training course is aimed at trying to cover all your available options! -
https://umbraco.com/training/course-details/application-integration-details/)
One option is to import your product data into Umbraco as Umbraco Document Types - this has the advantage of enabling editors to update the product descriptions - but you may have 'stock' and other values stored against each product that wouldn't work well in a cms (each stock change would create a content version in the cms)
Another option would be to keep the products in their existing database tables, but expose them through the Umbraco CMS, using a custom route.
You'd map a custom route in Umbraco:
RouteTable.Routes.MapUmbracoRoute(
"Product details",
"Product/details/{id}",
new
{
controller = "Product",
action = "Details",
id = UrlParameter.Optional
}, new ProductSectionVirtualNodeRouteHandler());
Now all request for products on the above route, would be handled outside of Umbraco usual routing, the 'ProductSectionVirtualNodeRouteHandler() class would be a custom class associated with the route which inherits 'UmbracoVirtualNodeRouteHandler class and whose job is to associate either an existing Umbraco IPublishedContent node with your request or create a new IPublishedContent item - this enables you to return existing Umbraco template views from your Product Controller, but display data from your products table 'as if it were in' Umbraco.
eg
public class ProductSectionVirtualNodeRouteHandler : UmbracoVirtualNodeRouteHandler
{
protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)
{
var productSectionId = 123;
var umbracoHelper = new UmbracoHelper(umbracoContext);
return umbracoHelper.TypedContent(productSectionId);
}
}
or you could create some kind of service layer that accesses the custom product database tables outside of Umbraco, and use SurfaceControllers child actions, to call these services and return partial views within the Umbraco templates to display your product details... but your urls won't be as nice as the custom route technique...
eg
public class HomePageController : SurfaceController
{
[ChildActionOnly]
public ActionResult FeaturedProducts(int count = 3)
{
var featuredProducts = ProductService.GetFeatured(count);
var items = Mapper.Map<IEnumerable<ProductPreviewViewModel>>(featuredProducts);
return PartialView("_FeaturedProducts", items);
}
}
Consider Umbraco for new project. Best way to integrate existing database?
Hi,
This is a general Umbraco architectural question, not a specific-to-the-line-of-code-question.
I am new to Umbraco but know my way around .net and asp.net mvc well.
For an upcoming project I am considering using Umbraco as a cms but as my experience with Umbraco is little, I cannot quite oversee what my options are when it comes to integrating an already existing mssql database.
The database that will support the Umbraco installation already has some tables present, containing product information. What is the best way to expose this data using the Umbraco api/models and subsequently integrate this data in Umbraco views?
E.g. can I create data models in the Umbraco admin and map those to the existing tables or is there another way that is considered to be a best practice?
Thank you.
Hi fluxmatix
With integrating this kind of setup with Umbraco there are lots of small variations in what you are trying to achieve that would influence the best approach (Ironically the Umbraco Application Integration training course is aimed at trying to cover all your available options! - https://umbraco.com/training/course-details/application-integration-details/)
One option is to import your product data into Umbraco as Umbraco Document Types - this has the advantage of enabling editors to update the product descriptions - but you may have 'stock' and other values stored against each product that wouldn't work well in a cms (each stock change would create a content version in the cms)
Another option would be to keep the products in their existing database tables, but expose them through the Umbraco CMS, using a custom route.
You'd map a custom route in Umbraco:
Now all request for products on the above route, would be handled outside of Umbraco usual routing, the 'ProductSectionVirtualNodeRouteHandler() class would be a custom class associated with the route which inherits 'UmbracoVirtualNodeRouteHandler class and whose job is to associate either an existing Umbraco IPublishedContent node with your request or create a new IPublishedContent item - this enables you to return existing Umbraco template views from your Product Controller, but display data from your products table 'as if it were in' Umbraco.
eg
more information here: https://our.umbraco.org/documentation/reference/routing/custom-routes
and
https://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/
or you could create some kind of service layer that accesses the custom product database tables outside of Umbraco, and use SurfaceControllers child actions, to call these services and return partial views within the Umbraco templates to display your product details... but your urls won't be as nice as the custom route technique...
eg
}
and your homepage template would have
or maybe you use a combination of these techniques... it depends alot on what you are trying to achieve, hope this helps ?
regards
Marc
Hi Marc,
Thank you very much. Very helpful!
is working on a reply...