我有一些问题想出一个好的结构来构建我的类和对象.在下面的代码中,我使用一个接口来定义我的类方法,但我也想将数据库连接传递给我的构造函数,以便类具有此连接.这是正确的,因为我在下面编码它的构造函数放在我的类和我的界面中的方法?
interface IDataItem
{
    public function saveItem(Item $theItem);
}
class DataItem implements IDataItem
{
    public function __construct(Database $database) 
    { 
        $this->database = $database;
    }
    public function saveItem(Item $item) 
    {       
        //save the item
    }
}
$db = new Database(); //from a database class
$dataItem = new DataItem($db);          
$dataItem->saveItem($anItem);
我对如何使用MVC执行插入和更新语句感到有点困惑.是否可以在控制器中创建对象的实例并将其传递给您的服务以保存它,或者您是否将数据传递给您的服务并处理其中的所有其他内容?
插入
在我的控制器中,例如:
$userservice->insert("myname","mypassword");
在我的UserService中:
function insert($username,$password){
      $user = ORM::for_table('user')->create();
      $user->username= $username;
      $user->password= $password;
      $user->save();
}
更新
在我的控制器中,例如:
$userservice->update("myname","mypassword",1);
在我的UserService中:
function insert($username,$password,$id){
      $user = ORM::for_table('user')->find($id);
      $user->username= $username;
      $user->password= $password;
      $user->save();
}
这是好习惯吗?因为我看到很多这些答案,例如在控制器中创建用户并将其传递到存储库以保存它: PHP中的正确存储库模式设计? 但我不喜欢在控制器中创建用户的想法......
我第一次使用OO创建了一个网站,我正在努力寻找正确的方法.我不喜欢在一个类中完成所有操作的类,因此我将实体和数据库类分开.例如:http://w3cyberlearning.com/php/mysql_oop_product_class.php 我发现这个类不那么合乎逻辑. - 为什么产品应该包含数据库实例 - $ product-> get_products()我认为根本不符合逻辑.
所以我根据以下结构创建了一个单独的类:
Table: Products
Fields: productID,productName,categoryID
Table: ProductCategory
Fields: categoryID,category
Classes:
Product
-productID
-productName
-category (object)
DataProduct
-insertProduct($product)
    insert query...
-deleteProduct($product)
    delete query...
-getProduct($id)
    select data from table ... (join between product and category to get all the fields)
    $category = new Category($categoryID,$categoryname)
    $product = new Product($productID,$productName);
    $product->setCategory($category);
    return $product;
ProductCategory
-categoryID
-category
DataProductCategory
...
但现在我遇到了一些问题,我正在处理它.
举例来说,删除一个产品,我可以执行两个不同的脚本,我应该使用哪一个?
http://www.mywebsite.com/books/1/
$id = 1 (1 retrieved from the url)
$DataProduct = new …我正在尝试使用线程在画布上绘图来创建一个简单的游戏引擎,但我有一些奇怪的问题,我无法解释.这个"游戏"的目的是在画布上每秒画一个圆圈.这工作,但不是我想要的工作方式,似乎应用程序在两个画布之间进行切换,并添加了一圈每个画布等你拿每秒具有相同数量的圈,但在不同的放置两个画布之间的切换在画布上.
我不知道我做错了什么,但我不熟悉Treadding,它与我的Android设备有多少核心或类似的东西有关?
我的代码如下所示,所以我只使用一个launchthread,它使用一个链接到animationthread的layoutfile,它启动一个线程并在画布上每秒画一个圆圈.(你可以忽略touchevent,它还没有使用).
该项目存在于主要的启动线程之外:
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
它使用这个布局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">     
        <com.androidtesting.AnimationView
            android:id="@+id/aview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
