I've a little confusion over here, on this line of code
var cs = ApplicationContext.Current.Services.ContentService.GetById(1000);
cs.GetValue("test");
var nd = new Node(1000);
nd.GetProperty("test");
Both of that code can be used.. What is the different between that two code.. When and Why we use either one of them
Umbraco Services
The service layer of the new umbraco API introduced in umbraco 6 includes a ContentService, a MediaService, a DataTypeService, and a LocalizationService. Check out the umbraco documentation for documentation on those services and the other umbraco services.
The services in umbraco hit the database and don't leverage all of the caching that umbraco provides. You should use these services sparingly. If you are trying to programatically add/update/delete from the database or if you are trying to get unpublished content from the database, you should use these services. If all you need is to query for published content, you should use the UmbracoHelper because it is much faster.
var cs = ApplicationContext.Current.Services.ContentService.GetById(1000);
cs.GetValue("test");
UmbracoHelper
The UmbracoHelper is what you should almost always be using when you want to query content from umbraco. It doesn't hit the database and is much faster than the umbraco services.
var node = Umbraco.TypedContent(1000);
var nodeVal = node.GetPropertyValue<string>("test");
If you find that you don't have access to the UmbracoHelper, you can make your own as long as you have an UmbracoContext:
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
var node = Umbraco.TypedContent(1000);
var nodeVal = node.GetPropertyValue<string>("test");
NodeFactory
The NodeFactory is obsolete. If you are using Umbraco 6 or higher, I would highly recommend converting to the UmbracoHelper.
var nd = new Node(1000);
nd.GetProperty("test");
In razor or frontend code, always use the UmbracoHelper
var node = Umbraco.TypedContent(1000);
var value = node.GetPropertyValue<string>("test");
This will query the cache for published nodes
You want to use the ContentService call to query the database, for example if you want information about unpublished nodes (you don't want to do this in your views)
Querying with the Node object is probably legacy (I've never used it)
Related
I'm looking at MVCSiteMapProvider but I can't find anything in documentation that would allow me to register lots of urls by index. I have the following
http://example.com/story/1
...
...
http://example.com/story/7000000
I'd like to be able to use the library to automatically serve these in lots of different files. I've read through all the documentation but can't find anything. It seems really fully featured though so I thought I would ask before rolling my own solution.
You can use a dynamic node provider or implement ISiteMapNodeProvider to programatically supply your own data (including custom ids) from any source.
Dynamic node providers can be added without using an external dependency injection container, but you need to add a "template" node either in XML or using .NET attributes to attach the provider to (see the above link).
public class StoryDynamicNodeProvider : DynamicNodeProviderBase
{
public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
{
// Entities would be your entity framework context class
// or repository.
using (var entities = new Entities())
{
// Create a node for each blog post
foreach (var story in entities.Stories)
{
DynamicNode dynamicNode = new DynamicNode();
dynamicNode.Title = story.Title;
// The key of the node that this node will be the child of.
// This works best if you explicitly set the key property/attribute
// of the parent node.
dynamicNode.ParentKey = "Home";
dynamicNode.Key = "Story_" + story.Id;
dynamicNode.Controller = "Story";
dynamicNode.Action = "Details";
// Add the "id" (or any other custom route values)
dynamicNode.RouteValues.Add("id", story.Id);
yield return dynamicNode;
}
}
}
}
Using ISiteMapNodeProvider you can build the entire SiteMap structure including the root node, but currently it requires using an external DI container to inject a custom implementation.
There is an example here of how you could implement ISiteMapNodeProvider yourself. Here is an example of injecting a custom implementation using SimpleInjector.
Do note that there is currently a limitation in the 10s of thousands for the total number of nodes on a server because they are cached in memory, so if you have that many nodes using preservedRouteParameters is a better choice. However, it has a limitation that the individual URLs can only appear in the SiteMapPath, but not in the Menu, SiteMap, or XML Sitemap for search engines.
I think this will be the answer you are looking for:
Add the below to the node in your sitemap.
preservedRouteParameters="id"
There is more documentation here, How to Config MVCSiteMap to realize the parameters?
I'm experimenting with ServiceStack in MVC, using standard server side controllers creating view models. There are no jquery calls (or any direct calls) to any of the services registered at /api. Since SS lets us resolve services directly using:
using (var dr = HostContext.ResolveService<DataReportService>(base.HttpContext))
I haven't been calling the services using JsonServiceClient. Instead I've been resolving the services and calling their methods directly.
var dataReport = new DataReport
{
IsArchived = false,
ReportDate = DateTime.Now,
ReportType = Model.ReportType
};
var drId = dr.Post(dataReport);
However, I have not been able to find a way to do this with the new AutoQuery feature. I know it creates a service automatically for any class that descends from QueryBase but I have had no luck resolving it. If I try to resolve the name used at run time then I won't compile (obviously). If I try something like this
using (var dr = HostContext.ResolveService<AutoQueryServiceBase>(base.HttpContext))
then it won't work either, because that is just a base class and not the actual registered instance. I know I could do this from JsonServiceClient but I'd really like to experiment with the direct call approach. Creating my own service that wraps any AutoQuery would work but seems like it defeats the purpose of the automatic creation. Still, I don't see any other way to proceed. Would love to hear any ideas.
Rather than using the ResolveService<T> method to get the service and call the executing method yourself, you can use HostContext.ServiceController.Execute method which allows you pass in the request DTO which will execute on the action method.
var dataReport = new DataReport
{
IsArchived = false,
ReportDate = DateTime.Now,
ReportType = Model.ReportType
};
var drId = HostContext.ServiceController.Execute(dataReport);
I hope this helps.
I have a question concerning combine datas from 2 repositorys in asp / mvc.
I have 2 repositories, for example, UserRepo and InvoiceRepo.
Now I want to create a query that contains user data and invoice data
(for example: All users with address and data from the last invoice).
What is best way for doing this?
Why do you have to do it with one call?
var users = userRepos.GetUsers();
var lastInvoices = invoiceRepos.GetLastInvoiceForAllUsers();
var usersWithInvoices = (from x in users
select new UserWithInvoice(x, lastInvoices.First(inv => inv.userId = x.Id);
That will be two SELECTs and must be easier to understand.
You can extend an existing repository (probably InvoiceRepo) with a method that performs a join and return all the data you need.
InvoiceRepo.GetUserInvoices(userId) sounds like a sensible option.
This is a common problem with repository pattern - finding a proper repo for your method.
I'm trying to return a JSON list of stuff from my server via an ASP.NET MVC front layer:
var stuff = repo.GetStuff();
return Json(stuff);
However, instead of the expected JSON, I get an error message stating
A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'.
I think I've found where this happens, but to explain it I need a simple example domain model as follows:
I am (lazily?) loading a selection of documents from NHibernate, like so:
var session = getNHibernateSession();
var query = new NhQueryable<Document>(session.GetSessionImplementation());
var docs = query.ToList().AsEnumerable();
I then pass the documents to return a JsonResult in my controller:
return Json(docs, JsonRequestBehavior.AllowGet);
Now, when Json() serailizes the collection, it walks over the properties of a document, finds a person. It serializes that person, and finds a project. It serializes the project, and finds - that's right - the person again! Since I'm lazy loading, it can just keep walking for ever if nothing stops it, but it's stopped by a circular reference error.
I don't really need to go all these levels down (I'd be fine without loading the project in the first place) - can I somehow affect how Json() serializes this collection, to not go further than, say, 2 levels down? I've googled around a little, but most of what I find seems to be from people who decided to use a serializing library directly, rather than just using the built-in functionality in .NET MVC. (Note: The solution to this problem must be possible to apply specifically to this case, since I might want to get JSON lists of people, including projects, somewhere else in the application...)
If you are retrieving Json, you have a service api. You have to design the api besides the implementation. Does the page that will be using it need all those fields and collections? probably not. What about adding more properties for other features and services? They will start appear in all the requests.
What you need is to use a ViewModel or just an anonymous type with the desired structure:
var session = getNHibernateSession();
var query = new NhQueryable<Document>(session.GetSessionImplementation());
var docs = query.ToList();
var result = query.Select(x => new {
x.Id,
x.Name,
People = new { p.Id,
p.Name,
p.Title
}
});
return Json( result, JsonRequestBehavior.AllowGet);
This way you can control what is being rendered and how.
It's already been answered here.
Also, it's generally a bad idea to expose your domain entities like this. If it's for read-only purposes it might not be so bad, but if any of your action methods accept a domain entity, then a specifically formatted request can overwrite properties on your domain entity that you don't want to (such as your PK).
To preserve object references in JSON, add the following code to Application_Start method in the Global.asax file:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.All;
How do I get list of projects from TFS server using webservice?
Is documentation available for the TFS Webservices?
I concur with bryanjonker as well. Don't call the webservice directly, that's really reserved for internal use. Create your own access layer and call the API.
You can also use the object model to get access to the ICommonStructureService. I've recently started using this, and find it to be cleaner. The code below does the same as bryanjonker's example:
var tfs = TeamFoundationServerFactory.GetServer(serverUri);
var projectCollection = tfs.GetService<ICommonStructureService>();
foreach (var projectInfo in projectCollection.ListProjects())
{
listBox1.Items.Add(projectInfo.Name);
}
Your best bet is to use the TFS DLLs and that API, which is at http://msdn.microsoft.com/en-us/library/bb130146(VS.80).aspx and http://msdn.microsoft.com/en-us/library/bb130334(v=VS.80).aspx . From what I understand, hitting the webservice directly is frowned upon.
To get a list of projects, I think there's multiple ways to do this. I posted code I used: use the GetServer method to get a project collection, then list through the ListProjects() method. I needed to do this because I needed to get the areas and iterations as well. This requires the Microsoft.TeamFoundation.Client namespace.
var tfs = TeamFoundationServerFactory.GetServer(Constants.TEAMFOUNDSERVER);
var projectCollection = (ICommonStructureService)tfs.GetService(typeof(ICommonStructureService));
foreach (var projectInfo in projectCollection.ListProjects())
{
//do stuff here
}