I want to store Contacts on to Azure table(name and gender as a property). so I basically two classes . the one which derives from the TableSerivceContext and the other from TableServiceEntity. now I cant connect the pieces. What I will really do at the cotroller(I use MVC3)
tnx for any hint?
im assuming that you are receiving the properties (name and gender) via post from a view.
so your controller might be like this
public ActionResult DoSomething(User model)
{
}
so what you need to do is.. that. make a new ofject of the class thats derived from the TableServiceEntity. and assign the Properties.
like this
var tableUser = new TableUser(){Name = model.Name, Gender=model.Gender}
then from the class derived from TableServiceContext make an object. and use AddObject() method to add the user to the table
http://msdn.microsoft.com/en-us/library/system.data.services.client.dataservicecontext.addobject.aspx
This is what I have done recently to create a very simple MVC3 + Windows Azure Table sample application:
Created a Model Class DataEntity inherit from TableServiceEntity which include all the table properties needed to store along with PartitionKey and RowKey
Created another Model class DataContext inherit from TableServiceConext which includes IQueryable sets up the Table
Created a Controller class which creates HTTPGet and HTTPPost method type ViewResult returning View. The controller also have code to create the Table first using Model DataContext type and then added code to call AddObject as DataEntity type as below:
DataContext context = new DataContext(storageAccount.TableEndpoint.AbsoluteUri, storageAccount.Credentials);
context.AddObject("DataEntryTable", dataEntity);
context.SaveChanges();
Finally you can create views from the controllers.
You would need to inherit ‘Contact’ from TableServiceEntity and a context class from TableServiceContext to provide all the methods to manage your ‘Contact’ entities. You can then invoke methods on the ‘Context’ class from anywhere (including the controller).
I have written an alternate Azure table storage client, Lucifure Stash, which does away with having to inherit from any base calls and supports additional abstractions over azure table storage. Lucifure Stash supports large data columns > 64K, arrays & lists, enumerations, composite keys, out of the box serialization, user defined morphing, public and private properties and fields and more.
It is available free for personal use at http://www.lucifure.com or via NuGet.com.
Download the Windows Azure Platform Training Kit and do the lab on Windows Azure Storage. In 15 minutes you will have a working prototype.
Related
I'm trying to refactor a codebase that was passing domain models to the views, to use DTOs instead.
Overall everything looks cleaner now, except one point: I have some business logic that belongs to the domain but needs to be executed in the view as well.
Example: I have an Activity class, that has a method to check whether a given User can edit it:
class Activity {
public function isEditableBy(User $user): bool {
...
}
}
The domain service layer uses this method to ensure that the Activity can be edited, and throw an exception if not.
The view uses this method to display an Edit button if the Activity is editable by the user.
Question: How to avoid duplicating this logic in the DTO and the model?
I thing you should not put logic in the DTO at all due to its definition "Data Transfer Object". So, "data" not "logic". Instead you can calculate result of the isEditableBy() method in your application service layer, put this result into a DTO and use the DTO in the view layer.
I am still relatively new to development for Windows Store Apps in XAML/C# and I'm currently dealing with a very random and intermittent problem with an app I have written.
Firstly a quick overview of how my app works - user logs on once a day, downloads data from web service and stores the data in xml files. Each time the app opens/resumes the data is loaded from xml, deserialized and stored in memory in the Application.Resouces Resource Dictionary.
The objects I am storing are my own classes which contain Observable Collections of other classes. I have declared these in App.xaml
<localdata:MyClass x:Key="MyClassResource">
When a page needs this data I reference it using
MyClass myClass = (MyClass)App.Current.Resources["MyClassResource"];
and bind it to controls. The user updates the data and these changes would also be saved to file periodically.
I am now starting to doubt whether this is the correct approach for storing my data.
Every so often the users reports problems with the stored data - I don't have enough details to fully discuss the specific problem right now but I wanted advise on whether it is fine to store my own objects in the Application Resource Dictionary.
There's nothing wrong with your approach. It is actually a very common way to create and access the viewmodel. There is an excellent blog post by Paul Stovell describing different approaches to create and access the viewmodel.
Create viewmodel from code-behind within the view
Inject the viewmodel as dependency into the view
Assign viewmodel to view's DataContext property
Set viewmodel via XAML to DataContext property
Define viewmodel as resource in XAML
Use a view model locator in XAML
DataTemplate property in XAML
DataTemplate and view class in XAML
The referenced article describes all 8 approaches with examples. Your approach is number 5.
I'm using EF as my dataprovider to save and validate my items.
I have some custom validation logic which needs access to other services or items.
I know the ValidationContext class supplies these options.
My question is how can I get my own validation context with an service provider or item to EF?
When I implement the IValidateObject interface on my POCO entity I get an instance of the validation context, but where can I make sure my own ValidationContext is used instead of the EF default one?
I want to base some validation on the fact that a service or item is available in the ValidationContext
To pass the ValidationContext you need to override DbContext.ValidateEntity(). The method takes two parameters - entity entry and items. You would pass your validation context in the items dictionary. Take a look at this stackoverflow question.
Shouldn't we be able to ise the DataSource Controls and append data to it from our Data Repository Class Library instead of using declarative the controls but using them with our own select, insert, update, delete methods? Something similar with BindingSource in Windows Forms!
In specific i use the entity framework in a Data Model Class and a Class Library having all the data access methods using the for a Web Client Application project, so I need to handle or the data commands from my class.
Thank you.
In such case don't use EntityDataSource - it doesn't allow you using your own data access class. Use ObjectDataSource instead where you can define your own data access class and provide methods for select, insert, update and delete.
I think this is more of a polymorphism question but it applies to SubSonic table objects...
Here's the thing (and I love this one):
TblUser userObj = new TblUser(1);
Which fills userObj's properties with all of PK=1's goodies.
Now, I'd like to add more properties to the existing user object, for example, an ArrayList property of say, account numbers.
I've seen questions like this around - "add a property to an existing object...", but in this case, would it be most-recommended to create a user wrapper object, then have a TblUser property type, and my own other additional properties in this?
Ok, so it looks like once-again I have come up with a solution to this, but am still curious about the possibility of adding properties to existing objects.
All the generated SubSonic classes are partials so all you need to do to add extra properties/methods to them is to create your own partial class with the same name in the same namespace and the two will be merged at compile time. For example for your TblUser class:
public partial class TblUser
{
public List<AccountNumber> AccountNumbers
{
get
{
// Get and return the AccountNumbers
}
}
}