Add property decorator to partial class - c#

I have a partial class in a dbml file.
public partial class Comment
string email
Clearly I can't put a decorator on it because this is a generated file and you shouldn't make change in it yourself.
So I created another partial class;
public partial class Comment
[IsEmailAddress]
string email
The above doesn't work but I need something like that so I can validate the email address on the model.

You should used MetadataType like so...
[MetadataType(typeof(CommentMetadata))]
public partial class Comment {
}
public class CommentMetadata {
[IsEmailAddress]
public string email {get;set;}
}
That will allow you to add your attributes without it being overridden the next time you update your models.

Related

Model binder for abstract class reference within abstract class asp.net core mvc 3

I'm following along at Model binder for abstract class in asp.net core mvc 2, but my model doesn't bind the HostedControls in ConcreteControlHost (see below). If I change the type of HostedControls to ConcreteControlText[], it does bind. I'd like to keep it as AbstractControl[] so that I can host multiple types of control.
I know the abstract binder is working because MainModel.Controls binds.
While debugging the binding of ConcreteControlHost, binder._propertyBinders has an entry for HostedControls as follows:
{[ModelMetadata (Property: 'ConcreteControlHost.HostedControls' Type: 'AbstractControl[]'), {Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ArrayModelBinder<MyProject.AbstractControl>}]}
Following that property (Value.ElementBinder.Inner) eventually leads to AbstractModelBinder.
Breakpoints in AbstractModelBinder are not hit when binding the properties of ConcreteControlHost, but are when binding the properties of MainModel (as in, I get the hits for ConcreteControlHost, but not for ConcreteControlText).
This isn't related to In an Editor Template call another Editor Template with the same Model because it isn't the same model, and because everything renders correctly, it just doesn't bind. None of the ConcreteControlTexts referenced by HostedControls are referenced directly by MainModel.Controls.
public class MainModel {
public AbstractControl[] Controls;
}
public abstract class AbstractControl {
public string TypeName { get; set;}
}
public class ConcreteControlText: AbstractControl {
public string Text { get; set; }
}
public class ConcreteControlHost: AbstractControl {
public AbstractControl[] HostedControls { get; set; }
}
Does anyone see what I need to change to allow model-binding to work on ConcreteControlHost.HostedControls?
Turns out in my actual code, HostedControls had { get; private set; }. Removing the private modifier on the setter made it work.
Jeremy, thanks for looking into this.

How to Implement IDataErrorInfo within a WPF Page (A Partial Class)

It's my first experience with WPF. And I'm developing an Inventory Management System. My design model has single Window, in which a frame loads different Pages while clicking on different buttons. While adding a new Inventory into the Database, I want to ensure Data Validation. I choose IDataErrorInfo in this regard. I have to implement the interface but unable to implement just writing as public partial class AddInventoryPage : Page, IDataErrorInfo. This shows error. The signature of the class is as following
public partial class AddInventoryPage : Page
I also tried as under but unable to achieve the functionality. Even I put a breakpoint within IDataErrorInfor part but the control doesn't go there.
namespace IMS
{
public partial class AddInventoryPage : IDataErrorInfo
{
//code here
}
public partial class AddInventoryPage : Page
{
//code here
}
}
As My Inventory module is completed except Data Validation, and I'm working on the Sales module; it's not a solution to change my design model. Moreover, I'm not using any Design Pattern like MVVM. It's straight.
Looking forward to a solution.
example with a validation against the property 'Name'
public class AddInventoryPage : IDataErrorInfo
{
public string Name { get; set; }
public string Error => null;
public string this[string columnName]
{
get
{
switch(columnName)
{
case nameof(Name):
if (Name == string.Empty) return "Name can not be empty";
}
return string.Empty;
}
}
}

What is the substitution for partial property in C#?

I have an application (mvc 3) where the code is autogenerated for the datacontext class. And I need to expand the functionality, so I created partial class with the same name. But I found that only "void methods" could be marked as partial, while I need kind of a partial property.
So, is there any way to expand property's functionality in C#?
Updated:
Here is the code:
public Table<Post> Posts
{
get
{
// writing info into Trace file
Log = Console.Out;
var result = this.GetTable<Post>();
Log = new LogLinqToSql();
SubmitChanges();
return result;
}
}
The thing is that if I make any change to the data model this code will disappear, so how can I move it to the "safer" place?
Sorry for using the answer field, the post is for the discussion above:
Can't you just wrap the property from the DataContext in another property in your class
e.g.
partial class NewClass
{
public Table<Post> NewProperty
{
get
{
DoHouseKeeping();
return this.PropertyFromOtherPartialClass;
}
}
}
Only the classes need to be marked as partial.
The solution for my problem (provide logging) is simple: In the autogenerated code there is a bunch of partial helper methods. And there is a method "OnCreated" which is called when the instance of MyTypeDataContext class is created. So, I just need to do the following:
public partial class WebStoreDataContext
{
partial void OnCreated()
{
// writing info into Trace file
Log = Console.Out;
Log = new LogLinqToSql();
SubmitChanges();
}
}
If You want to add Attributes to the properties this post provides all the information)

Is there an attribute that I can use with ASP.NET MVC 3 to prevent model fields from being automatically included in my views?

Is there an attribute that I can use with ASP.NET MVC 3 to prevent model fields from automatically showing up in my view? What I mean by this is that I have classes like the following:
public class EntityBase
{
public int ID { get; set; }
//more fields...
}
public class TestEntity : EntityBase
{
public string TestEntityName { get; set; }
//more fields...
}
I know about all of the attributes in System.ComponentModel and System.ComponentModel.DataAnnotations to enforce validation - Required, StringLength, etc. - but is there one I can use that will prevent certain fields from showing up in the view when I create it from Visual Studio? All of my project's model classes inherit from EntityBase, but I don't want any of EntityBase's fields to be visible on the view. I'm using Razor as my ViewEngine, in case it matters.
TIA,
Benjy
Use ScaffoldColumn:
[ScaffoldColumn(false)]

Using DataAnnotations with Entity Framework

I have used the Entity Framework with VS2010 to create a simple person class with properties, firstName, lastName, and email. If I want to attach DataAnnotations like as is done in this blog post I have a small problem because my person class is dynamically generated. I could edit the dynamically generated code directly but any time I have to update my model all my validation code would get wiped out.
First instinct was to create a partial class and try to attach annotations but it complains that I'm trying to redefine the property. I'm not sure if you can make property declarations in C# like function declarations in C++. If you could that might be the answer. Here's a snippet of what I tried:
namespace PersonWeb.Models
{
public partial class Person
{
[RegularExpression(#"(\w|\.)+#(\w|\.)+", ErrorMessage = "Email is invalid")]
public string Email { get; set; }
/* ERROR: The type 'Person' already contains a definition for 'Email' */
}
}
A buddy class is more or less the direction your code snippet is journeying, except your manually coded partial Person class would have an inner class, like:
[MetadataType(typeof(Person.Metadata))]
public partial class Person {
private sealed class MetaData {
[RegularExpression(...)]
public string Email { get; set; }
}
}
Or you could have your manually partial Person class and a separate Meta class like:
[MetadataType(typeof(PersonMetaData))]
public partial class Person { }
public class PersonMetaData {
[RegularExpression(...)]
public string Email;
}
These are workarounds and having a mapped Presentation class may be more suitable.
You need to either use a metadata "buddy" class or (my preference) project onto a presentation model instead of binding views directly to entities.

Categories

Resources