How to make dataannotations outside the entities - wpf

I'm using Wpf and EntityFrameWork in my project. I wanted to use DataAnnotations since it is a better way to validate the data. I managed to make it in my entity files. For example one of my entities include these:
public partial class Employee : INotifyPropertyChanged, IDataErrorInfo
{
public Employee()
{
this.Address = new HashSet<Address>();
this.Certificate = new HashSet<Certificate>();
this.ContactInformation = new HashSet<ContactInformation>();
this.Course = new HashSet<Course>();
this.Education = new HashSet<Education>();
this.EmployeeTeam = new HashSet<EmployeeTeam>();
this.Evaluation = new HashSet<Evaluation>();
this.ExamInformation = new HashSet<ExamInformation>();
this.JobTitle = new HashSet<JobTitle>();
this.MilitaryService = new HashSet<MilitaryService>();
this.Relationship = new HashSet<Relationship>();
this.WorkExperience = new HashSet<WorkExperience>();
}
public int Id { get; set; }
private string _CitizenId;
[Required(ErrorMessage = "CitizenId is required!")]
[RegularExpression(#"^\d+$", ErrorMessage = "Only numbers allowed")]
public string CitizenId
{
get
{
return _CitizenId;
}
set
{
_CitizenId = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("CitizenId"));
}
}
}
private int _RegNumber;
[Required (ErrorMessage = "Registration Number is required!")]
[RegularExpression(#"^\d+$", ErrorMessage = "Only numbers allowed")]
public int RegNumber
{
get
{
return _RegNumber;
}
set
{
_RegNumber = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("RegNumber"));
}
}
}
public Nullable<short> ResourceTypeId { get; set; }
public Nullable<short> EmployeeRoleId { get; set; }
private string _FirstName;
[Required(ErrorMessage="Name is required!")]
[RegularExpression(#"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage="Numeric expressions are not allowed!")]
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
}
}
}
private string _MiddleName;
[RegularExpression(#"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numeric expressions are not allowed!")]
public string MiddleName
{
get
{
return _MiddleName;
}
set
{
_MiddleName = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("MiddleName"));
}
}
}
private string _Surname;
[Required(ErrorMessage= "Surname is required!")]
[RegularExpression(#"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numeric expressions are not allowed!")]
public string Surname {
get
{
return _Surname;
}
set
{
_Surname = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Surname"));
}
}
}
private int _CustomerNo;
[Required(ErrorMessage = "Customer Number is required!")]
[RegularExpression(#"^\d+$", ErrorMessage = "Only numbers allowed")]
public int CustomerNo
{
get
{
return _CustomerNo;
}
set
{
_CustomerNo = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("CustomerNo"));
}
}
}
public System.DateTime BirthDate { get; set; }
private string _BirthPlace;
[Required(ErrorMessage="Birh Place is required!")]
[RegularExpression(#"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numeric expressions are not allowed!")]
public string BirthPlace
{
get
{
return _BirthPlace;
}
set
{
_BirthPlace = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("BirthPlace"));
}
}
}
public string Gender { get; set; }
public string MaritalStatus { get; set; }
public System.DateTime WorkBeginDate { get; set; }
//indicates that whether the start date is bigger than the end date which is a validation problem
// [CustomValidationsForDate("WorkBeginDate", ErrorMessage="Starting date cannot be bigger than the ending date")]
public Nullable<System.DateTime> WorkEndDate { get; set; }
private string _LogonName;
[Required(ErrorMessage = "Logon Name is required!")]
public string LogonName
{
get
{
return _LogonName;
}
set
{
_LogonName = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("LogonName"));
}
}
}
private int _DistanceToWork;
[RegularExpression(#"^\d+$", ErrorMessage = "Only numbers allowed")]
[Required(ErrorMessage="This field is required!")]
public int DistanceToWork
{
get
{
return _DistanceToWork;
}
set
{
_DistanceToWork = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("DistanceToWork"));
}
}
}
public Nullable<bool> HasInternet { get; set; }
public Nullable<bool> HasRemoteConnection { get; set; }
public int MilitaryStatus { get; set; }
public Nullable<int> DrivingLicense { get; set; }
public bool IsSmoker { get; set; }
public bool IsActive
{ get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
public string LeavingReason { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<System.DateTime> EndDate { get; set; }
public virtual ICollection<Address> Address { get; set; }
public virtual ICollection<Certificate> Certificate { get; set; }
public virtual ICollection<ContactInformation> ContactInformation { get; set; }
public virtual ICollection<Course> Course { get; set; }
public virtual ICollection<Education> Education { get; set; }
public virtual ICollection<EmployeeTeam> EmployeeTeam { get; set; }
public virtual ICollection<Evaluation> Evaluation { get; set; }
public virtual ICollection<ExamInformation> ExamInformation { get; set; }
public virtual ICollection<JobTitle> JobTitle { get; set; }
public virtual ICollection<MilitaryService> MilitaryService { get; set; }
public virtual ICollection<Relationship> Relationship { get; set; }
public virtual ICollection<WorkExperience> WorkExperience { get; set; }
//Following methos have been used in order to apply DataAnnotations
public event PropertyChangedEventHandler PropertyChanged;
public string Error { get { return String.Empty; } }
public string this[string property]
{
get { return ValidateProperty(this, property); }
}
public static string ValidateProperty(object instance, string propertyName)
{
PropertyInfo property = instance.GetType().GetProperty(propertyName);
object value = property.GetValue(instance, null);
List<string> errors = (from v in property.GetCustomAttributes(true).OfType<ValidationAttribute>()
where !v.IsValid(value)
select v.ErrorMessage).ToList();
return (errors.Count > 0) ? String.Join("\r\n", errors) : null;
}
}
}
So like I said I'm applying dataannotations inside the entities but what I want is doing that outside because whenever a change occurs in my database, all the entities will be refreshed so what I have here is not a dynamic structure.
So could you please help?
Thanks in advance.
Best regards.
Ege