</FrameLayout>
我的Surfaceview类有一个内部Thread类:
class AnimationView extends SurfaceView implements SurfaceHolder.Callback {
    private boolean touched = false;
    private float touched_x, touched_y = 0;
    private Paint paint;
    private Canvas c;
    private Random random;
    private AnimationThread thread;
    public AnimationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
        thread = new …我有一个应用程序通过数据层获取数据到表单,使用这样的东西:
public DataTable get(String query, ArrayList parameters = null)
{            
   using (iDB2Connection cn =  new iDB2Connection(ConfigurationManager.ConnectionStrings["iseries"].ToString()))
   {
       // get the data and return them    
   }
}
我有获取数据的表单,这很好.
但是,我创建了一个UserControl,它通过这个方法获取数据,当我运行我的项目时工作正常,但是,包含UserControl的表单会引发设计器异常.
"为了防止在加载设计器之前可能丢失数据,必须解决以下错误:"
我发现错误位于从中检索连接字符串<appSettings>.
它会抛出nullpointerexception.
但仅限于设计模式.当我忽略它时,一切正常,但是,我想知道如何解决这个问题.
<appSettings>通过我的UserControl访问它时为什么我的null?
更新1
似乎我的UserControl根本无法识别<appSettings>.
当我将此代码放入UserControl Load事件时,我也得到了一个null引用.
private void SelectUser_Load(object sender, EventArgs e)
{            
   txtLocation.Text = ConfigurationManager.AppSettings["location"].ToString();
}
我一直在研究和阅读很多关于在PHP中使用单独的层来创建可维护和可读的代码.但是,我看到很多代码将实体和数据库访问放在一个类中.例如:
class User{
     public $id;
     public $username;
     public $database;
     function add(){
           $database->query ....
     }
}
我觉得这很奇怪,因为在这里你将User类与数据库元素混合在一起,这使得维护起来更加困难.
我喜欢这样工作:
这样工作如下:
$database = new Database();
$database->openConnection();
$dataUser = new DataUser($db);
$user = new User(1,"myname");
$dataUser->saveUser($user);
所以我想知道,我是以正确的方式工作还是第一种创建代码的更好方法?我发现可能很容易维护,因为你有一个单独的实体和一个单独的数据库类来处理数据库操作.
我突然在我的网站上发现了一些奇怪的代码:
<script type="text/javascript">var NREUMQ=NREUMQ||[];NREUMQ.push(["mark","firstbyte",new Date().getTime()]);</script>
但我从来没有在我的代码中实现这一点,突然间它就在那里.
有什么办法在某种程度上被黑客入侵我的网站?或者是什么导致这个奇怪的代码?
更新:
我刚删除了所有代码,并添加了一个包含以下内容的index.php文件:
<html>
<head>
</head>
<body>
test
</body>
</html>
当我查看源代码时,会显示以下代码:
<html>
<head>
<script type="text/javascript">var NREUMQ=NREUMQ||[];NREUMQ.push(["mark","firstbyte",new Date().getTime()]);</script>
</head>
<body>
test
<script type="text/javascript">if(!NREUMQ.f){NREUMQ.f=function(){NREUMQ.push(["load",new Date().getTime()]);var e=document.createElement("script");e.type="text/javascript";e.src=(("http:"===document.location.protocol)?"http:":"https:")+"//"+"d1ros97qkrwjf5.cloudfront.net/42/eum/rum.js";document.body.appendChild(e);if(NREUMQ.a)NREUMQ.a();};NREUMQ.a=window.onload;window.onload=NREUMQ.f;};NREUMQ.push(["nrfj","beacon-3.newrelic.com","0320653fc3","2194086","YAFRYxcHXUYCBUdQWVlLZkUMSVpbBwNLF0ZfFA==",0,1,new Date().getTime(),"","","","",""]);</script>
</body>
</html>
更新2:
到目前为止我尝试了什么:
我想做的:
在android studio中创建三个android项目
1)公共项目或库项目(这是一个独立的项目)
2)使用库项目的项目
1 3)使用库项目的项目2
问题是什么?
在 eclipse 中很容易实现,但在 android studio 中则不行。我无法在 android studio 中的单个窗口中打开三个项目。我尝试使用模块来实现此目的,但它不符合我的要求,它只是在现有项目中添加一个模块,并且存在循环依赖问题。
我尝试了什么?
1) http://www.philosophicalhacker.com/2014/10/02/an-alternative-multiproject-setup-for-android-studio/ 
- 这是一个漫长的手动过程,Android Studio不直接支持它
php ×4
oop ×3
android ×2
c# ×1
class ×1
database ×1
dependencies ×1
interface ×1
javascript ×1
model ×1
monitoring ×1
project ×1
surfaceview ×1
web ×1
winforms ×1