简体   繁体   中英

C# Type Conversion / Linq

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?

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

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
{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM