Consider a domain where a Customer, Company, Employee, etc, etc, have a ContactInfo property which in turn contains a set of Address(es), Phone(es), Email(s), etc, etc...
Here is my abbreviated ContactInfo:
public class ContactInfo : Entity<int>
{
public ContactInfo()
{
Addresses = new HashSet<Address>();
}
public virtual ISet<Address> Addresses { get ; private set; }
public Address PrimaryAddress
{
get { return Addresses.FirstOrDefault(a => a.IsPrimary); }
}
public bool AddAddress(Address address)
{
// insure there is only one primary address in collection
if (address.IsPrimary)
{
if (PrimaryAddress != null)
{
PrimaryAddress.IsPrimary = false;
}
}
else
{
// make sure the only address in collection is primary
if (!Addresses.Any())
{
address.IsPrimary = true;
}
}
return Addresses.Add(address);
}
}
Some notes (I am not 100% sure if these are EF "best practices"):
collection of Address(es) is virtual to allow for lazy loading
private setter on collection prohibits collection replacement
collection is an ISet to insure that there are no duplicate addresses per contact
using AddAddress method I can insure that there is always and at most 1 address which is primary....
I would like (if possible) to prevent adding Addresses via ContactInfo.Addresses.Add() method and to force using of ContactInfo.AddAddress(Address address)...
I am thinking exposing the set of addresses via ReadOnlyCollection but will this work with Entity Framework (v5)?
How would I go about this?
Another option suggested by Edo van Asseldonk is to create a custom collection that inherits its behavior from Collection.
You'd have to make your own implementation for ISet but the principle is the same.
By hiding any methods that modifies the list and marking them as obsolete you effectively get a ReadOnlyCollection but EF will still be able to modify it when it's unboxed as Collection. In my version I've added an implicit operator conversion for List so we don't have to unbox the collection when adding items:
var list = ListProperty.ToList();
list.Add(entity)
ListProperty = list;
Where
public virtual EntityCollection<MyEntity> ListProperty { get; protected set; }
and here's the EntityCollection:
public class EntityCollection<T> : Collection<T>
{
[Obsolete("Unboxing this collection is only allowed in the declarating class.", true)]
public new void Add(T item) { }
[Obsolete("Unboxing this collection is only allowed in the declarating class.", true)]
public new void Clear() { }
[Obsolete("Unboxing this collection is only allowed in the declarating class.", true)]
public new void Insert(int index, T item) { }
[Obsolete("Unboxing this collection is only allowed in the declarating class.", true)]
public new void Remove(T item) { }
[Obsolete("Unboxing this collection is only allowed in the declarating class.", true)]
public new void RemoveAt(int index) { }
public static implicit operator EntityCollection<T>(List<T> source)
{
var target = new EntityCollection<T>();
foreach (var item in source)
((Collection<T>) target).Add(item); // unbox
return target;
}
}
This way you can still run your Linq as usual but will get a proper warning of usage when trying to modify the Collection property. Un-boxing it to a Collection would be the only way:
((Collection<MyEntity>)ListProperty).Add(entity);
One way is to make the ICollection property protected and create a new property of IEnumerable that just returns the list of the ICollection property.
The downside with this is that you are not able to query on addresses through the ContactInfo like get all contactinfo that lives in this city.
This is not possible!:
from c in ContactInfos
where c.Addresses.Contains(x => x.City == "New York")
select c
Code:
public class ContactInfo : Entity<int>
{
public ContactInfo()
{
Addresses = new HashSet<Address>();
}
protected virtual ISet<Address> AddressesCollection { get ; private set; }
public IEnumerable<Address> Addresses { get { return AddressesCollection; }}
public Address PrimaryAddress
{
get { return Addresses.FirstOrDefault(a => a.IsPrimary); }
}
public bool AddAddress(Address address)
{
// insure there is only one primary address in collection
if (address.IsPrimary)
{
if (PrimaryAddress != null)
{
PrimaryAddress.IsPrimary = false;
}
}
else
{
// make sure the only address in collection is primary
if (!Addresses.Any())
{
address.IsPrimary = true;
}
}
return Addresses.Add(address);
}
}
Related
I have class with method remove, it needs to delete Customer from CustomerList.
public class ObservableList<T>
{
List <T> CustomerList = new List<T>();
public void Remove(ObservableList<Customer> list) // not correct
{
//something for delete from list;
}
}
indexer in this class:
public T this[int indexer]
{
get { return CustomerList[indexer]; }
set { CustomerList.Add(value); }
}
The string for testing this method looks like this:
ObservableList<Customer> list = new ObservableList<Customer>();
LoadFromBin(list, "source1.bin");
list.Remove(list[2]);
The question is, how to properly format the input data for this method(Remove)? And is the indexer executed correctly in my code?
Based on the attempted usage Remove should accept T, not a collection:
public class ObservableList<T>
{
List <T> CustomerList = new List<T>();
public void Remove(T toRemove) => CustomerList.Remove(toRemove);
}
But again, based on usage it seems you should consider implementing RemoveAt:
public class ObservableList<T>
{
List <T> CustomerList = new List<T>();
public void Remove(T toRemove) => CustomerList.Remove(toRemove);
// usage: list.RemoveAt(2);
public void RemoveAt(int index) => CustomerList.RemoveAt(index);
}
As for the indexer - it is covered in the docs, your set method should set value for index, not add element:
public T this[int indexer]
{
get { return CustomerList[indexer]; }
set { CustomerList[indexer] = value; }
}
But in general, unless it is some exercise you are doing - consider advice from #Jeroen Mostert in the comments - see if existing collections won't serve your needs better than trying to write your own.
I have three columns in the db table that looks as follow:
When I add a new row, it should store the value on column fieldname in uppercase. How can I do that?
Since you tagged the question with entity framework, I assume you want to do it in your data layer or close to DB. There's a number of ways for doing this.
You could override SaveChanges() in your context. This will move the logic away from the model, but still ensure that the correct value is saved. Also, if you want it on several entities you can use an interface. When it's an interface you can do it for several of your entities without any duplicate code, as long as it's the same property. Otherwise you would need an attribute and reflection. Reusability is pretty high, but it adds some overhead to your SaveChanges().
public class CustomerEntity()
{
public string Name {get;set;}
}
public MyCustomContext : DbContext
{
// Other stuff...
public override int SaveChanges()
{
foreach (var entry in ChangeTracker.Entries<CustomerEntity>())
{
if (entry.State == EntityState.Modified || entry.State == EntityState.Added)
{
// Possibly check for null or if it's changed at all.
entry.Entity.Name = entry.Entity.Name.ToUpper();
}
}
return base.SaveChanges();
}
}
And with an interface:
public interface INameIsAlwaysUpperCase
{
string Name {get;set;}
}
public MyCustomContext : DbContext
{
// Other stuff...
public override int SaveChanges()
{
foreach (var entry in ChangeTracker.Entries<INameIsAlwaysUpperCase>())
{
if (entry.State == EntityState.Modified || entry.State == EntityState.Added)
{
// Possibly check for null or if it's changed at all.
entry.Entity.Name = entry.Entity.Name.ToUpper();
}
}
return base.SaveChanges();
}
}
You can add a custom validation. This will throw exception if it's not saved correctly. That way you can move the responsibility to the consumer of the model. However, depending on your scenario, you might not want to throw an exception. This is my favourite since it forces the consumer to do it the right way. As per comments, why throw when you can silently convert it? Yes, it's a valid question. For me it's about forcing the consumer of the data layer to use it correctly, and not let the daya layer decide what to to with the data. I personally don't like it when the business layer asks the data layer to save one thing, and then the data layer saves another thing. If lower case isn't a valid option, then it shouldn't be saved. I don't think it's much more different from using [Required]. But it's really about context and what works in your particular case.
public class CustomerEntity() : IValidatableObject
{
public string Name {get;set;}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
// Possibly check for null here as well...
if (this.Name.ToUpper() != this.Name)
{
yield return new ValidationResult("You need to save as upper!");
}
}
}
Use a property that manages this for you. This may be the simplest solution, even if I like to keep my entities "clean". It's absolutely the solution that will require least effort. However, the reusability is low, and what if you use your entitites all over the application and want the value to be lower case until it's actually saved? That's not possible. But, again, I think it comes down to your particular situation. If you want the value to be upper case even before you save it, this is probably the best solution!
public class CustomerEntity()
{
string _name;
public string Name
{
get { return _name; }
set { _name = value.ToUpper(); } // Check for null ?
}
}
Do it when saving. This moves the logic to when you're saving your entity. This is probably the least preferable option, since the reusability is non-existing. What happens in Update()? However, the OP specifically states "When I add a new row", so it may only be applicable when adding new entities. And in that case it could very well be the most prefered choice since it allows updates to have lower case. But it would have to depend on the use case.
public void AddCustomer(string name)
{
var customer = new CustomerEntity
{
Name = name.ToUpper()
};
_context.Customers.Add(customer);
}
Just use properties. If your model is as below:
public class MyModel
{
public int Id { get; set; }
public string Description { get; set; }
public string LanguageCode { get; set; }
public string FiledName { get; set; }
}
Then, change it to:
public class MyModel
{
private string fieldName;
public int Id { get; set; }
public string Description { get; set; }
public string LanguageCode { get; set; }
public string FiledName
{
get { return filedName; }
set
{
if(!string.IsNullOrEmpty(value))
fieldName = value.ToUpper();
else
fieldName = value;
}
}
}
Try this.
public string FiledName
{
get { return filedName; }
set
{
filedName = !string.IsNullOrEmpty( value ) ? value.ToUpper() : value;
}
}
Using a ValueConverter on the Property could be an effective way to do this.
public class YourDbContext : DbContext
{
public YourDbContext(DbContextOptions<YourDbContext> options)
: base(options)
{
}
public DbSet<Row> Rows { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
var converter = new ValueConverter<string, string>(
v => v.ToUpper(), // writing
v => v
);
// just one property
modelBuilder.Entity<Row>()
.Property(x => x.Column)
.HasConversion(converter);
// all of the string properties
foreach (var entityType in builder.Model.GetEntityTypes())
{
foreach (var property in entityType.GetProperties())
{
if (property.ClrType == typeof(string))
{
builder.Entity(entityType.Name)
.Property(property.Name)
.HasConversion(converter);
}
}
}
}
}
It's also possible to use a Custom Attribute :
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class Standardized : Attribute
{}
Then decorate properties inside your model :
public class MyModel
{
public string Id{ get; set; }
[Required]
[Standardized]
public string Description { get; set; }
}
Taken from #smoksnes accepted answer, inside your DbContext class, override SaveChanges(), SaveChangesAsync() (EF Core 5.x) and add a private method using reflection to obtain decorated properties and apply transformations, like this :
public override int SaveChanges()
{
StandardizeBeforeSaving();
return base.SaveChanges();
}
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
{
StandardizeBeforeSaving();
return await base.SaveChangesAsync(cancellationToken);
}
private void StandardizeBeforeSaving()
{
foreach (var entry in ChangeTracker.Entries())
{
if (entry.State == EntityState.Modified || entry.State == EntityState.Added)
{
var properties = entry.Entity
.GetType()
.GetProperties()
.Where(prop => Attribute.IsDefined(prop, typeof(Standardized)) && prop.PropertyType == typeof(string));
foreach (var property in properties)
{
var value = entry.CurrentValues[property.Name]?.ToString() ?? string.Empty;
entry.CurrentValues[property.Name] = value.Standardize();
}
}
}
}
Just be aware that reflection could be slower than other techniques presented in accepted answer. But for most scenarios (ie. user updates or creates couple of entities with not that many properties) it should be fine.
This may be trivial. But I could not able to get my heads over this.
public class Manager
{
public int ID { get; set; }
public List<Employee> Employees { get; set; }
public bool IsAllEmpEngaged { get; set; }
public void UpdateIsAllEmpEngaged()
{
IsAllEmpEngaged = Employees.All(emp => emp.IsEngagedwithWork == true);
}
}
public class Employee
{
public int ID { get; set; }
public bool IsEngagedwithWork { get; set; }
}
So, Whenever, the IsEngagedwithWork of Employee is setted with some value, I want to check whether all the Employees under aManager is Engaged with work or not and update the value of IsAllEmpEngaged of the respective Manager.
I just want to call UpdateIsAllEmpEngaged on changes in property IsEngagedwithWork of Employee. How can I achieve this?
Any other ways are also welcome.
Note: I tried with a having an event on Employee and attach Action from the Manager that will callback if any changes in Employee property. But I will be having hundreds of List<Manager>. I dont want to add event for each and every instance of Employee class. Any easy way?
Update:
I am working with WPF MVVM approach, I cannot use direct get with LinQ as it will not notify the UI. I have to set the property manually for change so that it will Notify the UI.
Also, In actual case, the IsEngagedwithWork will be updated in UI for the property IsEngagedwithWork.
Simple solution is add ManagerId as well to the Employee model class and after your line of code that sets IsEngagedwithWork of the employee instance (say emp), do the below thing
Manager mngr = managers.Select(m => m.ID == emp.ManagerId).FirstOrDefault();
if(mngr != null)
mngr.IsAllEmpEngaged = mngr.IsAllEmpEngaged && emp.IsEngagedwithWork;
I'd use the getter of the property like that
public bool IsAllEmpEngaged {
get {
return (Employees != null) &&
Employees.All(e => e.IsEngagedwithWork)
}
}
and you add the following method for the Manager Class
public void NotifyChanged() { OnPropertyChanged(() => IsAllEmpEngaged }
then you call it from the Employee Class (assuming you have the managers' list or an equivalent way)
private int _ID;
private bool _IsEngagedwithWork;
public int ID {
get { return _ID};
set {
_ID = value;
OnPropertyChanged(()=>ID );
notifyMe = managerList.FirstOrDefualt(m => m.ID == _ID);
if (notifyMe != null) { notifyMe.NotifyChanged()}
}
}
public bool IsEngagedwithWork {
get { return _IsEngagedwithWork ;}
set {
_IsEngagedwithWork = value;
OnPropertyChanged(()=>IsEngagedwithWork );
notifyMe = managerList.FirstOrDefualt(m => m.ID == _ID);
if (notifyMe != null) { notifyMe.NotifyChanged()}
}
}
What i would do :
make IsAllEmpEngaged private
make the collection Empoyess private and add a function that add a new employee:
Add a function that adds a new employee.
And now two choiches :
1) after the employee is added, iterate the collection and update the IsAllEmpEngaged property
public void AddNewEmployee(Employee employee){
this.Employees.Add(employee);
bool all = true;
foreach(Employee emp in this.Employees){
if (!emp.IsEngagedwithWork){
all = false;
break;
}
}
this.IsAllEmpEngaged = all;
}
2)
start with IsAllEmpEngaged = true; when the class is inited and the collection is empty
when the employee is added, update the IsAllEmpEngaged property but keeping count of the last choice (this works only if you don't remove employess)
public void AddNewEmployee(Employee employee){
this.Employees.Add(employee);
this.IsAllEmpEngaged = this.IsAllEmpEngaged && employee.IsEngagedwithWork
}
I can't say that this is any better, but surely this is a simple and easy way.
Why not just make IsAllEmpEngaged a method? You don't even need the set property accessor, so a method should suffice.
public bool IsAllEmpEngaged()
{
if (Employees == null)
{
// throw error
}
return Employees.All(e => e.IsEngagedwithWork);
}
Maybe it work's when you pass your Manager as parameter to Employee and then call your Method if IsEngagedWithWork is set to true.
public class Employee
{
private Manager _parentManager;
public Employee(Manager parentManager)
{
_parentManager=parentManager;
}
public int ID { get; set; }
private bool _isEngangedWithWork;
public bool IsEngagedwithWork
{
get{ return _isEngangedWithWork; }
set
{
_isEngangedWithWork=value;
if(_isEngangedWithWork)
_parentManager.UpdateIsAllEmpEngaged();
}
}
}
There is tons of info about skipping Properties based on conditionals, but I would like to skip the entire object based on conditions within the object's class. I would like a solution that is contained within the object's class if at all possible. Keep in mind this is a collection of myObj that I am serializing.
public class myObj
{
bool conditional;
ShouldSerialize()
{
return conditional;
}
}
Or
public class myObj
{
[JsonCondition]
public bool conditional{get;}
}
Or even
[JsonCondition(typeof(MyConditionChecker))]
public class myObj
{
public bool conditional{get;}
}
class MyConditionChecker: JsonCondition
{
public override bool CanConvert(object sourceObj)
{
return (sourceObj as myObj).conditional;
}
}
What I got from your comments you would be best served creating your own wrapper around Json that applies the filtering.
public interface IConditionalSerializer
{
bool ShouldBeSerialized();
}
public static class FilteredSerializer
{
public static string SerializeConditional<T>(IEnumerable<T> input)
where T : IConiditionalSerializer
{
return JsonConvert.SerializeObject(input.Where(e => e.ShouldBeSerialized()));
}
}
public class Demo : IConditionalSerializer
{
public bool ShouldBeSerialized() => false;
}
You might also replace the interface with a reflection approach, but keep in mind the performance loss.
public interface IConiditionChecker
{
bool ShouldBeSerialized(object instance);
}
public class ConditionAttribute : Attribute
{
public Type ConditionChecker { get; set; }
}
public static class FilteredSerializer
{
public static string SerializeConditional(IEnumerable<object> input)
{
var matches = (from entry in input
let att = entry.GetType().GetCustomAttribute<ConditionAttribute>()
let hasChecker = att != null && att.ConditionChecker != null
let checker = hasChecker ? (IConiditionChecker)Activator.CreateInstance(att.ConditionChecker) : null
where checker.ShouldBeSerialized(entry)
select entry);
return JsonConvert.SerializeObject(matches);
}
}
[Condition(ConditionChecker = typeof(SomeChecker))]
public class Demo
{
}
Edit: Based on your comment you could do this. Only must decide wether to use opt-in or opt-out in the where-statement. It must ether be casted != null && casted.ShouldBeSerialized or what it currently says.
public interface IShouldBeSerialized
{
bool ShouldBeSerialized();
}
public static class FilteredSerializer
{
public static string SerializeConditional(IEnumerable<object> input)
{
var matches = (from entry in input
let casted = entry as IShouldBeSerialized
where casted == null || casted.ShouldBeSerialized()
select entry);
return JsonConvert.SerializeObject(matches);
}
}
public class Demo : IShouldBeSerialized
{
public bool ShouldBeSerialized()
{
return false;
}
}
If you're able to use the JSON.NET serializer, in terms of not serializing specific items within a collection, you could make the main collection non serializable, then add another filtered collection that does serialize.
public class Manager
{
[JsonIgnore]
public Employee[] Employees { get; set; }
[JsonProperty("Employees")]
public Employee[] SerializableEmployees
{
get { return Employees.Where(e => e.Name != "Bob").ToArray(); }
set { Employees = value; }
}
}
Alternatively, you could mark your class with the [JsonConverter] attribute and use a custom converter to check your condition. A similar approach that ignores a class entirely is detailed here.
language specific (however, if you need a language please use C++/C# or Javascript). I trying to figure out how I would go about doing this, and how I would access the child objects from a parent object.
Say I have the following classes, and these are not written properly etc... :
Class: roomContainer (container of objects)
Class: Table (base class for a table, contains property of maximum-seats, current-number-of-seats, array of seats )
Class: Desk (extends Table, contains property for maximum draws, array of draws )
Class: seat (base class for seats, contains property of maximum-legs, arm-rest, back-rest)
Class: couch (extends seat, adds property maximum seats)
If I create an instance of roomContainer, and add within it's container a table, couch. Within the table I create multiple seats (or chairs), and a desk.
How would I be able to access the property of child objects property, when the parent has a container of different objects. I.e the roomContainer Container of objects, one of them is a table, and a desk - where the desk has different properties and an array of draws etc.. ?
You're looking for something called the Composite Design Pattern. This allows you to nest objects (as you described), and hold references to both the parent and the children (though some implementations do no maintain a parent reference - this is optional).
Here is an example implementation using your schema:
public static class Program // the supporting class definitions are below
{
public static void Main()
{
// create a root container
var room = new RoomContainer();
// create a child
var table = new Table(room, 4);
// put the table in the room
room.Add(table);
MakeMess(room);
}
// to show you how to access the properties
// if you don't already have a reference:
public static void MakeMess(RoomContainer room)
{
if(room == null)
{
throw new ArgumentNullException("room");
}
var seats = room.GetChildren<Table>().First().Seats.ToArray();
for (int index = 0; index < seats.Length; index++)
{
Console.WriteLine("You have kicked over Seat #{0}",(index+1).ToString());
}
}
}
// This is the base class of the components and provides the core functionality.
// You will want to make this object's interface minimal, so that the logic
// is consistent with all its children (without knowing what they might be in advance)
public abstract class Component
{
private readonly IList<Component> _children;
private readonly Component _container;
protected Component(Component container)
{
_container = container;
_children = new Component[] { };
}
public bool IsRoot { get { return _container == null; } }
public abstract bool IsContainer { get; }
public virtual void Add(Component component)
{
if (component == null)
{
throw new ArgumentNullException("component");
}
if (!IsContainer)
{
throw new NotSupportedException("Add is not supported by leaf components");
}
_children.Add(component);
}
public IEnumerable<T> GetChildren<T>()
where T: Component
{
if (!IsContainer)
{
throw new NotSupportedException("Only containers have children");
}
return _children.OfType<T>();
}
public IEnumerable<Component> Children
{
get
{
if (!IsContainer)
{
throw new NotSupportedException("Only containers have children");
}
return _children;
}
}
}
public class RoomContainer : Component
{
public RoomContainer() : base(null)
{
}
public override bool IsContainer { get { return true; } }
}
public class Table : Component
{
private readonly int _maximumSeatCount;
public Table(Component container, int maximumSeatCount) : base(container)
{
_maximumSeatCount = maximumSeatCount;
}
public override bool IsContainer { get { return true; } }
protected virtual bool CanAdd(Component component)
{
return component is Seat && MaximumSeatCount > CurrentSeatCount;
}
public override void Add(Component component){
if(CanAdd(component)){
base.Add(component);
}
else
{
throw new NotSupportedException("The component was an invalid child of Table and could not be added.");
}
}
public int MaximumSeatCount { get { return _maximumSeatCount; } }
public int CurrentSeatCount { get { return Seats.Count(); } }
public IEnumerable<Seat> Seats { get { return Children.OfType<Seat>(); } }
}
public class Seat : Component
{
// you can restrict the constructor to only accept a valid parent
public Seat(Table table) : base(table)
{
}
public override bool IsContainer
{
get { return false; }
}
}
If the all share common methods, for example Render(), Update(), SaveDetails(int Id), LoadDetails(int Id) then you could make them all inherit from a base class, or all impliment a common interface. This would remove the need for casting (below) when calling a common method (or accessing a common property).
To access properties unique to the derived class you would check the type of the child object, then cast the child object to access the property.
EDIT: Example:
foreach(Object obj in Room.ChildObjects)
{
if(obj is Desk)
{
Desk DeskObj = obj as Desk; // Cast the object reference as a desk.
DeskObj.MaxDraws = 50; // It's a big desk!
DestObj.Draws[1] = new Draw(); // ......
}
}
Something like this:
IEnumerable<Desk> desks = roomContainer.OfType<Desk>();
//Iterate and do stuff.
IEnumerable<Table> tables = roomContainer.OfType<Table>();
//Iterate and do stuff.