Making descendants of generic form visible and editable in VS designer - windows

I'm trying to create a base form using generics, so I could do sth like this:
public class GenericForm<T> : Form where T : IEntity
And then:
public class ManageCustomerForm : GenericForm<Customer>
But I'm not able to view ManageCustomerForm's designer. I understand it's because VS attempts to create an instance of GenericForm but doesn't know what type to use on T.
So I tried some solutions using TypeDescriptionProviders, cause it works for the case in which the base form class is abstract. But it doesn't work either. Is it possible?
One detail is I'd like to avoid to create an intermediate class like this:
public class GenericForm<T> : Form where T : IEntity
public class ManageCustomerIntermediateForm : GenericForm<Customer>
public class ManageCustomerForm : ManageCustomerIntermediateForm

I imagine that Customer is data for the form. Instead of binding the data to the form, use a separate data class for the Customer. DataBinding can be used to tie the user interface controls to the data source.
Another idea is to use the Model-View-Controller design pattern to separate the form (the view) from the model (the customer).

Related

Serialize VM vs state class

After my wonderings on the events registration (you can find here ViewModel Event Registration and ViewModel Lifetime), now I'm thinking about viewmodel tombstoning:
In case of Tombstoning, is the ViewModel serialization a good approach ?
I'm thinking about the case in which different viewmodels have a reference to the same class. In case of Viewmodels serialization and deserialization the referenced class instance could have duplicated instance, isn't it ?
Wouldn't be better to have specialized state classes whose unique purpose in to contain all the app data, everyviewmodel get data (i mean reference to the data) from there and update the data in there and the app think only to serialize those specialized class ?
Any experience on this subject is appreciated.
Regards
SkyG
Caliburn Micro has a lot of this built in to the framwork allowing you to save properties of a view model or the entire graph to both phone state and app settings. You just need to create a class and inherit from StorageHandler.
public class PivotPageModelStorage : StorageHandler<PivotPageViewModel>
{
public override void Configure()
{
this.ActiveItemIndex().InPhoneState().RestoreAfterViewLoad();
}
}
And to your other posted question. CM has a nice way of handling the forced view first approach on the phone. It allows you to do page navigation by specifying the VM and it will handle the rest. And as a bonus, if you specify parameters to pass CM will pull them off the query string and populate properties on the target VM.
public void GotoPageTwo()
{
navigationService.UriFor<PivotPageViewModel>().WithParam(x => x.NumberOfTabs, 5).Navigate();
}

Bind customizable views to dynamic models