You should create a model (view model) class and map from your entities (domain models) to the view model. AutoMapper can help speed this up. You then put your annotations on your view models and use these in your views.
This means that if your entity model changes you just need to update the mapping. It also means that you can create multiple different models for the same entity so your validation doesn't have to be the same for each view that uses it.

Related

INotifyPropertyChanged won't update grid in the window [duplicate]

This question already has answers here:
INotifyPropertyChanged WPF
(3 answers)
using of INotifyPropertyChanged
(3 answers)
Closed last year.
I've been this for a while now but I can't seem to be figuring out why my code won't update the window. The events are called and the invoke methods are invoked, but still, my window won't update
public class RaceContext : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Race CurrentRace { get; set; }
public string TrackName { get => CurrentRace == null ? "" : "Track name : " + CurrentRace.Track.Name; }
public List<IParticipant> Participants { get; set; }
public ObservableCollection<DriverListView> DriverListViews
{
get { return _DriverListViews; }
set { _DriverListViews = value; OnPropertyChanged(nameof(DriverListViews)); }
}
public ObservableCollection<DriverListView> _DriverListViews = new ObservableCollection<DriverListView>();
public void OnNextRace(object sender, NextRaceEventArgs e)
{
CurrentRace = e.Race;
e.Race.DriverChanged += OndriversChanged;
e.Race.DriverMoved += OnDriverMoved;
DriverListViews = new ObservableCollection<DriverListView>();
CurrentRace.Participants.ForEach(item =>
{
DriverListViews.Add(new DriverListView()
{
Driver = item
});
});
OnPropertyChanged();
}
public void OndriversChanged (object sender, DriversChangedEventArgs e)
{
Participants = CurrentRace.Participants;
e.DriverTimers.ForEach(timer =>
{
var view = DriverListViews.Where(item => item.Driver.Equals(timer.Driver)).First();
view.RaceStopwatch = timer.TotalRaceTimer.Elapsed;
view.LapStopwatch = timer.LapTimer.Elapsed;
});
OnPropertyChanged(nameof(DriverListViews));
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
This is the list view class
public class DriverListView
{
public IParticipant Driver { get; set; }
public int Position { get; set; }
public TimeSpan RaceStopwatch { get; set; }
public int LapCount { get; set; }
public TimeSpan LapStopwatch { get; set; }
public string LapPercentage { get; set; }
public int SectionCount { get; set; }
public string ParticipantName { get => Driver.Name; }
public TeamColors TeamColor { get => Driver.TeamColor; }
public int ParticpantPoints { get => Driver.Points; }
}
So when OnDriverChanged is called, the INotifyPropertyChanged is called. the window does not update the records
Thanks

How to return a string in the dataGrid.ItemsSource when it contains a ICollection<Continent>

When im displaying my database table it contains a few collections and those dont display in the datagrid. a column is generated but it stays blank. I do not know where to implement this either.
Maybe this will help also to explain what i want to display in the column instead of the ICollection
static void Main(string[] args)
{
using (var db = new Whataboutthisfish())
{
Vis vis = db.Vissen.Find(2);
if(vis != null)
{
if(vis.Continenten != null)
{
string s = "";
if(vis.Continenten.Count() > 1)
{
var continentenLijst = vis.Continenten;
s = continentenLijst.First().Naam;
foreach (Continent c in vis.Continenten)
{
s += ", "+c.Naam;
}
}
else
{
s = vis.Continenten.First().Naam;
}
Console.WriteLine(s);
}
}
}
}
I would like to return 1 string containing each continent's name using a seperator.
Like: "North-America" if there's only 1 continent in the collection
Or: "North-America, South-America" and so on for multiple.
Classes;
[Table("Continenten")]
public partial class Continent
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Continent()
{
Vissen = new HashSet<Vis>();
}
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Naam { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Vis> Vissen { get; set; }
}
}
[Table("Vissen")]
public partial class Vis
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Vis()
{
Continenten = new HashSet<Continent>();
Verbanden = new HashSet<Verband>();
Waterlagen = new HashSet<Waterlaag>();
}
public int Id { get; set; }
[StringLength(200)]
public string Naam { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Continent> Continenten { get; set; }
}
The Xaml for grid:
<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="0.833,0.846" Margin="10,10,10,10"/>
In code behind:
using (var db = new Catattafish.Whataboutthisfish())
{
dataGrid.ItemsSource = db.Vissen.ToList();
}

Breeze saving strategy

I'm creating my first spa with angular and breeze. So for so good and i'm very happy with the progress I've made. But now I'm stuck on my editing and saving my entity product (example class below). When I edit a product i also call the related products, and I have a checkbox (on saving) that says "override related products with same info". But what is the best way to do this? Server side? Should i expand the model on the client side? Are there examples available?
Product:
public class Product
{
#region Fields
private ICollection<ProductCategory> _productCategories;
private ICollection<ProductManufacturer> _productManufacturers;
private ICollection<ProductPicture> _productPictures;
private ICollection<ProductSpecificationAttribute> _productSpecificationAttributes;
private ICollection<ProductTierPrice> _productTierPrices;
#endregion Fields
#region Properties
public int Id { get; set; }
public ProductType ProductType { get; set; }
public int ParentGroupedProductId { get; set; }
public bool VisibleIndividually { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string MetaTitle { get; set; }
public string MetaDescription { get; set; }
public int DisplayOrder { get; set; }
public bool LimitedToStores { get; set; }
public string Sku { get; set; }
public string UniqueCode { get; set; }
public decimal Price { get; set; }
public decimal OldPrice { get; set; }
public decimal? SpecialPrice { get; set; }
public DateTime? SpecialPriceStartDateTimeUtc { get; set; }
public DateTime? SpecialPriceEndDateTimeUtc { get; set; }
public decimal DiscountPercentage { get; set; }
public bool HasTierPrices { get; set; }
public TaxRate TaxRate { get; set; }
public bool SyncToShop { get; set; }
public bool Deleted { get; set; }
public bool Locked { get; set; }
public State State { get; set; }
public DateTime? DateChanged { get; set; }
public DateTime? DateCreated { get; set; }
#endregion Properties
#region Mapping
public virtual ICollection<ProductCategory> ProductCategories
{
get { return _productCategories ?? (_productCategories = new List<ProductCategory>()); }
protected set { _productCategories = value; }
}
public virtual ICollection<ProductManufacturer> ProductManufacturers
{
get { return _productManufacturers ?? (_productManufacturers = new List<ProductManufacturer>()); }
protected set { _productManufacturers = value; }
}
public virtual ICollection<ProductPicture> ProductPictures
{
get { return _productPictures ?? (_productPictures = new List<ProductPicture>()); }
protected set { _productPictures = value; }
}
public virtual ICollection<ProductSpecificationAttribute> ProductSpecificationAttributes
{
get { return _productSpecificationAttributes ?? (_productSpecificationAttributes = new List<ProductSpecificationAttribute>()); }
protected set { _productSpecificationAttributes = value; }
}
public virtual ICollection<ProductTierPrice> ProductTierPrices
{
get { return _productTierPrices ?? (_productTierPrices = new List<ProductTierPrice>()); }
protected set { _productTierPrices = value; }
}
#endregion Mapping
}
Related Product:
public class RelatedProduct
{
#region Fields
#endregion Fields
#region Properties
public int Id { get; set; }
public int ProductId1 { get; set; }
public int ProductId2 { get; set; }
public int DisplayOrder { get; set; }
public State State { get; set; }
#endregion Properties
//#region Mapping
//public virtual Product Product1 { get; set; }
//public virtual Product Product2 { get; set; }
//#endregion Mapping
}
Capture Changes of All Products in Clinent Jquery Array and send to Server Side..
Serverside change your controller method argument to IEnumerable products , so you can save all changes in Batch
If you want to update only change value use HttpPatch on server side and update changed value only

How to use an object as a data source c#

I'm trying to figure out how to make an object data source where I can select which columns to display in Visual Studio and all that. Here's what I have so far, but I'm not sure what else I'm supposed to do?
public class ItemData
{
public string ItemName { get; set; }
public string Description { get; set; }
public string Quantity { get; set; }
public string ManuPartNumber { get; set; }
public string ListID { get; set; }
public string VendorRef { get; set; }
public string VendorName { get; set; }
public string EditSequence { get; set; }
public string UPC { get; set; }
}
public class ItemDataSource : IEnumerable<ItemData>
{
private ICollection<ItemData> list;
public ItemDataSource()
{
try
{
list = QBCom.GetItemList();
}
catch (Exception e)
{
list = new List<ItemData>();
}
}
public ItemDataSource(IEnumerable<ItemData> data)
{
list = data.ToList();
}
public IEnumerator<ItemData> GetEnumerator()
{
foreach (var item in list)
{
yield return item;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return ((IEnumerable<ItemData>)this).GetEnumerator();
}
}
I am not talking about ASP.net, I'm talking about stuff like DataTables and so forth. Just a regular winforms program.
I assume you want to bind to a DataGridView if so I would inherit from BindingSource and set the list to the DataSource. You can then set the datagridview DataSource to the object to view the columns in the visual studio properties window.
public class ItemDataSource : BindingSource
{
private ICollection<ItemData> list;
public ItemDataSource()
{
try
{
list = QBCom.GetItemList();
}
catch (Exception e)
{
list = new List<ItemData>();
}
this.DataSource = list;
}
public ItemDataSource(IEnumerable<ItemData> data)
{
list = data.ToList();
this.DataSource = list;
}
}

byte array getter setter in C# asp.net

i am taking bytes array from file upload control and saving them in a class object but i am getting exception of stackoverflow infinite loop or recursive like sonmething.
my code is:
public class UploadDetail
{`enter code here`
//public bool IsReady { get; set; }
public string FileSize { get; set; }
//public int UploadedLength { get; set; }
public string FileName { get; set; }
public byte[] FileinBytes;
public byte[] FileBytes
{
get
{
return FileBytes;
}
set
{
FileBytes = value;
}
}
here is my gridview button event where i am setting the objects value:
protected void gvUploadFiles_Clicked(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "doingUpload")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvUploadFiles.Rows[index];
if (gvUploadFiles.DataKeys[index]["FileName"] != null)
{
currentUpload.FileName = gvUploadFiles.DataKeys[index]["FileName"].ToString();
}
if (gvUploadFiles.DataKeys[index]["FileSize"] != null)
{
currentUpload.FileSize = gvUploadFiles.DataKeys[index]["FileSize"].ToString();
}
if (gvUploadFiles.DataKeys[index]["FileBytes"] == null)
{
currentUpload.FileBytes=(byte[])gvUploadFiles.DataKeys[index]["UploadDetail.FileBytes()"];
//currentUpload.FileBytes(row.FindControl("fileBytes"));
}
You've got an infinite recursion in your getter/setter.
Do you want to write it to FileinBytes?
public byte[] FileBytes
{
get
{
return FileinBytes;
}
set
{
FileinBytes = value;
}
}
An alternative could be to use an auto implemented property:
public byte[] FileBytes { get; set; }

Resources