Programing

다른 어셈블리에서 두 개의 부분 클래스를 사용하여 동일한 클래스를 나타낼 수 있습니까?

lottogame 2020. 7. 10. 08:16
반응형

다른 어셈블리에서 두 개의 부분 클래스를 사용하여 동일한 클래스를 나타낼 수 있습니까?


내 웹 응용 프로그램의 데이터 계층 역할을하는 'MyProject.Data'라는 프로젝트에 'Article'이라는 클래스가 있습니다.

데이터를 보거나 편집하는 웹 기반 관리 시스템 인 'MyProject.Admin'이라는 별도의 프로젝트가 있으며 ASP.NET Dynamic Data를 사용하여 빌드되었습니다.

기본적으로 부분 클래스를 사용하여 Article 클래스를 확장하여 "UIHint"익스텐더를 사용하여 해당 속성 중 하나를 확장하여 일반 여러 줄 텍스트 상자를 FCKEdit 컨트롤로 바꿀 수 있습니다.

내 부분 클래스와 익스텐더는 다음과 같습니다.

[MetadataType(typeof(ProjectMetaData))]
public partial class Project
{
}

public class ProjectMetaData
{
    [UIHint("FCKeditor")]
    public object ItemDetails { get; set; }
}

이제 부분 클래스가 원래 부분 클래스와 같은 프로젝트 (예 : MyProject.Data 프로젝트)에 있으면이 모든 것이 잘 작동합니다.

그러나 UI 동작은 데이터 계층이 아니라 관리 계층에 있어야합니다. 이 클래스를 MyProject.Admin으로 옮기고 싶습니다.

그러나 그렇게하면 기능이 손실됩니다.

내 근본적인 질문은 : 별도의 프로젝트에서 두 개의 부분 클래스를 가질 수 있지만 둘 다 동일한 "클래스"를 참조 할 수 있습니까?

그렇지 않은 경우 데이터 계층 논리와 UI 논리를 혼합하지 않고 내가하려는 일을 수행 할 수있는 방법이 있습니까?


아니요, 두 개의 다른 어셈블리 (프로젝트)에서 동일한 클래스를 참조하는 두 개의 부분 클래스를 가질 수 없습니다. 어셈블리가 컴파일되면 메타 데이터가 구워지고 클래스가 더 이상 부분적이지 않습니다. 부분 클래스를 사용하면 동일한 클래스의 정의를 두 개의 파일로 분할 할 수 있습니다.


언급 한 바와 같이 부분 클래스는 컴파일 타임 현상이며 런타임이 아닙니다. 어셈블리의 클래스는 정의상 완전합니다.

MVC 용어에서는 뷰 코드를 모델 코드와 별도로 유지하면서 모델 속성을 기반으로 특정 종류의 UI를 사용하려고합니다. MVC, MVP 및 다양한 기능에 대한 Martin Fowler의 뛰어난 개요확인하십시오 . 디자인 아이디어가 풍부하다는 것을 알 수 있습니다. Dependency Injection사용 하여 UI에 개별 엔터티 및 특성에 대해 어떤 종류의 컨트롤을 사용할 수 있는지 알려줄 수 있다고 가정 합니다.

우려를 분리하려는 당신의 목표는 위대합니다. 그러나 부분 클래스는 완전히 다른 문제 (주로 코드 생성 및 디자인 타임 모델링 언어)를 해결하기위한 것입니다.


확장 메소드 및 ViewModel은 다음과 같이 프론트 엔드에서 데이터 계층 오브젝트를 확장하는 표준 방법입니다.

데이터 계층 (클래스 라이브러리, Person.cs) :

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

표시 계층 (웹 응용 프로그램) 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)

Add the base file as a linked file into your projects. It's still partial but as allows you to share it between both projects, keep them synchronized and at the same time have version/framework specific code in the partial classes.


I've had similar issues with this. I kept my partial classes in my Data project so in your case the 'MyProject.Data'. MetaDataClasses shouldn't go in your Admin project as you will create a circular references other wise.

I added a new Class Lib project for my MetaDataClasses e.g. 'MyProject.MetaData' and then referenced this from my Data project


Perhaps use a static extension class.


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


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

참고URL : https://stackoverflow.com/questions/647385/is-it-possible-to-have-two-partial-classes-in-different-assemblies-represent-the

반응형