C# Type Conversion / Linq - c#

We currently have several underlying database tables such as Events, Shop Products, Content Pages etc. each with have shared properties such as having a Name, a details page on the front end of the site, a thumbnail url, an active flag etc.
I'm trying to figure out the most efficient way of creating a class of shared properties that can be used to pass around these objects generically. An example might be the search results page. The search can be done against the name of the collection of data which is actually across multiple tables originally.
I am struggling using inheritance due to all these classes originating from LINQ classes and I don't want to start editing the datacontext designer to suit my needs.
Currently each partial class on my LINQ classes contains a SharedObject method:
public partial class Event
{
public SharedObject SharedObject
{
get
{
return new SharedObject(this);
}
}
...
This is repeated for Events, Shop Products etc. The Shared Object class contains the following:
public class SharedObject
{
public string Reference { get; set; }
public string Name { get; set; }
public string ImageURL { get; set; }
public bool IsVisible { get; set; }
public bool IsAdminVisible { get; set; }
public string FrontEndDetailsURL { get; set; }
public string AdminDetailsURL { get; set; }
public object OriginalObject { get; set; }
public string ObjectDescription { get; set; }
public SharedObject(object originalObject)
{
if (originalObject.GetType() == typeof(Event))
{
Event eventx = (Event)originalObject;
Reference = eventx.Reference;
Name = eventx.Name;
ImageURL = eventx.ImageURL;
IsVisible = eventx.IsActive && !Event.IsArchived;
IsAdminVisible = !eventx.IsArchived;
FrontEndDetailsURL = eventx.DetailsURL;
AdminDetailsURL = eventx.AdminDetailsURL;
OriginalObject = originalObject;
ObjectDescription = "Event";
}
....
Does this sound like a suitable solution?

Consider using an interface. This is much more flexible.
public interface ISharedObject
{
string Reference { get; set; }
string Name { get; set; }
string ImageURL { get; set; }
bool IsVisible { get; set; }
bool IsAdminVisible { get; set; }
string FrontEndDetailsURL { get; set; }
string AdminDetailsURL { get; set; }
object OriginalObject { get; set; }
string ObjectDescription { get; set; }
}
public partial class Event : ISharedObject
{}

I think the most efficient way to pass around your shared objects is to use the "Chain of responsibility pattern" Chain of responsibility
For the inheritance LINQ you think of the use of IQueryable <T>. I hope that it can help you

Related

EF Core - Create report table from multiple tables

I have a report that I need to send to my react frontend that needs to be easily queried and searched. The problem is with the current method we need to pull all the entire database before performing a query due to nested objects and other factors.
To significantly speed up the process I want to create a Report Table/View to query from that stays up to date as the other tables change.
Here is a small example of the models:
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int TypeId { get; set; }
public ItemType Type { get; set; }
public int OrderId { get; set; }
public Order Order { get; set; }
}
public class ItemType
{
public int Id { get; set; }
public string Name { get; set; }
public List<Item> Items { get; set; }
}
public class Order
{
public int Id { get; set; }
public Customer Customer { get; set; }
public List<Item> Items { get; set; }
}
public class ItemReport
{
public string Name { get; set; }
public string Description { get; set; }
public string Type { get; set; }
public string Customer { get; set; }
public ItemReport(Item item)
{
Name = item.Name;
Description = item.Description;
Type = item.Type.Name;
Customer = item.Order.Customer.Name;
}
}
ItemReport is the model I use to send to the frontend.
I've read a fair amount on Keyless Entities and Views, but am in need of a little guidance on putting all the pieces together.
Currently we would pull all Items and required fields like Name from customer and turn it into an IEnumerable list of ItemReport to then be filtered/sorted or searched.
As a side note, there may be other solutions than the on I'm posting for that I would be open to as well.
I've looked into this quite a bit, but I don't think I'm finding the right solutions as an example when reading on Views they mention how they cannot be inserted into or updated with EF Core.
It is simple projection. Passing item in Constructor is not right way, because EF Core cannot look into compiled method body.
var query = context.Items
.Select(item => new ItemReport
{
Name = item.Name;
Description = item.Description;
Type = item.Type.Name;
Customer = item.Order.Customer.Name;
});

Making a class that will contain a subclass that can be different classes depending on the need

I'm unsure how to go about implementing this particular idea of I have a class lets call it EnhancedUserInput that will have some variables that all of the input types will have and a particular subclass depending on the need during operation so some extra variables and a list so for example sub classes of it would be MultipleChoice which would have MinSelection, MaxSelection and a list of a type called option with their own variables ect and then another possible sub class called ExplicitAgreement which would have the variables inputLabel1, inputLabel2 and a list of type BinaryInput which would have their own variables.
So far from what I understand the best way going about this would be to have some type of generic variable? I'll show some code to try and help get what it is I need across but was just wondering is there an easy way of doing this that I am unaware of?
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set}
// this is where I am unsure of how to go about it
public object inputType
{
MultipleChoice
ExplicitAgreement
}
}
public class MultipleChoice
{
public List<MultipleChoiceOption> Options { get; set; }
public int MinSelected { get; set; }
public int MaxSelected { get; set; }
}
public class ExplicitAgreement
{
public List<BinaryInputOption> Buttons { get; set; }
public string InputLabel1 { get; set; }
public string InputLabel2 { get; set; }
}
what would be the best path for this solution I can think of some possible ways but they would be a bit figity and was wondering if there are any simple ways?
Seems to me that you may have this the wrong way around. Maybe what you want is to just use class inheritance?
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set}
}
public class MultipleChoice : EnhancedCustomerInput
{
public List<MultipleChoiceOption> Options { get; set; }
public int MinSelected { get; set; }
public int MaxSelected { get; set; }
}
public class ExplicitAgreement : EnhancedCustomerInput
{
public List<BinaryInputOption> Buttons { get; set; }
public string InputLabel1 { get; set; }
public string InputLabel2 { get; set; }
}
Steve Harris's inheritance suggestion is good. Your original option to use Composition can work just fine, too:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public object InputData { get; set; }
}
The only problem is that consumers of your code need to know that InputData can be one of several different types, and you presumably need logic to switch on their type. You can add comments to the property to give people a hint, or you can use a library like LanguageExt, which provides an Either type:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public Either<MultipleChoice, ExplicitAgreement> InputData { get; set; }
}
This makes it much more obvious which types InputData can be, but would get very unwieldy if you have more than two possibilities.
You could also declare an interface that InputData must implement, which would make it easier for developers to find all the types that are intended to be used there. But an empty interface is considered to be a code smell because it indicates you're using interfaces for something they weren't really intended for.
Another option I've found to work well is to define an enum type to help identify which different types of input data you can have:
public class EnhancedCustomerInput
{
public string Title { get; set;}
public bool ResponseOptional { get; set;}
public string CancelLabel { get; set;}
public string SubmitLabel { get; set; }
public InputType InputType { get; set; }
public object InputData { get; set; }
}
public enum InputType { MultipleChoice, ExplicitAgreement }
This gives your business logic a specific set of possible types that you can switch your logic on, and works particularly well when the class is going to be serialized and deserialized, because then you can tell the deserializer which specific type of object to deserialize InputData to.
There are lots of options, each with their advantages and disadvantages.

Access Properties from a class into another class in Xamarin Forms

I would like to access the email of this class into another class that I am already instantiated on my render page.
Any help very much appreciated.
public class Access
{
public string Email { get; set; } //I want this value...
}
public class Types
{
public string Id { get; set; }
public string Sum { get; set; }
public string Addition { get; set; }
public string Email { get; set; } // ...to be this value inside my class TYPES
}
Well I think you can simply use a copy constructor for the Types class with the Access instance as parameter from which you want to copy the email value when creating a new Types instance.
From what you said it seems you don't care if its a particular instance (as your post is a bit confusing between class and instance)
public class Types
{
public string Id { get; set; }
public string Sum { get; set; }
public string Addition { get; set; }
public string Email { get; set; }
public Types(Access access)
{
Email = access.Email
}
}

Creating Objects where multiple objects have the same properties

My program is starting to get pretty big. and i have found that its starting to do the same thing in multiple area's.
Im trying to figure out how i can make it more efficient.
So i have an object that looks like this
public class TreeViewNode
{
public TreeViewNode()
{
Children = new ObservableCollection<TreeViewNode>();
}
public String Name { get; set; }
public string Description { get; set; }
public ObservableCollection<TreeViewNode> Children { get; set; }
}
i also have another object that looks like this;
public class ComputerObject
{
public String Name { get; set; }
public Int32 UUID { get; set; }
public DateTime Created { get; set; }
public ObservableCollection<Object> Children { get; set; }
}
Both these items need to have some of the same properties..
at the moment they both have the Children Property and the Name Property. but they both need to have some other common properties added to them.
so i have tried something like this.
public class BaseObject
{
public String Name { get; set; }
public ObservableCollection<Object> Children { get; set; }
public string Description { get; set; }
public BaseObject()
{
Children = new ObservableCollection<object>();
}
}
public class ComputerObject: BaseObject
{
public Int32 UUID { get; set; }
public DateTime Created { get; set; }
}
public class TreeViewNode: BaseObject
{
public String IconPath { get; set; }
}
Now this is just a cut down version of what i am implementing, i have alot of objects that share the same properties. and some that dont and mix and match. and i cannot figure out the best implimentation for this.
My Objects are becoming very cluttered, and when i rename a property i find that i have to rename it in several area's and this isnt the way its ment to be.
can someone please advise how i would implement multiple objects that share the same property names?
In my opinion you should not let classes inherit from one baseclass when these childclasses are not related to each other (like #Sriram Sakthivel asked Animal < Dog,Cat) just to share the same properties.
You should determine which classes are related (cat, dog are animals; car, motorcycle are vehicles) and then create baseclasses based on these "groupings".
I would look into decorator pattern. In short, you dont share common properties via inheritance. You make classes that contain common properties, and use these classes as properties in your end classes.
EDIT: Example is actually just a standard composition, it should work nevertheless
E.G.
public class Decorator1
{
public String Name { get; set; }
public ObservableCollection<Object> Children { get; set; }
public string Description { get; set; }
}
public class Decorator2
{
public long Id { get; set; }
}
public class ClassA
{
public Decorator1 TreeNodeImpl;
}
public class ClassB
{
public Decorator1 TreeNodeImpl;
public Decorator2 LongIdImpl;
}

Nested ViewModel Classes in asp.net MVC

I have four MVC model layer domain classes.
namespace MvcMobile.Models.BusinessObject
{
public class Speaker
{
public int SpeakerID { get; set; }
public string SpeakerName { get; set; }
}
public class Tag
{
public int TagID { get; set; }
public string TagName { get; set; }
}
public class Seminar
{
public string Seminar_Code { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Room { get; set; }
}
public class Seminar_Detail
{
public string Seminar_Code { get; set; }
public int SpeakerID { get; set; }
public int TagID { get; set; }
public string DateAndTime { get; set; }
}
}
I would like to make CRUD operation by using these classes. So I create two VeiwModel Classes.
namespace MvcMobile.ViewModel
{
public class Seminar_Root_ViewModel
{
public Seminar_Subsidiary_ViewModel Seminars { get; set; }
public List<Speaker> Speakers { get; set; }
public List<Tag> Tags { get; set; }
}
public class Seminar_Subsidiary_ViewModel
{
public Seminar Seminar { get; set; }
public List<Seminar_Detail> Seminar_Detail { get; set; }
}
}
For Controller layer, I consider that I will use Seminar_Root_ViewModel to make the whole CRUD operation processes.
What I would like to ask is that Is this proper way or correct way?
If you have more elegant way to make model layer and ViewModel layer, Please let me get suggestion.
Every suggestion will be appreciated.
[updated]
Let's assume that I make master-Detail form design.
Speaker and Tag are just look-up tables for dropdownlist or some controls like that.
Seminar is Master Data and Seminar_Detail will be Item Grid Data.
So As for this scenario, all of this classes are needed for this program.
Please let me know if my thinking is wrong.
The only thing I can see is if you are not going to re-use your Seminar_Subsidiary_ViewModel view model you could skip it.
If you are going to need those two properties Seminar and Seminar_Detail on another view or ajax call, it's perfectly fine to have that kind of separation.
Personally I'm not a huge fan of _ on class name, but that have nothing to do with the question.

Categories

Resources