I have a class called 'Article' in a project called 'MyProject.Data', which acts as the data layer for my web application.
我在一个名为“MyProject”的项目中有一个名为“Article”的课程。“Data”,它充当我的web应用程序的数据层。
I have a separate project called 'MyProject.Admin', which is a web-based admin system for viewing/editing the data, and was build using ASP.NET Dynamic Data.
我有一个单独的项目叫做“我的项目”。Admin'是一个基于web的用于查看/编辑数据的管理系统,是使用ASP构建的。网络动态数据。
Basically I want to extend the Article class, using a partial class, so that I can augment one of its properties with a "UIHint" extender, which will allow me to replace the normal multi-line textbox with an FCKEdit control.
基本上,我想要扩展Article类,使用一个局部类,这样我就可以用一个“UIHint”扩展器来扩充它的一个属性,它将允许我用FCKEdit控件替换普通的多行文本框。
My partial class and extender would look like this:
我的部分类和扩展器是这样的:
[MetadataType(typeof(ProjectMetaData))]
public partial class Project
{
}
public class ProjectMetaData
{
[UIHint("FCKeditor")]
public object ItemDetails { get; set; }
}
Now this all works fine if the partial class is in the same project as the original partial class - i.e. the MyProject.Data project.
如果部分类与原始的部分类(即MyProject)位于同一个项目中,那么这一切都没问题。数据项目。
But UI behavior shouldn't sit in the Data layer, but rather, in the Admin layer. So I want to move this class to MyProject.Admin.
但是UI行为不应该放在数据层中,而应该放在管理层中。我想把这个类移到myproject。admin。
However, if I do that, the functionality is lost.
但是,如果我这样做,功能就会丢失。
My fundamental question is: can I have 2 partial classes in separate projects, but both referring to the same "class"?
我的基本问题是:我是否可以在单独的项目中有两个部分类,但都指向相同的“类”?
If not, is there a way to accomplish what I'm trying to do, without mixing data-layer logic with UI logic?
如果没有,是否有一种方法可以在不将数据层逻辑与UI逻辑混合的情况下完成我正在尝试的工作?
8 个解决方案
#1
161
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.
不,不能在两个不同的程序集(项目)中有两个部分类引用相同的类。一旦编译完程序集,元数据就会被嵌入,你的类就不再是局部的了。部分类允许您将相同类的定义分割为两个文件。
#2
12
As noted, partial classes is a compile-time phenomenon, not runtime. Classes in assemblies are by definition complete.
如前所述,部分类是编译时现象,而不是运行时现象。程序集中的类根据定义是完整的。
In MVC terms, you want to keep view code separate from model code, yet enable certain kinds of UI based on model properties. Check out Martin Fowler's excellent overview of the different flavours of MVC, MVP and whatnot: you'll find design ideas aplenty. I suppose you could also use Dependency Injection to tell the UI what kind of controls are viable for individual entities and attributes.
在MVC术语中,您希望将视图代码与模型代码分离开来,同时根据模型属性启用特定类型的UI。看看Martin Fowler对不同风格的MVC, MVP等等的精彩概述:你会发现很多设计想法。我想您也可以使用依赖注入来告诉UI,什么样的控件对于个体实体和属性是可行的。
Your aim of separating concerns is great; but partial classes were intended to address entirely different issues (primarily with code generation and design-time modelling languages).
你分离关注点的目标是伟大的;但是部分类的目的是解决完全不同的问题(主要是代码生成和设计时建模语言)。
#3
5
Extension methods and ViewModels are the standard way to extend data-layer objects in the frontend like this:
扩展方法和视图模型是扩展前端数据层对象的标准方法:
Data Layer (class library, Person.cs):
数据层(类库,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:
显示层(web应用程序)
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):
ViewModel(用于扩展视图特定数据):
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:
控制器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:
看来,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)
#4
2
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.
将基本文件作为链接文件添加到项目中。它仍然是部分的,但是允许您在两个项目之间共享它,使它们保持同步,同时在部分类中有版本/框架特定的代码。
#5
1
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.
我也遇到过类似的问题。我在我的数据项目中保留了我的部分类,所以在你的例子中是“MyProject.Data”。MetaDataClasses不应该进入您的管理项目,因为您将创建一个循环引用。
I added a new Class Lib project for my MetaDataClasses e.g. 'MyProject.MetaData' and then referenced this from my Data project
我为我的元数据添加了一个新的类Lib项目。”。。然后从我的数据项目中引用它
#6
1
Perhaps use a static extension class.
可能使用静态扩展类。
#7
0
I may be mistaken here, but could you not simply define the ProjectMetaData class in your MyProject.Admin project?
这里我可能弄错了,但是您是否可以在MyProject中简单地定义ProjectMetaData类。管理项目吗?
#8
0
Just add class file as link in your new project and keep the same namespace in your partial class.
只需在新项目中添加类文件作为链接,并在部分类中保留相同的名称空间。
#1
161
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.
不,不能在两个不同的程序集(项目)中有两个部分类引用相同的类。一旦编译完程序集,元数据就会被嵌入,你的类就不再是局部的了。部分类允许您将相同类的定义分割为两个文件。
#2
12
As noted, partial classes is a compile-time phenomenon, not runtime. Classes in assemblies are by definition complete.
如前所述,部分类是编译时现象,而不是运行时现象。程序集中的类根据定义是完整的。
In MVC terms, you want to keep view code separate from model code, yet enable certain kinds of UI based on model properties. Check out Martin Fowler's excellent overview of the different flavours of MVC, MVP and whatnot: you'll find design ideas aplenty. I suppose you could also use Dependency Injection to tell the UI what kind of controls are viable for individual entities and attributes.
在MVC术语中,您希望将视图代码与模型代码分离开来,同时根据模型属性启用特定类型的UI。看看Martin Fowler对不同风格的MVC, MVP等等的精彩概述:你会发现很多设计想法。我想您也可以使用依赖注入来告诉UI,什么样的控件对于个体实体和属性是可行的。
Your aim of separating concerns is great; but partial classes were intended to address entirely different issues (primarily with code generation and design-time modelling languages).
你分离关注点的目标是伟大的;但是部分类的目的是解决完全不同的问题(主要是代码生成和设计时建模语言)。
#3
5
Extension methods and ViewModels are the standard way to extend data-layer objects in the frontend like this:
扩展方法和视图模型是扩展前端数据层对象的标准方法:
Data Layer (class library, Person.cs):
数据层(类库,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:
显示层(web应用程序)
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):
ViewModel(用于扩展视图特定数据):
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:
控制器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:
看来,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)
#4
2
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.
将基本文件作为链接文件添加到项目中。它仍然是部分的,但是允许您在两个项目之间共享它,使它们保持同步,同时在部分类中有版本/框架特定的代码。
#5
1
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.
我也遇到过类似的问题。我在我的数据项目中保留了我的部分类,所以在你的例子中是“MyProject.Data”。MetaDataClasses不应该进入您的管理项目,因为您将创建一个循环引用。
I added a new Class Lib project for my MetaDataClasses e.g. 'MyProject.MetaData' and then referenced this from my Data project
我为我的元数据添加了一个新的类Lib项目。”。。然后从我的数据项目中引用它
#6
1
Perhaps use a static extension class.
可能使用静态扩展类。
#7
0
I may be mistaken here, but could you not simply define the ProjectMetaData class in your MyProject.Admin project?
这里我可能弄错了,但是您是否可以在MyProject中简单地定义ProjectMetaData类。管理项目吗?
#8
0
Just add class file as link in your new project and keep the same namespace in your partial class.
只需在新项目中添加类文件作为链接,并在部分类中保留相同的名称空间。