Is it possible to have two partial classes in different assemblies represent the same class?

前端 未结 9 1426
栀梦
栀梦 2020-11-27 03:56

I have a class called \'Article\' in a project called \'MyProject.Data\', which acts as the data layer for my web application.

I have a separate project called \'MyP

相关标签:
9条回答
  • 2020-11-27 04:21

    Extension methods and ViewModels are the standard way to extend data-layer objects in the frontend like this:

    Data Layer (class library, Person.cs):

    namespace MyProject.Data.BusinessObjects
    {
      public class Person
      {
        public string Name {get; set;}
        public string Surname {get; set;}
        public string Details {get; set;}
      }
    }
    

    Display Layer (web application) PersonExtensions.cs:

    using Data.BusinessObjects
    namespace MyProject.Admin.Extensions
    {
      public static class PersonExtensions
      {
        public static HtmlString GetFormattedName(this Person person)
        {
           return new HtmlString(person.Name + " <b>" + person.Surname</b>);
        }
      }
    }
    

    ViewModel (for extended view-specific data):

    using Data.BusinessObjects
    namespace MyProject.Admin.ViewModels
    {
      public static class PersonViewModel
      {
        public Person Data {get; set;}
        public Dictionary<string,string> MetaData {get; set;}
    
        [UIHint("FCKeditor")]
        public object PersonDetails { get { return Data.Details; } set {Data.Details = value;} }
      }
    }
    

    Controller PersonController.cs:

    public ActionMethod Person(int id)
    {
      var model = new PersonViewModel();
      model.Data = MyDataProvider.GetPersonById(id);
      model.MetaData = MyDataProvider.GetPersonMetaData(id);
    
      return View(model);
    }
    

    View, Person.cshtml:

    @using MyProject.Admin.Extensions
    
    <h1>@Model.Data.GetFormattedName()</h1>
    <img src="~/Images/People/image_@(Model.MetaData["image"]).png" >
    <ul>
      <li>@Model.MetaData["comments"]</li>
      <li>@Model.MetaData["employer_comments"]</li>
    </ul>
    @Html.EditorFor(m => m.PersonDetails)
    
    0 讨论(0)
  • 2020-11-27 04:25

    Just add class file as link in your new project and keep the same namespace in your partial class.

    0 讨论(0)
  • 2020-11-27 04:27

    No, you cannot have two partial classes referring to the same class in two different assemblies (projects). Once the assembly is compiled, the meta-data is baked in, and your classes are no longer partial. Partial classes allows you to split the definition of the same class into two files.

    0 讨论(0)
  • 2020-11-27 04:29

    I may be mistaken here, but could you not simply define the ProjectMetaData class in your MyProject.Admin project?

    0 讨论(0)
  • 2020-11-27 04:30

    Perhaps use a static extension class.

    0 讨论(0)
  • 2020-11-27 04:30

    Since 2019 you can have 2 parts of a partial class in different assemblies using a trick. This trick is explained and demonstrated in this article:

    https://www.notion.so/vapolia/Secret-feature-Xamarin-Forms-control-s-auto-registration-1fd6f1b0d98d4aabb2defa0eb14961fa

    It uses at its core the MSBuild.Sdk.Extras extension to SDK like projects, which solves the limitation of having all partial parts of a class in the same assembly, by using one project with multiple simultaneous targets, effectively creating multiples assemblies in one compilation of the same project.

    0 讨论(0)
提交回复
热议问题