We are working on an ASP.NET MVC 3 using ext.net and EF 4.
Data model is mapped using EF4.
Views’ content is rendered from customizable XML files.
Example: Within one view, I can display fields that are related to both objects “customer” and “order”, so from this view I can modify the customer data and also add a new order.
How can we bind the view to the custom model that contains 2 objects (customer and order)? Using non strongly typed views will require a source code that will check all different possibilities (If I remove/add a field to display from the XML file, object constructor and CRUD operations parameters will change also.
We are wondering how can we handle such dynamic application?
Is this a common issue that was raised before? Or is there any solution to use dynamic views bound to custom model (object, xml, etc.)?
Your help is very appreciated, please enlighten me.
Based on what you replied to my comment, I can defenitely say that you need strongly typed views. That said, you decide what the model of your view is. If your view needs to manage users and orders at the same time, you can make a class like this:
public class MyCustomViewData
{
public IEnumerable<User> Users {get;set;}
public IEnumerable<Order> Orders {get;set;}
}
and then strongly type your view to MyCustomViewData and you're set. My example is oversimplified but I think you can get the point.
Unless I'm missing something, I believe the normal way round this would be to strongly type your view to say 'user' and then on the user object, define a property which is a collection of 'orders'.

Elegant Asp.Net MVC 3 custom membership wrapper class with extended (custom) methods and properties

I am attempting to write a custom membership class. It seems to work ok inhering the Membership class and providing functions for all the included required items (validate user, create user, delete user, isapproved, etc).
However, where I run into a problem is when I try to add properties or methods.
As all the other properties and methods are public override classes in a sealed class,
the additional properties do not show up.
Say for example (example only, not "real" code):
public sealed class Membership : MembershipProvider
{
public override string ApplicationName
{
get
{
return "myApp";
}
}
public string myValue { get;set;}
}
Now, I understand why myValue will not show up when I try to do Membership.myValue but Membership.ApplicationName will.
My question is, how to extend membership to show the custom items? Do I ditch Membership.xxx entirely and write a wrapper class? If so, how? I can find all the documentation in the world on how to create a custom membership class. I've got a working custom membership that works fine if I use all the available options only. I've got a custom roles provider and a custom config section to store everything and it's best friend.
What I don't have is an elegant solution.
I'd like the end result to be that I use one reference (such as Membership.xxx or myClass.xxxx) to reference all membership items + custom items.
Please provide examples of how to implement or links to appropriate items that will resolve the custom methods item.
Any time you reference the membership instance you will just have to cast it to your class type, that's all.
You can take a look at Extension Methods if you don't want to cast back and forth your instances

ASP.NET MVC 3: unobtrusive JavaScript validation

There are a lot of examples on how to "create your own model". Mark them with DataAnnotations. Scott Guthrie explains how to validate your model when using an ORM. What I don't find is when your model is actually coming in from an external DLL. How do you validate it?
Example:
/* Class coming in from an third-party DLL file. */
public class Person
{
public string Name{get;set;}
public int Age {get;set;}
}
The solution I am thinking of: Inherit the external class and then apply [MetadataType] to the inherited class.
[Metadata(typeof(Person2_Validation))]
public class Person2:Person{}
public class Person2_Validation
{
[Required,Stringlength(50,ErrorMessage="Name required"]
public string Name{get;set;}
[RegularExpression("([0-9]+)")]
public int Age
}
Is there a better way?
You could create a model and use a Mapper (like AutoMapper or EmitMapper or ValueInjecter) to map between your objects, and validate against the mapped model.
When you need to transfer the object back you can map between your model to the recieved model.
This is very similar to a ViewModel approach in ASP.NET MVC.
So it's something like this:
Class A (the class from the DLL)
Class B (your model)
You set all your annotations on B, and create whatever properties you need.
What you use is B. When you get something from the repository/source you map (copy all relevant values) A=>B and send it (let's say as a model in a View).
When you receive B back you validate it, and then map it the other way B=>A, and send it to the repository/service.
BTW: I would recommend using this approach even if model A was YOUR class.
Why use ViewModels instead of Domain Models in Views.
#Linkgoron answer is true. You are searching view model vs domain model. we need to think the model from dll is a domain model and map it to our own view model as we did even when dealing with our own repository/persistence. it is a best practice. don't worry about mapper, it will map automatically.
See this example:
http://weblogs.asp.net/shijuvarghese/archive/2010/02/01/view-model-pattern-and-automapper-in-asp-net-mvc-applications.aspx
See this #Nick DeVore answer why view model instead of domain model here
Why Two Classes, View Model and Domain Model?
and also
Bestpractice - Mixing View Model with Domain Model
Your issue could be one of the reason why :)

How to solve Passing 2 ViewModels one at a time in ascx page

I have 2 ViewModels (CreateVM, EditVM) with Create.aspx and Edit.aspx. Both share template called CreateEditForm.ascx .
Now problem is in CreateEditForm.ascx template I need to define model which will be once of the above ViewModel. Suppose CreateEditForm.ascx has CreateVM defined than I can not pass EditVM from the Edit ActionResult.
How to solve this problem so that my CreateEditForm.ascx can accept both ViewModels. Any generic style?
I hope you guys understand my question.
There's several possible approaches you can take:
If your create UI and your edit UI are very similar (e.g. if they only differ by the presence of an ID field), you can use just one viewmodel and do some conditional logic, e.g. if (Model.Id == null).
If your create UI and your edit UI are very similar you could also use two different viewmodels which implement a common interface and create a partial view which renders just the fields in common, e.g.
public class SomethingEditViewModel : ISomethingViewModel { }
public class SomethingCreateViewModel : ISomethingViewModel { }
public interface ISomethingViewModel
{
// define the common fields here
}
If your create and your edit UI's are way different, just use 2 separate viewmodels and therefore 2 separate views.

Resources